在寻找一种简单的异步方法来检查 iOS 中的 Internet 访问时,我有一个想法,使用 UIWebView 来处理异步:
-(void) testNet
{
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
webview.delegate=self;
NSString *urlAddress = @"http://bla.com/dummySmallFile.txt";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
//deal with no net stuff here
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
//net is there, use it here
}
这似乎有效,但我想知道在这种方法中是否存在我可能没有意识到的任何隐患。有任何想法吗?