利用AFN实时监测网络状态以及设置请求超时时间

写在appdelegate中:

- (void)AFN {

    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

    [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

        switch (status) {

            case -1:

                [SVProgressHUD showErrorWithStatus:@"未知网络"];

                break;

            case0:

                [SVProgressHUD showErrorWithStatus:@"没有网络"];

                break;

            case1:

                NSLog(@"GPRS网络");

                break;

            case2:

                NSLog(@"wifi网络");

                break;

            default:

                break;

        }

        if(status ==AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi)

        {

            NSLog(@"有网");

        }else

        {

            NSLog(@"没有网");

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"网络失去连接" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];

            alert.delegate = self;

            [alert show];

        }

    }];

}

设置请求超时时间

[_mgr.requestSerializer willChangeValueForKey:@"timeoutInterval"];

 _mgr.requestSerializer.timeoutInterval = 3.f;

[_mgr.requestSerializer didChangeValueForKey:@"timeoutInterval"];



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章