sendSeynchronousRequest

时间:2015-10-13 06:34:15

标签: ios rest ios9

我想发送一些编码为JSON的信息。我的代码在iOS 8.X中运行,但现在不是iOS 9.X.

CadenaConParametros = @"webme.mydomain.com/json/name/age/";
NSString *str = CadenaConParametros;
    NSURL *WcfSeviceURL = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];] init];

[request setURL:WcfSeviceURL];
[request setHTTPMethod:@"POST"];
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

但变量" respData"是零,控制台没有任何显示。

我已经配置了我的" info.plist":

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSExceptionDomains</key>
    <dict>
        <key>http://webme.mydomain.com</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionRequiresForwardSecrecy</key>
            <false/>
        </dict>
    </dict>
</dict>

有什么问题?

1 个答案:

答案 0 :(得分:0)

首先,我认为您应该确保您的网址(NSURL WcfSeviceURL)在设置为您的请求之前有效。

其次,如您所知,在iOS 9中已弃用[NSURLConnection sendSynchronousRequest:returningResponse:error:],因此您应该使用NSURLSession代替。

NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:WcfSeviceURL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
    if (error) {
    } else {
    }
}];

[task resume];
相关问题