你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> ios 判斷網絡

ios 判斷網絡

編輯:關於IOS

網絡的重要性,相信大家都知道了。這次介紹下ios是如何判斷網絡的好壞的,由於在開發中用到,所以分享給大家,很簡單。這裡要用到Reachability封裝類來實現,大家可以網上去下載Reachability.m 和Reachability.h文件,需要我提供請留言。

具體代碼:

在AppDelegate裡面實現:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.backgroundColor = [UIColor whiteColor];

//初始化登陸頁

LoginViewController *loginCtrl = [[LoginViewController alloc] init];

self.window.rootViewController = loginCtrl;

//判斷網絡

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name:kReachabilityChangedNotification

object:nil];

Reachability * reach = [Reachability reachabilityWithHostname:@"www.baidu.com"];

[reach startNotifier];

// MainViewController *mainController = [[MainViewController alloc] init];

// self.window.rootViewController = mainController;

[self.window makeKeyAndVisible];

return YES;

}

//通知

-(void)reachabilityChanged:(NSNotification*)note

{

Reachability * reach = [note object];

if([reach isReachable])

{

NSLog(@"Notification Says Reachable");

}

else

{

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"網絡已斷開" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];

[alertView show];

NSLog(@"Notification Says Unreachable");

}

}

代碼很簡單,大家可以用來試試!!!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved