How can I make an automation that triggers when my iPhone connects/disconnects from wifi

For the standard WiFi disconnect detection to work you need a way to contact your HA installation, when you are not on your home WiFi, like VPN, portforwarding or NabuCasa subscription.