【问题标题】:xcode 7 json always showing error in Objective Cxcode 7 json 在 Objective C 中总是显示错误
【发布时间】:2015-11-02 08:00:04
【问题描述】:

我在 Xcode 6.4 上构建了一个项目。它工作正常。但是在我将 Xcode 升级到 Xcode 7 的那一天,所有的 JSON 服务都无法正常工作并且总是给我错误。 这是我的代码

NSDictionary *get = @{@"name":name.text,@"username":username.text,@"mobile":phonenumber.text};
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [[NSString alloc]initWithFormat:@"r=%@",jsonInputString];

NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",registerUrl]];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:120.0];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse *response;
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (responseData != nil)
{

    NSMutableDictionary *jsonDict = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSLog(@"Json Dictionary =======%@",jsonDict);
}

if (error)
{
    NSLog(@"error %@",error.description);
}

它在 Xcode 6 上仍然可以正常工作

这是错误日志

Error Domain=NSURLErrorDomain Code=-1022 "资源不能被 加载,因为应用程序传输安全策略需要使用 安全连接。”

【问题讨论】:

标签: ios objective-c iphone json xcode


【解决方案1】:

打开您的Info.plist 文件,添加名称为NSAppTransportSecurity 和类型为Dictionary 的新行。然后在其中添加一个新行,名称为NSAllowsArbitraryLoads,类型为bool,并将值设置为YES.

【讨论】:

    【解决方案2】:

    您可以在 plist 中设置一些 NSAppTransportSecurity 值。

    • 在这种情况下,我不会将 NSAllowsArbitraryLoads 设置为 true(YES)。如果您要发送注册信息和个人用户信息,您应该安全地进行。
    • 如果您正在测试,可以将测试服务器添加到 ExceptionDomain

    如果您要连接到第三方,他们肯定有一个安全的服务器。如果是您自己的服务器,您可以获得便宜的签名证书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多