可达性变化导致崩溃

时间:2015-04-12 15:48:55

标签: ios memory-leaks nsnotificationcenter reachability

Apple开发人员的Reachability样本在我的应用程序中播放离线音乐(using this code)期间关闭wifi时导致内存泄漏,并且它被卡在我提到的底线,这可能是因为该播放器代码因为log {{{ 1}}

[__NSCFString reachabilityDidChange:]: unrecognized selector

在这种方法中:

[[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];

这是log:

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [CDVReachability class]], @"info was wrong class in ReachabilityCallback");

    CDVReachability* noteObject = (__bridge CDVReachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotificationString object: noteObject];
}

file

中有2015-04-12 19:52:03.416 HelloCordova[4094:1250289] -[__NSCFString reachabilityDidChange:]: unrecognized selector sent to instance 0x174232820
reachabilityDidChange

我不知道是不是正确但我试试这个:

#import "ReachabilityManager.h"
#import "Reachability.h"

@interface ReachabilityManager ()
@property NetworkStatus previousReachability;
@end

@implementation ReachabilityManager

- (id) init {
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                selector:@selector(reachabilityDidChange:)
                                                name:kReachabilityChangedNotification
                                                object:nil];
        self.previousReachability = -1;
    }
    return self;
}

- (void) reachabilityDidChange:(NSNotification *)notification {
    NSLog(@"reachabilityDidChange!");

    CDVReachability * r = [notification object];
    NetworkStatus ns = [r currentReachabilityStatus];

    if (self.delegate && [self.delegate respondsToSelector:@selector(reachabilityDidChangeFrom:to:)]) {
        [self.delegate reachabilityDidChangeFrom:self.previousReachability to:ns];
    }
    self.previousReachability = ns;
}

@end

甚至这个:

dispatch_async(dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kReachabilityChangedNotification object:noteObject];
    });}

0 个答案:

没有答案