xcode 在 UIWebView 中同步加载 html 内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10151231/
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-09-14 23:58:16  来源:igfitidea点击:

synchronously load html content in UIWebView

iphoneobjective-ciosxcodeuiwebview

提问by 4mahmoud

im using a UIWebView to show html content in my app, the app contains two arrows to navigate between topics just like a RSS reader app, but when the user hits up or down arrow, the next topic doesn't show up until the data come back and the user still able to interact with the UI which is a bit confusing,

我使用 UIWebView 在我的应用程序中显示 html 内容,该应用程序包含两个箭头来在主题之间导航,就像 RSS 阅读器应用程序一样,但是当用户点击向上或向下箭头时,下一个主题不会显示,直到数据到来返回并且用户仍然能够与 UI 交互,这有点令人困惑,

My question: how to block the UI when user moves to the next/back topic ? in other words how to make loadHTMLString:baseURL: works as a synchronous calling ? thanks !

我的问题:当用户移动到下一个/后退主题时如何阻止 UI?换句话说,如何使 loadHTMLString:baseURL: 作为同步调用工作?谢谢 !

回答by danh

You can let the load happen asynchronously, but set the web view's userInteractionEnabled property to NO. (then back to YES, on the didFinishLoad callback).

您可以让加载异步发生,但将 Web 视图的 userInteractionEnabled 属性设置为 NO。(然后在 didFinishLoad 回调中返回 YES)。

Or you could put up a clear colored view (with userInteractionEnabled set to NO) above the web view that has an activity indicator and button that lets the user cancel the load.

或者,您可以在具有活动指示器和按钮的 Web 视图上方放置一个清晰的彩色视图(将 userInteractionEnabled 设置为 NO),让用户取消加载。

An even better idea would be to place two other web views offscreen and start loading them for page N-1 and N+1. When the user presses a page arrow, swap frames with the corresponding prefetched web view.

一个更好的主意是在屏幕外放置另外两个 Web 视图,并开始为页面 N-1 和 N+1 加载它们。当用户按下页面箭头时,将框架与相应的预取 Web 视图交换。

回答by Authman Apatira

You don't actually want to block the UI. If you do that, there is a very high probability that Apple will reject your app once you send it in for app store submission. Anything that even remotely makes the application feel unresponsive will weigh heavily on you. Instead, create a background thread using GCD or performSelectorInBackground, handle your loading in that, and then once the loading is done, make all of the information available to your UIWebView all at once and alert it to render the display.

您实际上并不想阻止 UI。如果您这样做,一旦您将其发送到应用商店提交,Apple 很有可能会拒绝您的应用。任何即使远程使应用程序感觉没有响应的事情都会给您带来沉重的负担。相反,使用 GCD 或 performSelectorInBackground 创建一个后台线程,在其中处理您的加载,然后一旦加载完成,立即将所有信息提供给您的 UIWebView 并提醒它呈现显示。

回答by Zack Morris

If you are in a pinch and have a UIPageViewController and still want to use a UISynchedWebView to ensure that a page has loaded before you run javascript, you can run a block on the main thread's event queue. Still has a slight delay while the javascript runs but won't cause recursion in the run loop.

如果您处于紧要关头并且有一个 UIPageViewController 并且仍然想使用 UISyncedWebView 来确保在您运行 javascript 之前已加载页面,您可以在主线程的事件队列上运行一个块。javascript 运行时仍有轻微延迟,但不会导致运行循环中的递归。

dispatch_async(dispatch_get_main_queue(), ^{
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];    // self.webView is a UISynchedWebView

    NSLog(@"url:%@", [self.webView stringByEvaluatingJavaScriptFromString:@"window.location.href;"]);       // shows google.com instead of about:blank
});