【问题标题】:Why does shouldStartLoadWithRequest not load my /amp version in the webview, and does in safari?为什么 shouldStartLoadWithRequest 不在 webview 中加载我的 /amp 版本,而在 safari 中加载?
【发布时间】:2023-03-18 13:59:01
【问题描述】:

我有一个带有 UIWebView 的 iOS 应用程序。 在我的网站中,我所有的页面都有一个 /amp 版本 ( i.e www.website-example.com/pageX/amp )

shouldStartLoadWithRequest: 方法中我这样做:url = [url URLByAppendingPathComponent:@"amp"]

如果我这样做 [[UIApplication sharedApplication] openURL:url];return false;(所以它会在 Safari 中打开 url),它会打开好的 /amp 版本。

否则,如果我return false(在附加 /amp 之后),它会在 webview 中打开 url,它不会打开 /amp 版本,虽然URL 包含 /amp。

edit:我加的是Swift标签,因为不是编程语言的问题,是理解shouldStartLoadWithRequest方法。

有什么问题?

【问题讨论】:

  • 请贴一些相关代码。
  • 添加了所有必要的代码
  • 为什么没有人回答?没人知道答案吗?

标签: ios objective-c swift webview uiwebview


【解决方案1】:

您不能将/amp 附加到shouldStartLoadWithRequest 中的url,必须添加/amp 并传递给loadRequest 方法。它在 safari 中可以正常打开,因为您传递的是“好”的 url,但 UIWebView 在附加 /amp 之前打开了 url,而在 shouldStartLoadWithRequest 中,您无法操作 url。

【讨论】:

  • 嘿,谢谢你的回答。那么做我想做的事情的方法是什么?
  • 请编辑问题并粘贴代码loadRequest在哪里调用
【解决方案2】:

你应该得到这个代码:

[webView loadRequest:[NSURLRequest requestWithURL:[NSString stringWithFormat:@"%@/amp", request.URL]]];

在方法中:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

这对你有帮助。

【讨论】:

  • 谢谢,但你能解释一下为什么以及在哪里添加这个吗?它不起作用。它给出了一个警告 - requestWithURL 需要一个 URL,而不是一个字符串。
  • 在这个方法shouldStartLoadWithRequest 中,加载这个方法[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/amp", request.URL]]]];
  • 这个方法`[[UIApplication sharedApplication] openURL:url];`总是在safari中打开。
  • 问题是为什么在 Safari 中打开 /amp 而在 webview-without 中打开?你确定你明白了吗?你检查你的代码了吗?无论如何,感谢您提供帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 2010-12-09
  • 1970-01-01
  • 2011-08-18
相关资源
最近更新 更多