用業餘時間慢慢整理一下最近的收穫,關於NSURLSession,使用起來簡單方便,因爲其中使用了Block,所以回調非常好用,暫時沒有進行深入研究,只是把最基本的使用羅列出來,有空了再神八.
//get 請求方式
<pre name="code" class="objc">//
NSString *urlString=@"";
NSURL *url=[NSURL URLWithString:urlString];
NSURLSession *urlSession=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask=[urlSession dataTaskWithURL:url completionHandler:^(NSData *data,NSURLResponse *response,NSError *error){
if (error) {
NSLog(@"error-%@",error.description);
}
else{
//進行數據處理
}
}];
//開始請求
[dataTask resume];
//Post 請求方式
<pre name="code" class="objc">//
NSURL *url=[NSURL URLWithString:urlString];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSURLSession *urlSession=[NSURLSession sharedSession];
NSURLSessionDataTask *dataTask=[urlSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
NSLog(@"error-%@",error.description);
}
else{
<span style="white-space:pre"> </span>//進行數據處理
}];
[dataTask resume];