是否有任何事件或任何其他方式来检测 iPad 或 iPhone 何时超出特定 WiFi 范围。我搜索了这个,只能找到可达性类,而且我必须在后台不断检查连接性,这在我的情况下不是首选。任何帮助将不胜感激...
问问题
481 次
2 回答
2
这是可用于查找 wifi/互联网连接的示例代码
.h 文件
@class Reachability;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
Reachability* wifiReach;
}
.m 文件
**
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPhone" bundle:nil];
} else {
self.viewController = [[HPViewController alloc] initWithNibName:@"HPViewController_iPad" bundle:nil];
}
_navCon =[[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = _navCon;
// Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the
// method "reachabilityChanged" will be called.
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];
//Change the host name here to change the server your monitoring
wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];
[self showAlertOfInternetConncetion: wifiReach];
return YES;
}
//Called by Reachability whenever status changes.
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
[self showAlertOfInternetConncetion: curReach];
}
- (void)showAlertOfInternetConncetion : (Reachability*) curReach
{
NetworkStatus netStatus = [curReach currentReachabilityStatus];
switch (netStatus)
{
case NotReachable:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet connection" message:@"Your internet connection has stopped working. Please check it." delegate:self cancelButtonTitle:nil otherButtonTitles:@"Exit App", nil];
[alert show];
[alert release];
}
case ReachableViaWWAN:
{
NSLog(@"ReachableVia WWAN");
}
case ReachableViaWiFi:
{
NSLog(@"ReachableVia WiFi");
}
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
exit(0);
}
}
**
于 2012-12-19T10:53:26.437 回答
0
使用 Apple 的 SCNetworkReachability 类。阅读SCNetworkReachability 文档。测试使用:
(ReachabilityInst.currentReachabilityStatus().value == ReachableViaWiFi.value)
于 2016-02-11T08:19:21.813 回答