在Settings.bundle
中,我有一个带有标识符的文本输入url_preference
。
使用ViewController.h
,ViewController.m
和我的故事板我有一个UIWebView
设置显示来自设置的 url:
- (void) updateBrowser {
NSString *fullURL = [[NSUserDefaults standardUserDefaults] stringForKey:@"url_preference"];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_EmbeddedBrowser loadRequest:requestObj];
}
这行得通。
但是,当设置中的 URL 发生更改时,UIWebView
不会更新以反映新的 URL。
通过禁止应用在后台运行,解决了不反映更新 URL 的问题。但是,出现了一个新问题:如果设置中的 URL保持不变,则不会保留会话。UIWebView
只有在url_preference
更改时才应更新。
我一直在尝试使用applicationWillEnterForeground
inAppDelegate.m
来强制UIWebView
重新加载,但是我遇到了麻烦。
在 ViewController 中,我可以运行:
- (void)viewDidLoad {
[self updateBrowser];
}
但是当我尝试在 App Delegate 中运行相同的东西时它不会更新:
- (void)applicationWillEnterForeground:(UIApplication *)application
{
ViewController *vc = [[ViewController alloc]init];
[vc updateBrowser];
}
(我也包括- (void) updateBrowser;
在ViewController.h
,#import "ViewController.h"
和AppDelegate.m
谢谢你。