gpt4 book ai didi

ios - 按下主页按钮后,如何重新加载Web View ?

转载 作者:行者123 更新时间:2023-12-01 20:23:13 25 4
gpt4 key购买 nike

按下主屏幕按钮后,我想重新加载网址。我尝试了此线程中提到的代码:
How to reload a UIWebView on HomeButton press
但是一旦我从主屏幕返回到应用程序,这些都不会刷新/加载我的应用程序,这是到目前为止的代码,我有什么想法吗?

import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize webview;

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//load url into webview
NSString *strURL = @"http://myserver.com/firstpage";
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.webview.delegate = self;
[self.webview loadRequest:urlRequest];


}

- (id)init
{
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reloadWebView:)
name:UIApplicationWillEnterForegroundNotification
object:nil];
// Do some more stuff
}
return self;
}
- (void)reloadWebView:(NSNotification *)notification
{
[webview reload];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[self.webview reload];
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//No Internet Connection error code
-(void)webView:(UIWebView *)myWebView didFailLoadWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"You have no internet connection!" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
[alert show];

}
//Close app from AlertView
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
return;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

最佳答案

将此代码放入viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadWebView:) name:UIApplicationWillEnterForegroundNotification object:nil];

这个代码在 viewDidUnload
[[NSNotificationCenter defaultCenter] removeObserver:self];

关于ios - 按下主页按钮后,如何重新加载Web View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30002301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com