【发布时间】:2013-03-03 00:57:52
【问题描述】:
以下方法非常适用于检查用户是否有互联网连接。我想检查整个应用程序中的互联网连接,并且想知道是否可以将它放在我的 App Delegate 中的某个地方,它会被每个视图控制器调用。
有没有办法做到这一点?如果我把它放在我的 applicationDidFinishLaunching 中,它似乎无法正常工作。任何建议都会很棒!谢谢!
NSURL *scriptUrl = [NSURL URLWithString:@"http://www.google.com/m"];
NSData *data = [NSData dataWithContentsOfURL:scriptUrl];
if (data) {
NSLog(@"Device is connected to the internet");
}
else {
NSLog(@"Device is not connected to the internet");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connectivity"
message:@"You must have an internet connection" delegate:self cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
return;
}
【问题讨论】:
标签: iphone ios objective-c xcode