objective-c - 从未调用可达性通知

标签 objective-c ios app-store appstore-approval 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

最佳答案

您的对象 Reachability 是自动释放的,因此它是 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

关于objective-c - 从未调用可达性通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10184082/

相关文章:

iphone - 与 UIGestureRecognizer 一起监听 UITouch 事件

iphone - 如果操作从 UIAlertView 委托(delegate)启动,则 UIViewController 在从导航 Controller 弹出后不会被释放

iOS 应用程序加载器 - 应上传什么类型的文件

ios - 公开可用的 iOS app_id 到 app_bundle 映射?

objective-c - 将 UIImage 转换为 tiff/tif 文件

ios - 通过 json post 发送类

iOS 10 UNNotificationAction,可以从后台模式切换到前台模式吗?

ios - TableView 中混合 UIPicker 和 TextField

ios - 将 View 作为标题嵌入 SwiftUI 导航栏中

ios - 苹果开发者账号注册-个人账号-卖家名称