在库中调用回调

时间:2014-03-05 11:22:12

标签: ios iphone callback

我正在使用可达性类,其中我必须在其中调用名为“startNotifier”的方法,后者又调用回调函数。我正在使用创建包含reachability.h / .m的lib和其他两个带有以下代码的文件:

wifi.h

#import <Foundation/Foundation.h>
#import "Reachability.h"

@protocol MyDelegate
-(void)currentInternetStatus:(BOOL)isInternetAvailable;
@end

@interface wifi : NSObject
{
    Reachability *internetReachable;
    Reachability *hostReachable;
    id <MyDelegate>delegate ;
}
#pragma mark Booleans
@property BOOL hostActive;
@property BOOL internetActive;
@property (nonatomic,retain)id <MyDelegate>delegate ;

#pragma mark Functions
-(void)internetAvailability;

@end

wifi.m

#import "wifi.h"

@implementation wifi

@synthesize hostActive,internetActive,delegate;

#pragma mark Methods for checking Network Connection

/** Method for checking Network Connection*/
-(void)internetAvailability
{
[[NSNotificationCenter defaultCenter]removeObserver:self name:kReachabilityChangedNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability alloc]init];
//Reachability *internetReachable = [Reachability reachabilityForInternetConnection];
internetReachable = [Reachability reachabilityForLocalWiFi];
[internetReachable startNotifier];
}


/** Notification called to check networks presence*/
- (void) checkNetworkStatus:(NSNotification *)notice
{
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");

            self.internetActive = NO;
            [self.delegate currentInternetStatus:NO];
            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;
            [self.delegate currentInternetStatus:NO];
            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;
            [self.delegate currentInternetStatus:NO];
            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;

            break;

        }
    }

}
@end

当我在appdelegate中编写相同的代码时,它可以工作,但是当我写相同的代码(.h / .m)时 在图书馆,它没有。

这就是我使用它的方式:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    wifi *obj = [[wifi alloc]init];
    obj.delegate = self;
    [obj internetAvailability];

    return YES;
}

- (void)currentInternetStatus:(BOOL)isInternetAvailable
{
    NSLog(@"net status %d", isInternetAvailable);
}

1 个答案:

答案 0 :(得分:0)

您正在创建wifi *obj = [[wifi alloc]init];,但从不保留它。因此,在application:didFinishLaunchingWithOptions:完成后,实例将被销毁。

由于检查网络可用性的过程是异步的,因此永远不会发送通知。

您需要保留obj个实例。例如,将其存储在AppDelegate

中的“强”属性中