將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");
}
@phl_Austria這可能幫助你 - :https://www.objc.io/issues/5-ios7/from-nsurlconnection-to-nsurlsession/ –
顯示updateZipDownloaded方法的代碼。 – user3182143
檢查我的答案。它可以幫助你。 – user3182143