iOS uiwebview goBack 历史控制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10872498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:36:46  来源:igfitidea点击:

iOS uiwebview goBack history control

iphoneobjective-ciosxcodecocoa-touch

提问by Jaume

I am loading a "webpage1" on an uiwebview and saving its first request on an instance variable "webviewHistory". Then, I must reuse that webview to load another page, "webpage2" (saved its first request to "webviewHistory" too), and history should start now from this request. Problem is if I execute goBack (button IBAction) from "webpage2", I can keep going back to "webpage1" history and following. If I check request and compare with initial saved, works! but not with redirections, for example, if I trigger request to www.youtube.com, request is redirectioned to m.youtube.com and first one is not saved as navigation history! how to solve it?

我正在 uiwebview 上加载“webpage1”并将其第一个请求保存在实例变量“webviewHistory”上。然后,我必须重用该 webview 来加载另一个页面“webpage2”(也将其第一个请求保存到“webviewHistory”),并且历史现在应该从这个请求开始。问题是,如果我从“webpage2”执行 goBack(按钮 IBAction),我可以继续返回到“webpage1”历史记录和关注。如果我检查请求并与初始保存的进行比较,则有效!但不是重定向,例如,如果我触发对 www.youtube.com 的请求,请求将重定向到 m.youtube.com 并且第一个不会保存为导航历史记录!如何解决?

if (![webViewSocial.request.URL.absoluteString isEqualToString:self.webviewHistory]){
    [webViewSocial goBack];
    }

回答by Msencenb

UIWebviews have instance methods that allow you to go backwards and forwards with the history, without you having to track the last request. Is there a particular reason you are saving the last request specifically?

UIWebviews 具有实例方法,允许您根据历史记录前后移动,而无需跟踪最后一个请求。您是否有特殊原因专门保存最后一个请求?

Sample code:

示例代码:

if ([webView canGoBack]) {
    [webView goBack];
}

Here is a project that is a simple uiwebview browser if you want to play with it: https://github.com/msencenb/UIWebView-Example

这是一个简单的 uiwebview 浏览器项目,如果您想使用它:https: //github.com/msencenb/UIWebView-Example

回答by CodeOverRide

for swift 3 version of Msencenb answer:

对于 swift 3 版本的 Msencenb 答案:

if webView.canGoBack {
    webView.goBack()
}