我使用的是reachability類,其中我必須調用其中的方法稱爲「startNotifier」,後者又調用回調函數。我正在使用創建一個包含reachability.h/.m和兩個其他文件與下面的代碼:在庫中調用回調
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);
}
描述它是如何工作的。顯示你如何使用'wifi'類。你爲什麼保留代表? – Wain
我已經在上面添加了代碼。 – user2798258
那麼,它怎麼不起作用?它是否編譯?如果是,我猜它運行,所以問題是你沒有收到你的'currentInternetStatus:'回調:是嗎? – Romain