从 NSURLConnection 到 NSURLSession

From NSURLConnection to NSURLSession

将 NSURLConnection 转换为 NSURLSession 的最佳方法是什么?

错误信息:(Xcode)

ViewController.m:342:45: 'stringByAddingPercentEscapesUsingEncoding:' is deprecated: first deprecated in iOS 9.0 - Use -stringByAddingPercentEncodingWithAllowedCharacters: instead, which always uses the recommended UTF-8 encoding, and which encodes for a specific URL component or subcomponent since each URL component or subcomponent has different rules for what characters are valid.

我的代码:

-(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");

}

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

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

stringByAddingPercentEncodingWithAllowedCharacters:

Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.

传递给以下方法的字符

  • (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 class and related classes provide an API for downloading content via HTTP. This API provides a rich set of delegate methods for supporting authentication and gives your app the ability to perform background downloads when your app is not running or, in iOS, while your app is suspended.

To use the NSURLSession API, your app creates a series of sessions, each of which coordinates a group of related data transfer tasks. For example, if you are writing a web browser, your app might create one session per tab or window. Within each session, your app adds a series of tasks, each of which represents a request for a specific URL (and for any follow-on URLs if the original URL returned an HTTP redirect).

Like most networking APIs, the NSURLSession API is highly asynchronous. If you use the default, system-provided delegate, you must provide a completion handler block that returns data to your app when a transfer finishes successfully or with an error. Alternatively, if you provide your own custom delegate objects, the task objects call those delegates’ methods with data as it is received from the server (or, for file downloads, when the transfer is complete).