UIWebView is not refreshing



我希望每当应用程序激活时(通过主屏幕或双击主页按钮启动),我的应用程序中的网络视图都会刷新。

我的ViewController.m看起来像这样:

- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:@"http://cargo.bplaced.net/cargo/apptelefo/telefonecke.html"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[_webView loadRequest:req];
[super viewDidLoad];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_webView reload];
}
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}

这个代码怎么了?提前感谢!

我认为viewDidAppear:不会在应用程序进入前台时触发;这些viewWill*和viewDid*方法用于视图转换(模态、推送),与应用程序生命周期事件无关。

您要做的是专门注册前台事件,并在收到通知时刷新Web视图。您将在viewDidAppear:方法中注册通知,并在viewDidDisappear:方法中取消注册。这样做是为了让控制器在不向用户显示任何内容时,如果它消失,就不会继续重新加载网络视图(或尝试重新加载僵尸实例并崩溃)。以下内容应该有效:

- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[_webView reload]; // still want this so the webview reloads on any navigation changes
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)viewDidDisappear:(BOOL)animated{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil];
}
- (void)willEnterForeground {
[_webView reload];
}

最新更新