我只想知道如何防止每次打开已连接到 Internet 的应用程序时出现警报视图。如果有帮助,我正在使用 ARC。
这是我在 AppDelegate 中的 didFinishLaunchingWithOptions 方法中的代码:
__weak id myself = self; // to silence warning for retain cycle
_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://apple.com"]];
[_httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
{
// Not reachable
NSLog(@"Not connected to the internet");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Not connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:
{
NSLog(@"Connected to the internet via WiFi");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Connected to the internet via WiFi" delegate:myself cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:
{
NSLog(@"Connected to the internet via WWAN");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
[alert show];
break;
}
default:
break;
}
}];