2011-05-10 7 views
0

안녕하세요. 다시 인터넷에 연결되어 있지 않으면 아래 앱에 오류가 표시됩니다. 다음은 파일입니다. FirstViewController.h :iphone 앱에 인터넷 오류가 표시되지 않습니다.

// 
// 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 // 은 5/4/11에 존 브리지에 의해 만들어진 //. // 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 

답변

1

로트 사과 의해 제공된 도달 가능성 예를 사용 http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

모습 : + (도달 가능성 *) reachabilityWithHostName (는 NSString *) 여기서 hostName; 이 시작됩니다.

또 다른 방법은 NSURLConnection을 사용하여 HTML을로드하고 웹보기에 제공하는 것입니다. URL 연결이 실패하면 클라이언트에 인터넷 연결이없는 것일 수 있습니다.

+0

이 아직도 혼동하고있어 srry 메신저 당신은 샘플 코드를 시도하는 시간을 소요 –

+0

새로운 포기하기 전에 더 많은 문서를 읽으십시오. – pzearfoss

2

컨트롤러 클래스에 UIWebViewDelegate를 구현하십시오. 컨트롤러 클래스의 다음 메소드를 재정의 (override)하십시오. 이 메소드 이름은 자명하다. 그에 따라 오류를 처리해야합니다. viewDidLoad 메서드에서 다음 문장을 설정하여 컨트롤러 클래스에 웹 뷰 대리자를 설정하십시오. 또한 클래스에 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; 
    } 
    

는 도움이되기를 바랍니다.

관련 문제