-(void)reach { /** AFNetworkReachabilityStatusUnknown = -1, // 未知 AFNetworkReachabilityStatusNotReachable = 0, // 無鏈接 AFNetworkReachabilityStatusReachableViaWWAN = 1, // 3G 花錢 AFNetworkReachabilityStatusReachableViaWiFi = 2, // 局域網絡,不花錢 */ // 若是要檢測網絡狀態的變化,必須用檢測管理器的單例的startMonitoring [[AFNetworkReachabilityManager sharedManager] startMonitoring]; // 檢測網絡鏈接的單例,網絡變化時的回調方法 [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) { #ifdef DEBUG NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status)); #endif if (status > 0) [UserDefaultsUtills setIsSwitchIsOn:YES]; else [UserDefaultsUtills setIsSwitchIsOn:NO]; if (status ==AFNetworkReachabilityStatusNotReachable) { [SVProgressHUD showErrorWithStatus:@"網絡異常,請檢查網絡設置!" duration:1.2]; self.window.rootViewController=errovc; } }]; }
拷貝這個方法再appdelegate。m裏面,而後在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {裏面調用就好了網絡