从NSURLConnection到NSURLSession

从NSURLConnection到NSURLSession

问题描述:

将NSURLConnection转换为NSURLSession的最佳方式是什么?从NSURLConnection到NSURLSession

错误消息:(Xcode中)

ViewController.m:342:45: 'stringByAddingPercentEscapesUsingEncoding:' 被弃用:在IOS 9.0第一弃用 - 使用-stringByAddingPercentEncodingWithAllowedCharacters:代替,它总是使用推荐的UTF- 8编码,并且针对特定的URL组件或子组件编码,因为每个URL组件或子组件对于哪些字符有效具有不同的规则。

我的代码:

-(void)downloadZip 
{ 
    NSLog(@"Start Downloading Zip File"); 

    NSDate *myDate = (NSDate *)[[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdate"]; 


    NSString *path = [NSString stringWithFormat:phpLinkgetZip, myDate]; 

    NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    NSLog(@"Checking update at Zip File : %@", path); 
    NSLog(@"Checking update Time : %@", myDate); 


    responseData = [[NSMutableData alloc] init]; 


    NSURLRequest* updateRequest = [NSURLRequest requestWithURL: url]; 

    NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:updateRequest delegate:self]; 
    [connection start]; 

    NSLog(@"Zip Downloading start..."); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    [responseData setLength:0]; 
    filesize = [[NSNumber numberWithLong: [response expectedContentLength] ] retain]; 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [self updateZipDownloaded]; 
    [filesize release]; 
    [connection release]; 

} 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
    NSNumber* curLength = [NSNumber numberWithLong:[responseData length] ]; 
    float progress = [curLength floatValue]/[filesize floatValue] ; 


} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Zip Downloading error"); 

} 
+1

@phl_Austria这可能帮助你 - :https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/ –

+0

显示updateZipDownloaded方法的代码。 – user3182143

+0

检查我的答案。它可以帮助你。 – user3182143

如果你想克服上述错误,请使用以下代码

NSCharacterSet *setPath = [NSCharacterSet URLPathAllowedCharacterSet]; 
NSString *strURL = [path stringByAddingPercentEncodingWithAllowedCharacters:setPath]; 

stringByAddingPercentEncodingWithAllowedCharacters:

返回从接收器做了一个新的字符串替换所有 中的字符不在指定的具有百分比编码字符的集合。

性状传递给设置为低于方法

  • (NSCharacterSet *)URLUserAllowedCharacterSet;
  • (NSCharacterSet *)URLPasswordAllowedCharacterSet;
  • (NSCharacterSet *)URLHostAllowedCharacterSet;
  • (NSCharacterSet *)URLPathAllowedCharacterSet;
  • (NSCharacterSet *)URLQueryAllowedCharacterSet;
  • (NSCharacterSet *)URLFragmentAllowedCharacterSet;

如果你想从NSURLConnetion去NSURLSession,做以下事情

NSURL *URL = [NSURL URLWithString:@"http://example.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 

NSURLSession *session = [NSURLSession sharedSession]; 
NSURLSessionDataTask *task = [session dataTaskWithRequest:request 
            completionHandler: 
^(NSData *data, NSURLResponse *response, NSError *error) { 
    // ... 
}]; 

[task resume]; 

NSURLSession类和相关类通过HTTP下载内容的API。此API提供了一组丰富的委托方法来支持身份验证,并使您的应用能够在您的应用未运行时执行后台下载,或者在iOS中暂停您的应用时执行后台下载。

要使用NSURLSession API,您的应用程序会创建一系列会话,其中每个会话都会协调一组相关的数据传输任务。例如,对于 示例,如果您正在编写Web浏览器,则您的应用程序可能会为每个选项卡或窗口创建一个 会话。在每个会话中,您的应用程序会添加一系列 任务,其中每个任务表示对特定URL的请求(如果原始URL返回HTTP重定向,则表示任何后续URL的 )。

与大多数网络APIs一样,NSURLSession API的异步性非常高,为 。如果您使用系统提供的默认代理,则您的 必须提供完成处理程序块,以便在传输成功完成或发生错误时将数据返回到您的应用程序 。或者,如果您提供自己的自定义委托对象,则任务对象将调用 这些委托人的方法以及从服务器 (或对于文件下载,当传输完成时)收到的数据。