2017-03-15 4 views
0

안녕하세요. NSurlConnection을 사용하는 프로젝트가 있습니다. NSUrlSession으로 바꾸고 싶습니다. 다음 코드는 .KIndly 변경 사항에 대한 도움말입니다.NSUrlConnection을 NSUrlSession으로 바꿉니다.

- (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

[[[NSURLSession sharedSession]dataTaskWithRequest:request 
    completionHandler:^(NSData * _Nullable data, NSURLResponse * 
    _Nullable response, NSError * _Nullable connectionError) { 
    //Handle your response 
    }] resume]; 
시도