在iPhone应用程序中显示没有互联网错误

时间:2011-05-10 01:22:43

标签: iphone xcode

你好我再次尝试在下面的应用程序中显示错误,如果没有互联网连接。这是文件。 FirstViewController.h:

//
//  FirstViewController.h
//  DailyJoke
//
//  Created by John Bridge on 5/4/11.
//  Copyright 2011 Bridge and co. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface FirstViewController : UIViewController {
    IBOutlet UIWebView *webView;
}
@end

这是FirstViewController.m:     //     // FirstViewController.m     // DailyJoke     //     //由John Bridge于2011年5月4日创建。     // Copyright 2011 Bridge and co。版权所有。     //

#import "FirstViewController.h"


@implementation FirstViewController




-(void)awakeFromNib
{
        [webView loadRequest:[NSURLRequest requestWithURL:[NSURL             URLWithString:@"http://www.johnbridge180.com/IPhoneIPad/IPhone/DailyJoke/DailyJoke.html"]]];
}

/*
// The designated initializer. Override to perform setup that is required before the     view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a     nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

3 个答案:

答案 0 :(得分:2)

将UIWebViewDelegate实现到您的控制器类。并覆盖控制器类中的以下方法。这些方法名称是不言自明的。您需要相应地处理错误。通过在viewDidLoad方法中设置以下句子,将webview委托设置为控制器类。也不要忘记将UIWebViewDelegate实现到您的类。

  
      
  • webView.delegate = self;
  •   
#pragma mark webView delegate
-(void)webViewDidStartLoad:(UIWebView *)wView{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = TRUE;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    if ([error code] == -999) {
    }   
    else if([error code] == -1009 || [[error localizedDescription] isEqualToString:@"no Internet connection"]){
        [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;
    }
    else if([error code] == -1001 || [[error localizedDescription] isEqualToString:@"timed out"]){      
    }
    else if([error code] == -1004 || [[error localizedDescription] isEqualToString:@"can’t connect to host"]){              
    }
    else if (error != NULL) {       
    }
    else{       
    }
}
-(void)webViewDidFinishLoad:(UIWebView *)wView{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = FALSE;  
}

希望它有所帮助。

答案 1 :(得分:1)

很多人使用apple提供的Reachability示例:http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

看起来像: +(可达性*)reachabilityWithHostName:(NSString *)hostName; 将是开始的地方。

另一种方法是使用NSURLConnection加载HTML并将其提供给webview。如果URL连接失败,则客户端可能没有Internet连接。

答案 2 :(得分:0)

如果您正在使用:

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

您可以在此处找到网络错误代码:

https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/constant_group/URL_Loading_System_Error_Codes