首先在 Device 的 iOS 9 中用 NSURLConnection 连接

First connect with NSURLConnection in iOS 9 on Device

我用的是XCode7.

对于iOS 9 我加入info.plist

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

在应用程序中我有登录按钮。当用户按下登录按钮时,我尝试从 Internet

加载一些数据
 NSData *respPayload = [NSData dataWithContentsOfFile:filePath];

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:endpointUrl];
 [request setHTTPMethod:@"POST"];
 [request addValue:@"text/xml; charset=UTF-8" forHTTPHeaderField:@"content-type"];
 [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
 [request setHTTPShouldHandleCookies:NO];
 [request setHTTPBody:data];   
  respPayload = [NSURLConnection sendSynchronousRequest:request returningResponse:&resp error:&err];

我收到错误

kCFURLErrorTimedOut = -1001

如果用户再次按下登录按钮,此代码将完美运行。问题是当我第一次尝试加载数据时出现错误,这只是第一次。听起来很奇怪。在 iOS 8 我没有这个问题。对于模拟器,我没有这个问题,只有 iOS 9 中的设备。谢谢

sendAsynchronousRequest 在 iOS 9

中被弃用

像下面这样使用NSURLSession

NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // The server answers with an error because it doesn't receive the params
}];
[uploadTask resume];

我什么都试过了。

对于我的问题,请帮助添加到 info.plist 我的域:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>YOUR_URL.com</key>
        <dict>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <true/>
            <key>NSIncludesSubdomains</key>
            <false/>
            <key>NSRequiresCertificateTransparency</key>
            <false/>
            <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
            <false/>
            <key>NSThirdPartyExceptionMinimumTLSVersion</key>
            <string>TLSv1.2</string>
            <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
            <true/>
        </dict>
    </dict>
</dict>