通知可达性从来没有所谓 [英] Reachability Notification Never Called

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

问题描述

我在我的code。使用可达不好受。我想保持它很简单,通过启动在发射的观察员,然后就收到更改通知。在下面的code时,reachabilityChanged方法不会被调用。我试过多次迭代,但是这是最简单的版本。它编译和运行。请帮助...

的**的 * 的* AppDelegate.h code * 的** *

 #进口<的UIKit / UIKit.h>#IFDEF PHONEGAP_FRAMEWORK
    #进口<的PhoneGap / PGViewController.h>
    #进口<的PhoneGap / PGURLProtocol.h>
    #进口<的PhoneGap / Reachability.h>
#其他
    #进口PGViewController.h
    #进口PGURLProtocol.h
    #进口Reachability.h#万一@interface的AppDelegate:NSObject的< UIApplicationDelegate,UIWebViewDelegate,PGCommandDelegate> {
    * NSString的invokeString;
}@属性(非原子,副本)的NSString * invokeString;
@属性(非原子,强)IBOutlet中的UIWindow *窗口;
@属性(非原子,强)IBOutlet中PGViewController *的viewController;@结束

的**的 * 的* AppDelegate.m code段 * 的** *

 #进口AppDelegate.h#进口MainViewController.h#IFDEF PHONEGAP_FRAMEWORK
    #进口<的PhoneGap / PGPlugin.h>
    #进口<的PhoneGap / PGURLProtocol.h>
    #进口<的PhoneGap / Reachability.h>
#其他
    #进口PGPlugin.h
    #进口PGURLProtocol.h
    #进口Reachability.h#万一@implementation的AppDelegate
@synthesize invokeString,窗口的viewController; - (无效)reachabilityChanged:(NSNotification *)的通知
{    的NSLog(@???????? code从不送过来??????????);    *可达性达到= [通知对象]
    NSParameterAssert([到达isKindOfClass:[可达性类]);
    NetworkStatus remoteHostStatus = [到达currentReachabilityStatus]    如果(remoteHostStatus == NotReachable){的NSLog(@****无法接通****);}
    否则,如果(remoteHostStatus == ReachableViaWiFi){的NSLog(@**** ****无线); }
    否则,如果(remoteHostStatus == ReachableViaWWAN){的NSLog(@**** ****细胞); }
} - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{    [NSNotificationCenter defaultCenter]的addObserver:自我选择:@选择(reachabilityChanged :)名称:kReachabilityChangedNotification对象:无];    *可达性达到= [可​​达reachabilityForInternetConnection]
    [到达startNotifier]    NetworkStatus remoteHostStatus = [到达currentReachabilityStatus]    的NSLog(@???? ALWAYS INITS WITH无法接通????);
    如果(remoteHostStatus == NotReachable){的NSLog(@的init ****无法接通****);}
    否则,如果(remoteHostStatus == ReachableViaWiFi){的NSLog(@INT **** ****无线); }
    否则,如果(remoteHostStatus == ReachableViaWWAN){的NSLog(@的init **** ****细胞); }    // ...}@结束


解决方案

您的对象可达性自动释放所以它的dealloc而不是工作了。

我试试你的code和它的工作对我来说:

AppDelegate.h code

  [...]
@财产(保留,非原子)可达*范围;
[...]

AppDelegate.m code段

  [...]
@synthesize到达; - (无效)reachabilityChanged:(NSNotification *)的通知
{    的NSLog(@!!!!!!!!!! code是CALL NOW !!!!!!!!!!);    NetworkStatus remoteHostStatus = [到达currentReachabilityStatus]    如果(remoteHostStatus == NotReachable){的NSLog(@****无法接通****);}
    否则,如果(remoteHostStatus == ReachableViaWiFi){的NSLog(@**** ****无线); }
    否则,如果(remoteHostStatus == ReachableViaWWAN){的NSLog(@**** ****细胞); }
} - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{    [NSNotificationCenter defaultCenter]的addObserver:自我选择:@选择(reachabilityChanged :)名称:kReachabilityChangedNotification对象:无];    self.reach = [可达reachabilityForInternetConnection] //保留范围
    [到达startNotifier]    NetworkStatus remoteHostStatus = [到达currentReachabilityStatus]    的NSLog(@???? ALWAYS INITS WITH无法接通????);
    如果(remoteHostStatus == NotReachable){的NSLog(@的init ****无法接通****);}
    否则,如果(remoteHostStatus == ReachableViaWiFi){的NSLog(@INT **** ****无线); }
    否则,如果(remoteHostStatus == ReachableViaWWAN){的NSLog(@的init **** ****细胞); }    // ...}[...]
- (无效){的dealloc
    [发布范围];
    [超级的dealloc];
}@结束

I am having a hard time using Reachability in my code. I would like to keep it very simple by initiating an observer at launch and then just receiving change notifications. In the following code, the reachabilityChanged method is never called. I’ve tried many iterations but this is the simplest version. It compiles and runs. Please help...

**** AppDelegate.h code ****

#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 code snippet ****

#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

解决方案

Your object Reachability is autorelease so it's dealloc and not working anymore.

I try your code and it's working for me:

AppDelegate.h code

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

AppDelegate.m code snippet

[...]
@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

这篇关于通知可达性从来没有所谓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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