2012-03-29 88 views
0

我有一个带有所有网页浏览标签的应用程序。我正在使用UIWebViewDelegate,以便在设备失去对互联网的访问时发生错误。我还使用Reachability类来跟踪连接状态的任何变化。重新连接到互联网后WebView无法恢复

问题是这样的:

  1. 我去片一个
  2. 杀了我的互联网连接(我得到的信息说我失去了互联网连接)
  3. 我去片两(而互联网连接不见了)
  4. 我通过UIWebViewDelegate方法获得标签两个错误信息didFailLoadWithError
  5. 我重新连接互联网
  6. 我点击了我创建的刷新按钮,但没有收到任何东西。这就是问题
  7. 如果我回到一个标签或其它任何标签,它工作正常

我敢肯定,一旦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 2012-03-29 05:53:23

+0

我把它挂在xib文件中。该按钮适用于所有其他选项卡,包括这一个,只要没有错误。它被正确调用,我看到进度指示器出现。出错后,我甚至没有看到进度指示器。 – JustLearningAgain 2012-03-29 15:52:07

+0

你是否在做任何响应可达性通知? – rosslebeau 2012-03-29 17:12:15

回答

0

我不确定这是最好的解决方案,但这是我做的。

我将代码从viewDidLoad启动到viewDidAppear。当我进入选项卡时,viewDidAppear方法被调用。在这种方法中,我调用适当的代码行来重新启动webview([webview loadRequest:URL];)

我还为bPageLoaded添加了一个标志,并将其设置为false,因此在加载网页时出现错误。我在viewDidAppear方法中检查该标志,以便在出现错误时除外重绘页面。