在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];
新闻热点
疑难解答