使用 AFNetworking 2 和可达性没有互联网连接警报
Posted
技术标签:
【中文标题】使用 AFNetworking 2 和可达性没有互联网连接警报【英文标题】:No internet connection alert using AFNetworking 2 and Reachability 【发布时间】:2013-11-14 18:58:51 【问题描述】:我正在尝试弄清楚如何使用 AFNetworking 2 和 Reachability 显示“无互联网连接”警报。
我已将 Reachability 和 AFNetworking 导入到我的控制器中。我从 AFNetworking 2 文档中复制的以 AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
开头的代码部分,我不确定它是否属于它。
更新
我的应用程序现在在没有互联网连接时显示警报,但显示警报需要很长时间,我也怀疑这是我构建代码的最佳方式。 (另外,如果我在主视图控制器上,并且在没有连接时单击单元格,应用程序崩溃,我不知道是否有解决此问题的方法。
- (void)viewDidLoad
[super viewDidLoad];
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability * reachability)
NSLog(@"Reachable");
;
reach.unreachableBlock = ^(Reachability * reachability)
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No internet connection"
message:@"No internet connection"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
NSLog(@"Not Reachable");
;
[reach startNotifier];
self.upcomingReleases = [[NSMutableArray alloc] init];
[self makeReleasesRequests];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; // Make nav items white
[self.collectionView registerClass:[ReleaseCell class] forCellWithReuseIdentifier:@"ReleaseCell"];
-(void)makeReleasesRequests
NSURL *url = [NSURL URLWithString:@"http://www.soleresource.com/upcoming.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"@");
self.upcomingReleases = [responseObject objectForKey:@"upcoming_releases"];
[self.collectionView reloadData];
failure:nil];
[operation start];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
switch (status)
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
case AFNetworkReachabilityStatusNotReachable:
default:
[operationQueue setSuspended:YES];
break;
];
谢谢。
【问题讨论】:
[到达 startNotifier];不是可选的,它需要开始通知。 Check this for what if connection lost during process 【参考方案1】:我使用 AFNetworkingOperationDidFinishNotification。每次 http 请求失败时,都会弹出警报并通知用户。
- (void)addNetworkObserver
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(HTTPOperationDidFinish:)
name:AFNetworkingOperationDidFinishNotification
object:nil];
- (void)HTTPOperationDidFinish:(NSNotification *)notification
AFHTTPRequestOperation *operation = (AFHTTPRequestOperation *)[notification object];
if (![operation isKindOfClass:[AFHTTPRequestOperation class]])
return;
if (operation.error)
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connection error"
message:@"Missing connection to the internet"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
【讨论】:
【参考方案2】:啊,我遇到了同样的问题,你需要删除 [alert show] 并添加到主线程,所以它应该是这样的,
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"No internet connection"
message:@"No internet connection"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
// [alert show];
NSLog(@"Not Reachable");
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
【讨论】:
【参考方案3】:如果您使用的是 AFNetworking 3.0,请在 ViewDidLoad 或您的 AppDelegate 上调用此代码:
// Monitoring Reachability
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status)
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
// Add your alert here
];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
【讨论】:
【参考方案4】:用可达性试试这个
Reachability * reach = [Reachability reachabilityWithHostname:@"www.google.com"];
reach.reachableBlock = ^(Reachability * reachability)
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Title" message:@"Reachable" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alrt performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:yes];
;
reach.unreachableBlock = ^(Reachability * reachability)
UIAlertView *alrt=[[UIAlertView alloc]initWithTitle:@"Title" message:@"Not Reachable" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alrt performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:yes];
;
[reach startNotifier];
【讨论】:
我以您的代码为起点更新了我的应用程序,现在它会在没有互联网连接时显示警报,但显示警报需要很长时间。我也怀疑那是我构建代码的最佳方式。 你计算过那个时间吗....不算太多,我一直用这个,从来没有遇到过这个问题。 使用主线程在reachableBlock中执行任何你想要的操作,它工作正常 应用程序打开它显示一个空白页面(因为它无法加载任何单元格),然后可能 8 秒后出现警报。我也不知道“在reachableblock中执行任何你想要的......”是什么意思 您是否在主线程中显示警报视图,例如 [yourAlrt performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:yes];【参考方案5】:确保您在reachabilityManager 上调用startMonitoring(坚持使用AFNetworking 2 内置的,在这种情况下添加Reachability 是多余的)。
[manager.reachabilityManager startMonitoring]
您可以通过在设备或模拟器上打开/关闭飞行模式来进行测试。请记住,这不会处理超时/连接缓慢/错误请求的情况。这是你必须同时处理的事情。
【讨论】:
以上是关于使用 AFNetworking 2 和可达性没有互联网连接警报的主要内容,如果未能解决你的问题,请参考以下文章