Connect to captive portals with swift

Hello, I am new to networking with Swift, so forgive me when I ask a foolish question. I am trying to automatically login into a captive portal. Does Apple give us enough access to accomplish such task?

Thank you for your help

I haven’t tried this out for myself so I’m not sure whether it actually works. But here’s what my initial plan to do this would be.

If you don’t know the URL (or you want to automatically login in to multiple different portals depending on the network you’re on)

  1. Create a URLSession that doesn’t follow redirects by using a delegate with the willPerformHTTPRedirection delegate method
  2. Make a request to a website that usually doesn’t redirect (i think www.google.com would work)
  3. Check if the status code is in the 3 hundreds (the series or redirect status codes). If so, get the Location header of the response. The value of that header should be the address of the captive portal
  4. Figure if out what credentials to use based on the portal url, and then do a post request containing the correct form fields to the correct url (you can open the portal in a browser and use the developer tools to figure out where the login form posts to and what fields it has)

If you know the url and only need to login to one portal

This is significantly easier. Just do step 4 on its own.


As I said, I haven’t tested this, but feel free to let me know what parts don’t work or whether I’ve misinterpreted your goal. I can try my best to create a better rough plan for you.

(post deleted by author)

(post deleted by author)

Thank you for your response.
Yes I know the url of the captive portal. I had the idea with doing the post request too but unfortunately I do not have wifi access at the point of connecting so the post request would not go through. Still thank you so much again.

You likely need to use a headless browser in order to programmatically click the necessary button to dismiss the prompt and connect to the internet.