ios
ios

Reputation: 975

xcode 7 json always showing error in Objective C

I had build a project on Xcode 6.4 . It was working fine . But the day i have upgraded my Xcode to Xcode 7, all the JSON services are not working and always giving me error . Here is my code

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);
}

It is still working fine on Xcode 6

Here is Error log

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

Upvotes: 0

Views: 312

Answers (2)

Masa
Masa

Reputation: 519

You can set some NSAppTransportSecurity values in your plist.

  • I would not set NSAllowsArbitraryLoads to be true(YES) in this case. If you're sending registration and personal user information, you should be doing that securely.
  • If you're testing, you can add your test server to ExceptionDomain

If you're connecting to a third party, they most surely have a secure server. If it's your own server, you can get a cheap signed certificate.

Upvotes: 0

arturdev
arturdev

Reputation: 11039

Open your Info.plist file, add a new row with name NSAppTransportSecurity and type of Dictionary. Then add a new row in it with name NSAllowsArbitraryLoads and type of bool and set value to YES.

enter image description here

Upvotes: 2

Related Questions