【问题标题】:iOS UIWebView not accepting URL changingiOS UIWebView 不接受 URL 更改
【发布时间】: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


    【解决方案1】:

    初始请求加载到与未来请求不同的 Web 视图中。如果你查看你的代码,你有[webView loadRequest:urlRequest];

    接下来几行你有webView=[[UIWebView alloc]init];,它将webView 变量设置为一个新的UIWebView 实例,然后你将它添加到你的视图中。这个网页视图是不可见的,因为它的框架大概是CGRectZero。委托方法正在触发,因为您已将新实例的委托设置为 self,但您还是看不到结果。

    删除webView=[[UIWebView alloc]init];,假设您已在其他地方初始化了 Web 视图,事情可能会如您所愿。

    【讨论】:

    • 你让我开心,谢谢!这是为了什么? webView=[[UIWebView alloc]init];
    【解决方案2】:

    loadRequest:urlRequest之后,你创建了一个新的UIWebView,它覆盖了旧的webView,请删除它。

    【讨论】:

      【解决方案3】:

      移动这一行:

      webView=[[UIWebView alloc]init];
      

      在这一行之前:

      [webView loadRequest:urlRequest];
      

      在这个方法中:

      viewDidLoad
      

      【讨论】:

        【解决方案4】:

        移除 webView = [UIWebView alloc]init];应该工作

        【讨论】:

          猜你喜欢
          • 2020-11-18
          • 2018-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-03-14
          • 2010-11-29
          • 2014-04-04
          • 2016-04-21
          相关资源
          最近更新 更多