![](/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.