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

Sid*_*rth 9 ios swift swift3

重要信息,我在TestCase下运行所有​​这些.

我的网址是 http://my-MacBook-Pro.local:8080/myapi/v2/Driver/getDriver?domain=123&driver=125&key=9808098

如果我在Safari运行时粘贴此URL iPhone Simulator.它以JSON响应.我注意到我的catalina.out报告了这个请求.

我启用Allow HTTP ServicesSettings->Developer.

这是代码段

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()
Run Code Online (Sandbox Code Playgroud)

编辑

参考 添加以下内容info.plist,仍然我的http请求没有通过localhost运行tomcat的我的macport 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> 
Run Code Online (Sandbox Code Playgroud)

编辑

即使使用这些设置也可以尝试info.plist.没有影响.做了Clean,但没有影响.

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoadsInWebContent</key>
        <true/>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
Run Code Online (Sandbox Code Playgroud)

我想我的代码有问题.

Sid*_*rth 1

在测试用例下运行网络调用,需要XCTestExpectation.

测试用例不等待async completionHandlers被调用。不幸的是,XCTestExpectation框架的设计不友好。我们应该能够集成测试用例,而不必更改我们的类结构。

让我们追踪苹果论坛上对此问题的回应