iphone - 检测设备是否超出 wifi 范围

标签 iphone objective-c ios ipad

是否有任何事件或任何其他方法可以检测 iPad 或 iPhone 何时超出特定 WiFi 范围。我搜索了这个,只能找到 Reachability 类,而且我还必须在后台不断检查连接性,这在我的情况下不是首选。任何帮助将不胜感激...

最佳答案

这是您可以用来查找 wifi/internet conctivity 的示例代码

.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);
    }
}

**

关于iphone - 检测设备是否超出 wifi 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950468/

相关文章:

iphone - IOS:我可以在xcode中通过恢复id获取对象吗?

C#检测是否是Iphone插件

iphone - 使用 Cocoa-Touch 在 iOS 中将 html 文件转换为 PDF 文档

iphone - 更改 UITableview 中节标题的颜色

ios - 如何找出为什么函数在 iOS 中被弃用?

ios - UITabBarController 以编程方式添加 Controller 导致通知打开时出现问题

iphone - Objective-C:使用参数对数组进行排序

iphone - 通过网站分发 iPad 应用程序(.ipa 格式文件)

iphone - 识别圆圈手势以删除注释,如何检测圆圈?

iphone - Webservice调用卡住ipad应用