Last active
August 29, 2015 14:25
-
-
Save stdrc/48543ed23872fb9b6d7a to your computer and use it in GitHub Desktop.
NSURLConnection 发送异步请求
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 代码如下: | |
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] | |
queue:[[NSOperationQueue alloc] init] | |
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
//异步请求完成后的操作 | |
}]; | |
// 如果要在 completionHandler 中更改界面, 需要用以下代码: | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^ { | |
//更改界面的操作 | |
}]; | |
// 示例如下: | |
[acticityIndicator startAnimating]; | |
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]] | |
queue:[[NSOperationQueue alloc] init] | |
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { | |
[[NSOperationQueue mainQueue] addOperationWithBlock:^ { | |
[acticityIndicator stopAnimating]; | |
}]; | |
if (connectionError == nil) { | |
[data writeToFile:[NSTemporaryDirectory() stringByAppendingPathComponent:@"data.plist"] | |
atomically:YES]; | |
} | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment