使用AFNetworking 2和Reachability没有互联网连接警报

Chr*_*oya 1 reachability ios afnetworking

我正试图弄清楚如何使用AFNetworking 2和Reachability显示"无互联网连接"警报.

我将Reachability和AFNetworking导入到我的Controller中.我的代码部分以我从AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];AFNetworking 2文档中复制开始,我不确定它是否属于它.

UPDATE

我的应用程序现在显示一个警报,只要没有互联网连接,但它需要太长时间才能显示警报,我也怀疑这是我可以构建我的代码的最佳方式.(另外如果我在主视图控制器上,当没有连接时我点击一个单元格应用程序崩溃,我不知道是否有办法解决这个问题).

- (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;
        }
    }];

}
Run Code Online (Sandbox Code Playgroud)

谢谢.

car*_*ich 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];
   }
}
Run Code Online (Sandbox Code Playgroud)