使用蘋果的默認可達性類:從您的項目This link
複製Reachability.h和Reachability.m文件
下載可達性項目。
並在應用程序委託文件中設置此方法。
-(void)initializeRechabilityObeserver
{
//Change the host name here to change the server your monitoring
hostReach = [Reachability reachabilityWithHostName: @"www.apple.com <http://www.apple.com>"];
[hostReach startNotifier];
//[self updateInterfaceWithReachability: hostReach];
internetReach = [Reachability reachabilityForInternetConnection];
[internetReach startNotifier];
//[self updateInterfaceWithReachability: internetReach];
wifiReach = [Reachability reachabilityForLocalWiFi] ;
[wifiReach startNotifier];
//[self updateInterfaceWithReachability: wifiReach];
}
對於下面的代碼獲得可達性變化的通知應用:
在應用didFinishLaunching添加該通知方法
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
internetReachable = [Reachability reachabilityForInternetConnection] ;
[internetReachable startNotifier];
}
,並添加這個方法:
- (void)reachabilityChanged: (NSNotification*)note
{
NSLog(@"Reachability changed");
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self updateInterfaceWithReachability: curReach];
}
我已經使用了相同的代碼,你mentioned..But它不與WiFi工作啓用設備 – NSS