Reputation: 3461
I have a Xcode app which I am updating to the latest iOS. I now notice that on building I have the following error/warning:
/ConfViewController.m:198:46: 'sendSynchronousRequest:returningResponse:error:' is deprecated: first deprecated in iOS 9.0 - Use [NSURLSession dataTaskWithRequest:completionHandler:] (see NSURLSession.h
From what I have read I should start to use "NSURLSession" but how do I use "NSURLSession" in my code, or am I looking at this incorrectly?
My code:
NSString *deviceName = [[UIDevice currentDevice]name];
NSString *post =[[NSString alloc] initWithFormat:@"devicename=%@",deviceName];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://www.mydomain/sysscripts/conf/devicelookup17.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
//The ERROR point
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
// NSLog(@"%@",jsonData);
// NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSInteger roomid = [(NSNumber *) [jsonData objectForKey:@"roomid"] integerValue];
// NSLog(@"%ld",(long)success);
//NSLog(@"%ld",(long)roomid);
NSString *RoomID = [NSString stringWithFormat:@"%ld",(long)roomid];
// NSLog(@"%@", RoomID);
NSString *firstString = @"http://www.mydomain/apps/conf/lon/dt/devices/ /template17.php";
// NSLog(@"%@", firstString);
NSString *roomID = RoomID;
// NSLog(@"%@", roomID);
NSString *newString = [firstString stringByReplacingOccurrencesOfString:@" " withString:roomID];
// NSLog(@"%@", newString);
NSURL *url2 = [NSURL URLWithString: newString];
NSLog(@"%@", url2);
NSURLRequest *request2 = [NSURLRequest requestWithURL:url2];
// ConfViewController *navex =[[ConfViewController alloc] initWithNibName:nil bundle:nil];
//[self presentViewController:navex animated:YES completion:NULL];
[webView loadRequest:request2];
}
Many thanks in advance for your time.
Upvotes: 0
Views: 4777
Reputation: 285160
Replace
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
with
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
}] resume];
Put the entire code after the sendSynchronousRequest
line in the completion block (between the braces).
Replace
if ([response statusCode] >=200 && [response statusCode] <300)
with
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = httpResponse.statusCode;
if (statusCode >= 200 && statusCode < 300)
Replace urlData
with data
.
Delete
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
Upvotes: 6