在iOS7中,蘋果介紹了新的API —— NSURLsession,這一系列的API是用來替代NSURLConnection的。


1 - (id)initWithCoder:(NSCoder *)aDecoder 2 { 3 self = [super initWithCoder:aDecoder]; 4 NSLog(@"hello initWithCoder"); 5 //1 6 NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://www.okcoin.com/api/ticker.do"]]; 7 //2 8 NSURLSession *session = [NSURLSession sharedSession]; 9 //310 NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {11 if (!error) {12 //413 NSHTTPURLResponse *httPResponse = (NSHTTPURLResponse *)response;14 //515 if (httpResponse.statusCode == 200) {16 //617 NSString *string = [[NSString alloc] initWithData:data encoding:NSStringEncodingConversionAllowLossy];18 NSLog(@"%@",string);19 }20 }21 }];22 //723 [dataTask resume];24 25 return self;26 }1、將URL打包成request
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注