我正试图弄清楚如何使用AFNetworking 2和Reachability显示“无互联网连接”警报。
我将Reachability和AFNetworking导入到我的Controller中。我的代码部分以AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
开头,我复制了AFNetworking 2文档,我不确定它是否属于它。
更新
我的应用程序现在显示一个警报,只要没有互联网连接,但警报显示需要太长时间,我也怀疑这是我可以构建我的代码的最佳方式。 (另外如果我在主视图控制器上,当没有连接时我点击一个单元格应用程序崩溃,我不知道是否有办法解决这个问题。)
- (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;
}
}];
}
感谢。
答案 0 :(得分:5)
我使用 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];
}
}
答案 1 :(得分:1)
啊,我有同样的问题,你需要删除[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];
答案 2 :(得分:1)
如果您正在使用AFNetworking 3.0,请在ViewDidLoad或您的AppDelegate上调用此代码:
// Monitoring Reachability
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
// Add your alert here
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
答案 3 :(得分:0)
尝试使用可达性
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];
答案 4 :(得分:0)
确保你在reachabilityManager上调用startMonitoring(坚持使用内置于AFNetworking 2的内容,在这种情况下添加Reachability是多余的。)
[manager.reachabilityManager startMonitoring]
您可以通过在设备或模拟器上切换飞机模式来测试此问题。请记住,这不会处理超时/慢速连接/错误请求的情况。这是你必须要处理的事情。