利用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"];



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