Objective c UIWebView未刷新

Objective c UIWebView未刷新,objective-c,ios,uiwebview,webview,refresh,Objective C,Ios,Uiwebview,Webview,Refresh,我希望每次应用程序激活时(通过主屏幕启动或双击home按钮),应用程序中的webview都会刷新 我的ViewController.m如下所示: - (void)viewDidLoad { NSURL *url = [NSURL URLWithString:@"http://cargo.bplaced.net/cargo/apptelefo/telefonecke.html"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; [_w

我希望每次应用程序激活时(通过主屏幕启动或双击home按钮),应用程序中的webview都会刷新

我的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;
}

这个代码有什么问题?提前谢谢

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

您要做的是专门注册前台事件,并在收到通知时刷新webview。您将在
视图显示:
方法中注册通知,并在
视图消失:
方法中取消注册通知。这样做是为了,当控制器消失时,当它没有向用户显示任何内容时,不会继续重新加载webview(或尝试重新加载僵尸实例并崩溃)。类似于以下的方法应该可以工作:

- (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];
}

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

您要做的是专门注册前台事件,并在收到通知时刷新webview。您将在
视图显示:
方法中注册通知,并在
视图消失:
方法中取消注册通知。这样做是为了,当控制器消失时,当它没有向用户显示任何内容时,不会继续重新加载webview(或尝试重新加载僵尸实例并崩溃)。类似于以下的方法应该可以工作:

- (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];
}