2012-11-30 31 views
1

我是新的應用程序開發,並已做出使用uiwebviews使用幾個視圖控制器的應用程序。 我想添加一個alertview,當iphone沒有連接到互聯網。 我試過實現不同的代碼,但是alertview不會彈出。沒有互聯網連接的xcode警報視圖

繼承人我的代碼。

// 
// ViewControllersocialgo.h 
// GO!SocialMedia 
// 
// Created by Adam Rais on 28/11/2012. 
// Copyright (c) 2012 SocialGO. All rights reserved. 
// 

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

@interface ViewControllersocialgo : UIViewController <UIWebViewDelegate> 
@property (strong, nonatomic) IBOutlet UIWebView *socialgo; 

@end 

和ViewControllerpractical.m

// 
// ViewControllerpractical.m 
// GO!SocialMedia 
// 
// Created by Adam Rais on 28/11/2012. 
// Copyright (c) 2012 SocialGO. All rights reserved. 
// 

#import "ViewControllersocialgo.h" 
#import <sys/socket.h> 
#import <netinet/in.h> 
#import <SystemConfiguration/SystemConfiguration.h> 





@interface ViewControllersocialgo() 

@end 

@implementation ViewControllersocialgo 


-(BOOL) isInternetAvailable { 
    struct sockaddr_in zeroAddress; 
    bzero(&zeroAddress, sizeof(zeroAddress)); 
    zeroAddress.sin_len = sizeof(zeroAddress); 
    zeroAddress.sin_family = AF_INET; 

    SCNetworkReachabilityRef reachability =  SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct  sockaddr*)&zeroAddress); 
if(reachability != NULL) { 
    //NetworkStatus retVal = NotReachable; 
    SCNetworkReachabilityFlags flags; 
    if (SCNetworkReachabilityGetFlags(reachability, &flags)) { 
     if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 
     { 
      // if target host is not reachable 
      return NO; 
     } 

     if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 
     { 
      // if target host is reachable and no connection is required 
      // then we'll assume (for now) that your on Wi-Fi 
      return YES; 
     } 


     if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0) || 
      (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 
     { 
      // ... and the connection is on-demand (or on-traffic) if the 
      //  calling application is using the CFSocketStream or higher APIs 

      if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 
      { 
       // ... and no [user] intervention is needed 
       return YES; 
      } 
     } 

     if ((flags & kSCNetworkReachabilityFlagsIsWWAN) ==  kSCNetworkReachabilityFlagsIsWWAN) 
     { 
      // ... but WWAN connections are OK if the calling application 
      //  is using the CFNetwork (CFSocketStream?) APIs. 
      return YES; 
     } 
    } 
} 

return NO; 

} 




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[_socialgo setDelegate:self]; 
NSString *[email protected]"http://adam182testing.comoj.com/socialgo.html"; 
NSURL *url=[NSURL URLWithString:fullURL]; 
NSURLRequest *requestOBJ=[NSURLRequest requestWithURL:url]; 
[_socialgo loadRequest:requestOBJ]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

感謝

回答

2

您的視圖控制器必須實現UIWebViewDelegate,試試這個代碼

ViewControllerpractical.h

// 
// ViewControllerpractical.h 
// GO!SocialMedia 
// 
// Created by Adam Rais on 28/11/2012. 
// Copyright (c) 2012 SocialGO. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface ViewControllerpractical : UIViewController <UIWebViewDelegate> 
@property (strong, nonatomic) IBOutlet UIWebView *practical; 

@end 

ViewControllerpractical.m

// 
// ViewControllerpractical.m 
// GO!SocialMedia 
// 
// Created by Adam Rais on 28/11/2012. 
// Copyright (c) 2012 SocialGO. All rights reserved. 
// 

#import "ViewControllerpractical.h" 

@interface ViewControllerpractical() 

@end 

@implementation ViewControllerpractical 

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
               message:@"Can't connect. Please check your internet Connection" 
               delegate:self 
             cancelButtonTitle:@"OK" 
             otherButtonTitles:nil]; 
[alert show]; 
} 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
[_practical setDelegate:self]; 
NSString *[email protected]"http://adam182testing.comoj.com/practical.html"; 
NSURL *url=[NSURL URLWithString:fullURL]; 
NSURLRequest *requestOBJ=[NSURLRequest requestWithURL:url]; 
[_practical loadRequest:requestOBJ]; 
} 

- (void)didReceiveMemoryWarning 
{ 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 

@end 

不管怎麼說,作爲禮Ganem說,爲更好地利用可達性。

我在我的項目使用此功能:

-(BOOL) isInternetAvailable { 
    struct sockaddr_in zeroAddress; 
    bzero(&zeroAddress, sizeof(zeroAddress)); 
    zeroAddress.sin_len = sizeof(zeroAddress); 
    zeroAddress.sin_family = AF_INET; 

    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr*)&zeroAddress); 
    if(reachability != NULL) { 
     //NetworkStatus retVal = NotReachable; 
     SCNetworkReachabilityFlags flags; 
     if (SCNetworkReachabilityGetFlags(reachability, &flags)) { 
      if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 
      { 
       // if target host is not reachable 
       return NO; 
      } 

      if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 
      { 
       // if target host is reachable and no connection is required 
       // then we'll assume (for now) that your on Wi-Fi 
       return YES; 
      } 


      if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand) != 0) || 
       (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 
      { 
       // ... and the connection is on-demand (or on-traffic) if the 
       //  calling application is using the CFSocketStream or higher APIs 

       if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 
       { 
        // ... and no [user] intervention is needed 
        return YES; 
       } 
      } 

      if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 
      { 
       // ... but WWAN connections are OK if the calling application 
       //  is using the CFNetwork (CFSocketStream?) APIs. 
       return YES; 
      } 
     } 
    } 

    return NO; 

} 
+0

我已經嘗試了上面的代碼,仍然沒有運氣。 – Adam

+0

出現錯誤,請嘗試新的錯誤。它的工作,我已經測試了它 – jcesarmobile

+0

非常感謝你,讓UIAlertView工作。接下來會嘗試使用蘋果可達性。 – Adam

3

不要等到錯誤,告訴他有沒有互聯網連接的用戶。使用Apple自己的方法檢測連接性 - 使用ARC的Apple sample projectthis version。它會立即告訴你用戶是否失去連接。

+0

非常感謝。我將學習如何將其實施到我的項目中。 – Adam

+0

如果您發現我的答案有幫助,請考慮接受它作爲正確答案。 –