简体   繁体   中英

How to get an IP address in iOS 9 using NSURLSession class

I have multiple hardware devices that contain mini web servers which because they use DHCP can have any IP address on the local domain.

I use NSURLSession to scan through the range of IP Addresses .2 to .253 trying to GET an index.html that these devices host.

This works great as long as I have App Transport Security turned off, of course as soon as I turn it on the calls fail because it doesn't allow IP address connections.

Is there anyway to do this short of leaving ATS off and praying Apple allows me through when I explain the need?

A bit of pseudo code :)

App figures out its own IP address say 192.168.0.10
for i in 2..<254 {
    creates a new ip address of 192.168.0.2, .3, .4 etc etc
    NSURLSession to https://above IP address/index.html
    if ( succeeds ) {
         I found a device
    }
}

Turn on ATS and this is no longer allowed

Sure. You can use HTTPS with a self-signed certificate for the IP address, then provide a means for the user to paste in either allowed keys or allowed custom root certs (your choice), then override TLS chain validation in a way that automatically accepts responses that either A. pass the normal cert checks or B. match those specified keys or are signed by those root certs.

I'm not saying that this is trivial, but it can be done.

With that said, you really shouldn't be probing a network like that. You should use service discovery. That won't help with the certificate problem, but it will make your app a lot less likely to get rejected for abusing the LAN. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM