IP地址变更通知 [英] ip address change notifications

查看:163
本文介绍了IP地址变更通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Reachability类来检测wifi可用性.

I'm using the Reachability class to detect wifi availability.

在某些情况下,ipad将连接到一个wifi网络,而用户将连接到另一个可用的wifi网络.

There are some situations when an ipad will be connected to one wifi network,and a user will connect to another available wifi network.

在这些网络过渡期间,不会生成可到达"->不可到达"->可到达"通知.

During these network transitions reachable->unreachable->reachable notifications are not generated.

此连接更改是iM试图监听的,其中ipad的IP地址更改了.

This connection change where the ip address of the ipad changes is what Im trying to listen for.

是否存在有关本地wifi连接更改的通知,或者我是否需要定期轮询IP?

Do notifications for local wifi connection changes exist or will I have to just poll my ip periodically?

推荐答案

我个人会使用

I would personally just poll the IP however frequently you find appropriate (once a second should be fine), using the code found here, and just store the previous occurrences result, see if it changes.

我真的建议您设置一个简单的委托类,为您执行此操作,并将自定义事件发送到可能需要这些更新的任何类.这应该很简单,尤其是考虑到您似乎有一定的经验.

What I would really recommend is setting up a simple delegate class that does this for you and will send a custom event to whatever class may need those updates. It should be pretty straight forward, especially considering it seems like you have some experience.

更新

我在下面发布了一些代码,这些代码将创建一个委托,该委托将在检测到IP的任何更改后回调任何类.请注意,由于我目前不在使用XCode的计算机前,可能会出现一些错误/错别字,但您应该了解一般情况.

I have posted some code below that will create a delegate that will call back to whatever class once it detects any change in IP. Note there may be some errors/typos as I am not in front of a computer with XCode currently, but you should get the general idea.

IPChangeNotifier.h

#import <UIKit/UIKit.h>

@protocol IPChangeNotifierDelegate;

@interface IPChangeNotifier : NSObject {
    NSString *prevIP;
    NSTimer *changeTimer;
    id changeDelegate;
}
-(id) initWithTimer:(float)time andDelegate:(id)del;
-(NSString*)getIPAddress;
-(void) checkForChange;
@end

@protocol IPChangeNotifierDelegate <NSObject>
@optional
-(void) IPChangeDetected:(NSString*)newIP previousIP:(NSString*)oldIP;
@end

IPChangeNotifier.m

#import IPChangeNotifier.h
#import <ifaddrs.h>
#import <arpa/inet.h>
@implementation IPChangeNotifier

-(id) initWithTimer:(float)time andDelegate:(id)del {
    changeTimer = [NSTimer scheduleTimerWithTimeInterval:time target:self selector:@selector(checkForChange) userInfo:nil repeats:YES];
    prevIP = @"";
    changeDelegate = del;
}

-(void) checkForChange {
    NSString *currentIP = [self getIPAddress];
    if (![currentIP isEqualToString:prevIP]) {
        if ([changeDelegate respondsToSelector:@selector(IPChangeDetected:)]){
             [changeDelegate IPChangeDetected:currentIP previousIP:prevIP];    
        }
        prevIP = currentIP;
    }
}

- (NSString *)getIPAddress {    
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    NSString *wifiAddress = nil;
    NSString *cellAddress = nil;

    // retrieve the current interfaces - returns 0 on success
    if(!getifaddrs(&interfaces)) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while(temp_addr != NULL) {
            sa_family_t sa_type = temp_addr->ifa_addr->sa_family;
            if(sa_type == AF_INET || sa_type == AF_INET6) {
                NSString *name = [NSString stringWithUTF8String:temp_addr->ifa_name];
                NSString *addr = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)]; // pdp_ip0     
                NSLog(@"NAME: \"%@\" addr: %@", name, addr); // see for yourself

                if([name isEqualToString:@"en0"]) {
                    // Interface is the wifi connection on the iPhone
                    wifiAddress = addr;    
                } else
                if([name isEqualToString:@"pdp_ip0"]) {
                    // Interface is the cell connection on the iPhone
                    cellAddress = addr;    
                }
            }
            temp_addr = temp_addr->ifa_next;
        }
        // Free memory
        freeifaddrs(interfaces);
    }
    NSString *addr = wifiAddress ? wifiAddress : cellAddress;
    return addr ? addr : @"0.0.0.0";
} 
@end

然后您可以通过在接口文件中添加<IPChangeNotifierDelegate>来简单地创建想要委托的任何类,然后通过执行以下类似的简单操作来初始化通知程序.

You can then simply make whatever class you want a delegate by adding <IPChangeNotifierDelegate> to your interface file and then initialize the notifier by doing something simple like below.

IPChangeNotifier *ipChecker = [[IPChangeNotifier alloc] initWithTimer:1.0 andDelegate:self]

还要确保包含以下方法,以确保可以获取更改事件并执行所需的任何操作.

Also make sure you have the following method included to make sure you can get the change events and do whatever you need.

-(void) IPChangeDetected:(NSString*)newIP previousIP:(NSString*)oldIP {
     // Do what you need
}

这篇关于IP地址变更通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆