我有一个Xcode应用程序,我正在更新到最新的iOS.我现在注意到在构建时我有以下错误/警告:
/ConfViewController.m:198:46:'sendSynchronousRequest:returningResponse:error:'不推荐使用:首先在iOS 9.0中弃用 - 使用[NSURLSession dataTaskWithRequest:completionHandler:](参见NSURLSession.h
根据我的阅读,我应该开始使用"NSURLSession",但如何在我的代码中使用"NSURLSession",或者我是否正确地看到了这个?
我的代码:
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];
}
Run Code Online (Sandbox Code Playgroud)
非常感谢你的时间.
更换
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
Run Code Online (Sandbox Code Playgroud)
同
[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * data, NSURLResponse * response, NSError * error) {
}] resume];
Run Code Online (Sandbox Code Playgroud)将整个代码放在sendSynchronousRequest完成块中的行之后(在大括号之间).
更换
if ([response statusCode] >=200 && [response statusCode] <300)
Run Code Online (Sandbox Code Playgroud)
同
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
NSInteger statusCode = httpResponse.statusCode;
if (statusCode >= 200 && statusCode < 300)
Run Code Online (Sandbox Code Playgroud)替换urlData为data.
删除
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
2890 次 |
| 最近记录: |