2012-03-29 2 views
0

모든 웹보기 탭이있는 응용 프로그램이 있습니다. 장치가 인터넷에 액세스 할 수 없을 때 오류가 발생하도록 UIWebViewDelegate를 사용하고 있습니다. Reachability 클래스를 사용하여 연결 상태의 변경 사항을 추적합니다.인터넷에 다시 연결하면 WebView가 복구되지 않음

문제는 이것이다 :

나는 동안 (I 탭이 이동
  • (내가 인터넷 연결이 끊어 말하는 메시지가) 내 인터넷 연결을 죽여 탭 하나
  • 로 이동
    1. 인터넷 연결
    2. 이 내가 인터넷을 다시 연결
    3. 내가 UIWebViewDelegate 방법을 통해 didFailLoadWithError을 탭 두 가지의 오류 메시지가) 사라
    4. 내가 만든 새로 고침 버튼을 눌렀는데 아무 것도 얻지 못합니다. 내가 탭 하나 또는 다른 탭으로 돌아갈 경우, 그것은

    내가있는 UIWebView 오류를 밖으로 내가 뭔가를 다시 초기화해야하지만 일단 내가 모르는 확신 잘 작동 문제

  • 입니다 뭐??????

    다음은 탭 코드입니다.

    #import "MINWebTab2Controller.h" 
    
    @implementation MINWebTab2Controller 
    @synthesize webView; 
    @synthesize timer; 
    @synthesize progressIndicator; 
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
    { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
        // Custom initialization 
    } 
    return self; 
    } 
    
    - (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. 
    } 
    
    #pragma mark - View lifecycle 
    
    - (void)viewDidLoad 
    { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
    
    // Set up progress indicator for web page load 
    //timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self   selector:@selector(webViewLoading) userInfo:nil repeats:YES]; 
    //[progressIndicator startAnimating]; 
    
    webView.delegate = self; 
    webView.scalesPageToFit = YES; 
    
    NSString *urlAddress = @"http://www.mobilityinitiative-synergy.com/index.php/presentations"; 
    
    //Create a URL object. 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    
    //URL Requst Object 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    
    //Load the request in the UIWebView. 
    [webView loadRequest:requestObj]; 
    
    } 
    
    - (void)viewDidUnload 
    { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    } 
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
    { 
    // Return YES for supported orientations 
    return YES; 
    } 
    
    #pragma mark UIWebViewDelegate methods 
    - (void)webViewDidStartLoad:(UIWebView *)thisWebView 
    { 
    [progressIndicator startAnimating]; 
    } 
    
    - (void)webViewDidFinishLoad:(UIWebView *)thisWebView 
    {  
    //stop the activity indicator when done loading 
    [progressIndicator stopAnimating]; 
    } 
    
    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { 
    NSLog(@"Error for WEBVIEW: %@", [error description]); 
    [progressIndicator stopAnimating]; 
    } 
    
    @end 
    

    이것은 주 대리인 클래스의 코드입니다. 보시다시피, 저는 Apple에서 제공 한 Reachability 클래스 (파생 클래스)를 사용하고 있습니다.

    #import "MINAppDelegate.h" 
    #import "Reachability.h" 
    
    @implementation MINAppDelegate 
    
    @synthesize window = _window; 
    @synthesize rootController; 
    @synthesize connectedToInternet; 
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    connectedToInternet = YES; 
    
    // Set up Root Controller 
    [[NSBundle mainBundle] loadNibNamed:@"TabBarController" owner:self options:nil]; 
    [self.window addSubview:rootController.view]; 
    
    // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the 
    // method "reachabilityChanged" will be called. 
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil]; 
    
    // allocate a reachability object 
    Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"]; 
    
    // here we set up a NSNotification observer. The Reachability that caused the notification 
    // is passed in the object parameter 
    [[NSNotificationCenter defaultCenter] addObserver:self 
                 selector:@selector(reachabilityChanged:) 
                  name:kReachabilityChangedNotification 
                  object:nil]; 
    
    [reach startNotifier]; 
    
    
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
    } 
    
    //Called by Reachability whenever status changes. 
    - (void) reachabilityChanged: (NSNotification*)note 
    {   
    Reachability * reach = [note object]; 
    
    if([reach isReachable]) 
    { 
        if(connectedToInternet == NO) 
        { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Change Detected" 
                     message:@"You are now connected to the internet and can continue to use application." 
                     delegate:nil 
                   cancelButtonTitle:@"OK" 
                   otherButtonTitles:nil]; 
         [alert show]; 
        } 
        connectedToInternet = YES; 
    } 
    else 
    { 
        if(connectedToInternet == YES) 
        { 
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Change Detected" 
                     message:@"You must be connected to the internet to use this app. Please connect to internet and reload the application." 
                     delegate:nil 
                   cancelButtonTitle:@"OK" 
                   otherButtonTitles:nil]; 
         [alert show]; 
         //exit(0); 
        } 
        connectedToInternet = NO; 
    }  
    } 
    
    - (void)applicationWillResignActive:(UIApplication *)application 
    { 
    /* 
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    */ 
    } 
    
    - (void)applicationDidEnterBackground:(UIApplication *)application 
    { 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    */ 
    } 
    
    - (void)applicationWillEnterForeground:(UIApplication *)application 
    { 
    /* 
    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
    */ 
    } 
    
    - (void)applicationDidBecomeActive:(UIApplication *)application 
    { 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
    } 
    
    - (void)applicationWillTerminate:(UIApplication *)application 
    { 
    /* 
    Called when the application is about to terminate. 
    Save data if appropriate. 
    See also applicationDidEnterBackground:. 
    */ 
    } 
    
    @end 
    
  • +0

    새로 고침 버튼에 연결되는 기능은 어디에 있습니까? – rosslebeau

    +0

    xib 파일에 연결했습니다. 이 버튼은 오류가없는 한이 버튼을 포함한 다른 모든 탭에서 작동합니다. 제대로 호출되면 진행 표시기가 표시됩니다. 오류가 발생하면 진행률 표시기가 보이지 않습니다. – JustLearningAgain

    +0

    도달 가능성 알림에 대한 응답으로 무엇을하고 있습니까? – rosslebeau

    답변

    0

    저는 이것이 최선의 해결책이라고 확신하지 않지만 여기에 제가 한 일이 있습니다.

    viewDidLoad에서 viewDidAppear으로 webview를 시작하는 코드를 이동했습니다. 탭으로 이동할 때 viewDidAppear 메서드가 호출됩니다. 이 메서드에서 적절한 코드 줄을 호출하여 webview ([webview loadRequest : URL];)를 호출합니다.

    또한 bPageLoaded에 플래그를 추가하고 웹 페이지를로드하는 동안 오류가 발생하면이 값을 false로 설정합니다. viewDidAppear 메서드에서 해당 플래그를 검사하여 오류가있을 때를 제외하고 페이지를 다시 그리지 않을 것입니다.

    관련 문제