iOS模拟器不向运行tomcat webservice的localhost mac发送http请求

标签 ios swift swift3

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

上运行 tomcat
<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/

相关文章:

ios - SKSpriteNode 仅在 iOS 9 或更高版本上可见,无论是在模拟器还是在真实手机中?

ios - 从 Parse 返回数字字段到 Tableview 自定义单元格

android - 根据 oAuth2.0 身份验证应该在设备 native 浏览器中进行,那么为什么所有已知的应用程序都具有 native 登录屏幕

ios - 归档在 ios 中需要很多内存和更多时间吗

ios - Xcode 6 中的错误 - View Controller 没有名为( subview )的导出

iOS 横切问题

swift - 函数被调用但不执行

swift - 如何在swift中缩短网格游戏

swift - PageViewController透明或没有背景

ios - 更改 URL 最后一个组件名称并向重命名的组件添加扩展名