从不调用可达性通知

我很难在代码中使用可达性。 我希望通过在启动时发起一个观察员,然后只是接收更改通知来保持它非常简单。 在下面的代码中,reachabilityChanged方法永远不会被调用。 我已经尝试了很多迭代,但这是最简单的版本。 它编译和运行。 请帮忙…

** * * AppDelegate.h代码* ** *

#import <UIKit/UIKit.h> #ifdef PHONEGAP_FRAMEWORK #import <PhoneGap/PGViewController.h> #import <PhoneGap/PGURLProtocol.h> #import <PhoneGap/Reachability.h> #else #import "PGViewController.h" #import "PGURLProtocol.h" #import "Reachability.h" #endif @interface AppDelegate : NSObject < UIApplicationDelegate, UIWebViewDelegate, PGCommandDelegate> { NSString* invokeString; } @property (nonatomic, copy) NSString* invokeString; @property (nonatomic, strong) IBOutlet UIWindow* window; @property (nonatomic, strong) IBOutlet PGViewController* viewController; @end 

** * * AppDelegate.m代码片段* ** *

 #import "AppDelegate.h" #import "MainViewController.h" #ifdef PHONEGAP_FRAMEWORK #import <PhoneGap/PGPlugin.h> #import <PhoneGap/PGURLProtocol.h> #import <PhoneGap/Reachability.h> #else #import "PGPlugin.h" #import "PGURLProtocol.h" #import "Reachability.h" #endif @implementation AppDelegate @synthesize invokeString, window, viewController; - (void) reachabilityChanged:(NSNotification *)notice { NSLog(@"???????? CODE NEVER GETS HERE ??????????"); Reachability *reach = [notice object]; NSParameterAssert([reach isKindOfClass: [Reachability class]]); NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; if(remoteHostStatus == NotReachable) {NSLog(@"**** Not Reachable ****");} else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"**** wifi ****"); } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"**** cell ****"); } } - (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; Reachability *reach = [Reachability reachabilityForInternetConnection]; [reach startNotifier]; NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; NSLog(@”???? ALWAYS INITS WITH Not Reachable ????”); if(remoteHostStatus == NotReachable) {NSLog(@"init **** Not Reachable ****");} else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"int **** wifi ****"); } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"init **** cell ****"); } // ... } @end 

你的对象可达性是自动释放,所以它是dealloc,不再工作。

我尝试你的代码,它为我工作:

AppDelegate.h代码

 [...] @property (retain, nonatomic) Reachability* reach; [...] 

AppDelegate.m代码片段

 [...] @synthesize reach; - (void) reachabilityChanged:(NSNotification *)notice { NSLog(@"!!!!!!!!!! CODE IS CALL NOW !!!!!!!!!!"); NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; if(remoteHostStatus == NotReachable) {NSLog(@"**** Not Reachable ****");} else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"**** wifi ****"); } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"**** cell ****"); } } - (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; self.reach = [Reachability reachabilityForInternetConnection]; //retain reach [reach startNotifier]; NetworkStatus remoteHostStatus = [reach currentReachabilityStatus]; NSLog(@"???? ALWAYS INITS WITH Not Reachable ????"); if(remoteHostStatus == NotReachable) {NSLog(@"init **** Not Reachable ****");} else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"int **** wifi ****"); } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"init **** cell ****"); } // ... } [...] -(void)dealloc{ [reach release]; [super dealloc]; } @end