2012-04-17 25 views
6

Tôi gặp khó khăn khi sử dụng Khả năng hiển thị trong mã của tôi. Tôi muốn giữ nó rất đơn giản bằng cách bắt đầu một người quan sát lúc khởi động và sau đó chỉ nhận được thông báo thay đổi. Trong đoạn mã sau, phương thức reachabilityChanged không bao giờ được gọi. Tôi đã thử nhiều lần lặp nhưng đây là phiên bản đơn giản nhất. Nó biên dịch và chạy. Xin vui lòng giúp ...Thông báo khả năng hiển thị Không bao giờ được gọi là

** * * đang 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 

** * * Đoạn mã 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 
+0

Chắc chắn danh hiệu và/hoặc thẻ có thể được cập nhật để làm cho câu hỏi này dễ nhận biết hơn ... –

+0

Chỉ cần chỉnh sửa - tựa đề hy vọng và thẻ có nhiều tính mô tả hơn. –

Trả lời

26

Đối tượng của bạn reachability được autorelease vì vậy nó dealloc và không làm việc nữa.

tôi cố gắng mã của bạn và nó làm việc cho tôi:

đang AppDelegate.h

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

đang AppDelegate.m đoạn

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

Tôi nghĩ thế thôi! Tôi đã thử sửa đổi của bạn bằng cách bật Chế độ trên máy bay và tôi thấy thông báo nhật ký thích hợp. Cảm ơn bạn rất nhiều! –

+0

@Sarah Moreland Bạn được chào đón. Vì bạn là một người dùng mới, tôi tự do nhắc nhở bạn bỏ phiếu cho câu trả lời của tôi nếu bạn thích nó và/hoặc chấp nhận trả lời nếu nó giải quyết được vấn đề của bạn. Nút ở trên cùng bên trái của câu trả lời của tôi –

+0

Cảm ơn, tôi đã kiểm tra câu trả lời chấp nhận nhưng tôi không thể bỏ phiếu vì tôi không có danh tiếng 15. Tôi sẽ làm nếu tôi có thể. Đó là một câu trả lời tuyệt vời. –

Các vấn đề liên quan