![](/img/trans.png)
[英]GTMHTTPFetcher.m 'initWithRequest:delegate:start Immediately:' is deprecated: first deprecated in iOS 9.0 - Use NSURLSession (see NSURLSession.h)
[英]'initWithRequest:delegate:' is deprecated: first deprecated in iOS 9.0 - use NSURLSession(see NSURLSession.h)
我是目标C的新手,并使用iOS 9 Xcode 7.2并获得此警告“'initWithRequest:delegate:'已被弃用:首先在iOS 9.0中弃用 - 使用NSURLSession(请参阅NSURLSession.h)”如何解决它。 我的代码如下。
+(id)createGetConnectionWithName:(NSString*)strConnectionName_
withUrl:(NSString *)pageUrl
parameterNames:(NSArray *)arrParamNames
parameterValues:(NSArray *)arrParamValues
delegate:(id)delegate
{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@",pageUrl]];
NSMutableString *post =[NSMutableString string];
for(int i=0;i<[arrParamNames count];i++)
{
if(i==[arrParamNames count]-1)
{
[post appendFormat:@"%@=%@",[arrParamNames objectAtIndex:i],
[arrParamValues objectAtIndex:i]];
}
else
{
[post appendFormat:@"%@=%@&",[arrParamNames objectAtIndex:i],
[arrParamValues objectAtIndex:i]];
}
}
// if(![strConnectionName_ isEqualToString:APP_AUTH_CODE_NAME])
// [post appendFormat:@"&Key=%@",[self getAuthCode]];
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/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
// conn =[[NSURLConnection alloc] initWithRequest:request delegate:self] ;
return [[self alloc] initWithRequest:request delegate:delegate];
}
提前致谢。
不知道背景,就不可能说。 看起来它可能是NSURLConnection上类别的一部分,在这种情况下,您必须将它转换为NSURLSession上的类别中的实例方法(使其创建数据任务),然后将整个代码库转换为使用NSURLSession。
几个大障碍:
结果是,这可能是对代码的重大更改,具体取决于您的委托方法的复杂程度。 如果你实际上没有使用委托(或者没有对它们做太多的事情),那么这是一项微不足道的任务。
或者只是做
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// offending line of code goes here
#pragma clang diagnostic pop
并且不要担心。 如果NSURLConnection很快就会消失,我会感到惊讶,考虑到有多少代码会破坏它。 如果我错了,至少你会在路上捣乱这个问题一段时间。 :-)
在iOS 9中不推荐使用NSURLConnection
。您可以使用自iOS 7以来存在的NSURLSession。
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
// do something with the data
}];
[dataTask resume];
您也可以使用像Alamofire这样的第三方图书馆。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.