Important information, I am running all this under a TestCase.
我的网址是 http://my-MacBook-Pro.local:8080/myapi/v2/Driver/getDriver?domain=123&driver=125&key=9808098
如果我将此 URL 粘贴到在同一 iPhone 模拟器
上运行的 Safari
上。它以 JSON 响应。我注意到我的 catalina.out 报告了这个请求。
我在 Settings->Developer
上启用了Allow HTTP Services
。
这是代码片段
print("fullurl ->"+urlComponents.url!.absoluteString)
URLSession.shared.dataTask(with: urlComponents.url!, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
print (json)
}catch let error as NSError{
print(error)
}
}
}).resume()
编辑
Reference
将以下内容添加到 info.plist
,但我的 http 请求仍然没有到达我的 localhost
mac 在 port 8080
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>My-MacBook-Pro.local</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
</dict>
</dict>
编辑
甚至在 info.plist
中尝试了这些设置。没有影响。执行了Clean
,但没有影响。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
我猜我的代码有问题。
最佳答案
您需要在 info.plist 文件中设置适当的标志,就像您要求跟踪用户位置或访问照片库和相机一样。默认情况下,Apple 会阻止对不安全网络调用(即 http)的请求。
您可以查看如何设置个别异常或将 NSAllowsArbitraryLoads
设置为 true 以允许 ALL
不安全调用。这自然被认为是更简单但不太安全的选择。
关于iOS模拟器不向运行tomcat webservice的localhost mac发送http请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42454667/