可达性通知从未被调用

7
我在使用Reachability时遇到了困难。我想通过在启动时初始化观察者并只接收变化通知来保持简单。在下面的代码中,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

当然,可以更新标题和/或标签以使这个问题更容易识别... - user166390
刚刚编辑过 - 希望标题和标签更具描述性。 - Sarah Moreland
1个回答

28

您的Reachability对象是自动释放的,因此它已被销毁且不再工作。

我尝试了您的代码并且它对我有效:

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

我想就这样了!我尝试了您的修改,打开了飞行模式,我看到了正确的日志消息。非常感谢您! - Sarah Moreland
@Sarah Moreland 不用谢。由于您是新用户,我想提醒您,如果您喜欢我的回答,请投票支持或接受回答,按钮位于我的答案左上方。 - Martin Magakian
谢谢,我已经检查了接受的答案,但由于我没有15个声望,无法进行投票。如果可以的话,我一定会这样做的。这是一个很棒的答案。 - Sarah Moreland

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接