iOS中 WGAFN_網絡監控 技術分享 – iPhone手機開發 iPhone軟體開發教學課程

需要用到第三方AFNetworking/SVProgressHUD

沒有的可以關註我微博私信我.https://weibo.com/hanjunqiang

 

AppDelegate.m

 

#import AFNetworking.h
#import SVProgressHUD.h

代碼實現比較簡單:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    


    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
    [SVProgressHUD setRingThickness:8];
    
    // 1. 獲得網絡監控的管理者
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    
    // 2. 設置網絡狀態改變後的處理
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 當網絡狀態改變瞭, 就會調用這個block
        switch (status) {
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@未知網絡);
                break;
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@沒有網絡(斷網));
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@手機自帶網絡);
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@WIFI);
                
                [SVProgressHUD showSuccessWithStatus:@WIFI];

                break;
        }
        
    }];
    
    // 3. 開始監控
    [manager startMonitoring];



    return YES;
}

最終效果:

 

 

發佈留言