0
嗨我有一個使用NSurlConnection的項目,我只是想用NSUrlSession替換它。以下是代碼.KIndly幫助進行更改。用NSUrlSession替換NSUrlConnection
- (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
{
}