ホームボタンを押したらURLをリロードしたい。私はこのスレッドに記載されているコードを試しました: 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