【发布时间】:2016-03-23 21:09:45
【问题描述】:
我的 UIWebView 有一个奇怪的问题,它不接受任何更改。
当我启动应用程序时,它会完美地加载 URL,委托方法会被触发,但是 UIWebView 不接受之后的任何操作,例如隐藏它或更改它的 URL。当我调用 [webView loadRequest:urlRequest]; 将当前 URL 更改为另一个时,委托方法被触发,但 WebView 继续查看 OLD 页面,没有变化(仅指示器显示几秒钟,然后在 webViewDidStartLoad 和 @987654323 中显示日志命令@ 和 shouldStartLoadWithRequest 工作正常)。
这是下面的场景:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad");
NSString *urlString = @"http://old.example.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
// For here, everything is fine, the WebView loads the OLD URL
webView.delegate = self;
webView.opaque = NO;
webView=[[UIWebView alloc]init];
[webView setBackgroundColor:[UIColor grayColor]];
[webView setDelegate:(id<UIWebViewDelegate>)self];
[self.view addSubview:webView];
activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator.frame = CGRectMake(200.0, 200.0, 100.0, 40.0);
activityIndicator.center = self.view.center;
[self.view addSubview: activityIndicator];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(loadNotificationUrl:)
name:@"loadNotificationUrl" object:nil];
}
-(void)loadNotificationUrl:(NSNotification*) notification
{
// Now the notification arrives holding the NEW URL
NSDictionary* userInfo = notification.userInfo;
NSString* NotificationUrl = (NSString*)userInfo[@"url"];
NSLog (@"Successfully received: %@", NotificationUrl); // it gets --> "http://new.example.com"
NSURL *url = [NSURL URLWithString:NotificationUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSLog(@"Before Load: %@", [webView stringByEvaluatingJavaScriptFromString:@"window.location.href"]); // --> this is showing "about:blank"!!
[webView loadRequest:urlRequest]; // This works but doesn't change anything in the WebView! OLD page still showing.
}
谁能告诉我我错过了什么?
【问题讨论】:
标签: ios objective-c uiwebview