简体   繁体   English

如何使用NSURLSession类在iOS 9中获取IP地址

[英]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. 我有多个包含迷你Web服务器的硬件设备,因为他们使用DHCP可以在本地域上拥有任何IP地址。

I use NSURLSession to scan through the range of IP Addresses .2 to .253 trying to GET an index.html that these devices host. 我使用NSURLSession扫描IP地址范围.2到.253试图获取这些设备托管的index.html。

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. 只要我关闭了App Transport Security,当我打开呼叫失败时,它就会很好用,因为它不允许IP地址连接。

Is there anyway to do this short of leaving ATS off and praying Apple allows me through when I explain the need? 无论如何,除了ATS之外没有做到这一点并祈祷Apple允许我在解释需求时通过吗?

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 打开ATS,不再允许这样做

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. 您可以将HTTPS与自签名证书一起用于IP地址,然后为用户提供粘贴允许密钥或允许的自定义根证书(您的选择)的方法,然后以自动接受响应的方式覆盖TLS链验证 A.传递正常的证书检查或B.匹配那些指定的密钥或由这些根证书签名。

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. 这对证书问题没有帮助,但它会使你的应用程序因滥用局域网而不太可能被拒绝。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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