2013-08-01 20 views
2

我正在爲iOS6.0和>開發應用程序。它是基於選項卡的導航控制器應用整個應用程序的「處理無互聯網」

我有說10 UIViewControllers每個視圖 - 控制需要互聯網的工作。

所以現在我的問題是什麼是最好的方式來處理沒有互聯網的東西?此外,如果Internet連接可用,那麼應用程序必須再次正常工作。

附:我完全知道Reachability類。但我不想在所有10個視圖控制器中設置可訪問性更改通知。

必須有某種方法可以做到這一點意味在任何視圖控制器中,它將顯示沒有互聯網視圖和互聯網回來時它像往常一樣工作。類似禮物沒有互聯網視圖當互聯網不存在或類似。但不知道如何?

不知道,雖然不過我聽說,蘋果已經提供了一些東西,顯示在頂部的應用程序的「不上網」消息,且不會允許用戶瀏覽互聯網,除非回來。

我需要完全一樣的。

任何成功的指南將非常感激。

回答

4

我會選擇稍微不同的方法。爲什麼不創建一個爲您處理互聯網連接通知的UIViewController子類?你可以做這樣的半僞代碼。我剛剛寫下了這些代碼。它可能包含錯誤。所以不要複製&粘貼。

@interface SMInternetBaseViewController : UIViewController { 
    SMOverlay* overlay; 
} 
@end 

@interface SMInternetBaseViewController() 
- (void)reachabilityChanged:(NSNotification *) not; 
@end 

@implementation SMInternetBaseViewController 
- (id)init { 
    self = [super init]; 
    if (self) { 
     // Register here the method reachabilityChanged for Reachability notifications 
    } 
    return self; 
} 

- (void)reachabilityChanged:(NSNotification *) not 
{ 
    // Define here how to behave for different notifications 

    if (__NO_INTERNET__) { 
     // Add an overlay to the view. 
     if (!overlay) { 
      overlay = [[SMOverlay alloc] init]; 
     } 
     [self.view addSubview:overlay]; 
    } 

    if (__AGAIN_INTERNET__) { 
     [overlay removeFromSuperview]; 
    } 

} 
@end 

然後,你可以很容易地使這種SMInternetBaseViewController的所有視圖控制器子類,你不必再關心它。

0

你並不需要創建通知您可以使用可達性類檢查網絡連接作爲

Reachability *networkReachability = [Reachability reachabilityForInternetConnection]; 
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];  

if (networkStatus == NotReachable) 
{ 
    //Notify that internet is not available 

} 
0

您的電話可能還是有互聯網,但是你的服務器或域可能會下降,來處理這個正確使用與您的域名平以下。

#import <SystemConfiguration/SystemConfiguration.h> 

if ([self gotConnection]) //do what you want 
else //warn that your server is down 

- (BOOL)gotConnection 
{ 
    static BOOL checkNetwork = YES; 
    BOOL check; 
    if (checkNetwork) { 
     checkNetwork = NO; 

     Boolean success;  
     const char *host_name = "<#yourdomain.com#>"; 

     SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name); 
     SCNetworkReachabilityFlags flags; 
     success = SCNetworkReachabilityGetFlags(reachability, &flags); 
     check = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired); 
     CFRelease(reachability); 
    } 
    return check; 
} 
相關問題