您好我有一个使用NSurlConnection的项目我只想用NSUrlSession替换它。下面是代码.KIndly帮助改变。
- (IBAction)getResponse:(id)sender
{
[status setText:@"Retrieving response async"];
[response setText:@""];
NSURL *requestUrl = [[NSURL alloc] initWithString:url.text];
NSURLRequest *request = [NSURLRequest requestWithURL:requestUrl cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
}
- (void)connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"Response recieved");
}
- (void)connection:(NSURLConnection*) connection didReceiveData:(NSData *)data
{
NSLog(@"Data recieved");
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[response setText:responseString];
[status setText:@"Response retrieved async"];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
}
答案 0 :(得分:0)
试试这个
[[[NSURLSession sharedSession]dataTaskWithRequest:request
completionHandler:^(NSData * _Nullable data, NSURLResponse *
_Nullable response, NSError * _Nullable connectionError) {
//Handle your response
}] resume];