2
我修改了蘋果的可達性類以便與我的服務器ip一起使用。但是當我使用reachabilityWithAddress
時,它不會在應用啓動時調用reachabilityChanged
。它只被稱爲互聯網連接狀態已更改。 (就像關閉Wi-Fi一樣),但是,如果我使用reachabilityWithHostName
,reachabilityChanged
函數在應用啓動時調用。IP可達性類
我缺少什麼?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
///////////////////////////////////////////////////////////////////////////////////
// Reachability Local Notifications
///////////////////////////////////////////////////////////////////////////////////
hasInternetConnection = NO;
struct sockaddr_in address;
address.sin_len = sizeof(address);
address.sin_family = AF_INET;
address.sin_port = htons(80);
address.sin_addr.s_addr = inet_addr("X.X.X.X");
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name:kReachabilityChangedNotification object: nil];
hostReach = [Reachability reachabilityWithAddress:&address];
[hostReach startNotifier];
...
}
的方法
然後:
-(void)reachabilityChanged:(NSNotification*)note
{
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
if (curReach == hostReach) {
NetworkStatus netStatus = [curReach currentReachabilityStatus];
if (netStatus != ReachableViaWiFi && netStatus != ReachableViaWWAN) {
hasInternetConnection = NO;
}
else {
hasInternetConnection = YES;
}
}
else {
DLog(@"Something go wrong!");
}
}