DOM Ready 上的 iOS UIWebView

发布于 2025-01-02 12:28:26 字数 102 浏览 0 评论 0原文

我的问题是 UIWebView 在渲染其内容之前触发 webViewDidFinishLoad,因此在显示视图后,在显示视图内容之前会出现短暂的延迟。当 DOM 完全渲染时是否可以触发事件?

My problem is that UIWebView fires webViewDidFinishLoad before its content was rendered, therefore after I display the view I get a short delay before content of the view is displayed. Is it possible to fire an event when DOM is fully rendered?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

开始看清了 2025-01-09 12:28:26

是的,为此使用 JavaScript。在 HTML 页面中添加一个 window.onload JavaScript 处理程序,然后当它触发时,您可以执行以下两件事之一:

  1. 通过加载虚假 URL 并使用 webview 委托拦截该 URL 来调用应用程序。例如

    //在您的网页中,在 javascript 中
    
    window.onload = function() { document.location.href = 'http://madeupdomain.com/loaded'};
    
    //可可中的webview委托
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求导航类型:(UIWebViewNavigationType)navigationType
    {
        NSURL *URL = [请求网址];
        NSString *host = URL.host;
        if ([主机 isEqualToString:@"madeupdomain.com"])
        {
            //webview已经加载完毕,所以显示它
            webview.hidden = NO;
    
            返回否;
        }
        返回是;
    }
    
  2. 在 JavaScript 代码中设置一个变量标志,然后让应用程序使用计时器在 Web 视图上调用 stringByEvaluatingJavascriptFromString 来轮询 Web 视图,以查看该标志是否已设置。

    //在您的网页中,在 javascript 中
    
    window.pageHasLoaded = false;
    window.onload = function() { window.pageHasLoaded = true; };
    
    //在您的本机应用程序代码中的计时器中
    
    if ([[webview stringByEvaluatingJavascriptFromString:@"window.pageHasLoaded"] isEqualToString:@"true"])
    {
         webview.hidden = NO;
    }
    

Yes, use JavaScript for this. In your HTML page add a window.onload JavaScript handler, then when it fires you can do one of two things:

  1. call out to the application by loading a fake URL and intercepting this with the webview delegate. E.g.

    //in your web page, in javascript
    
    window.onload = function() { document.location.href = 'http://madeupdomain.com/loaded'};
    
    //webview delegate in cocoa
    
    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    {
        NSURL *URL = [request URL];
        NSString *host = URL.host;
        if ([host isEqualToString:@"madeupdomain.com"])
        {
            //the webview has finished loading, so show it
            webview.hidden = NO;
    
            return NO;
        }
        return YES;
    }
    
  2. set a variable flag in your javascript code, then have the application poll the webview with a timer calling stringByEvaluatingJavascriptFromString on the webview to see if that flag has been set yet.

    //in your web page, in javascript
    
    window.pageHasLoaded = false;
    window.onload = function() { window.pageHasLoaded = true; };
    
    //in a timer in your native app code
    
    if ([[webview stringByEvaluatingJavascriptFromString:@"window.pageHasLoaded"] isEqualToString:@"true"])
    {
         webview.hidden = NO;
    }
    
南冥有猫 2025-01-09 12:28:26

试试这个

@interface LoadNewsPageController : UIViewController <UIWebViewDelegate>
@property(nonatomic, weak) IBOutlet UIWebView *webView;
@end



@implementation LoadNewsPageController {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.webView.delegate = self;

    NSLog(@"%@", self.newsObject.url);
    NSURL *websiteUrl = [NSURL URLWithString:@"http://google.com"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
    [self.webView loadRequest:urlRequest];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if ([[webView stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {
        // UIWebView object has fully loaded.
        NSLog(@"Dom ready");

    }
}


@end

try this

@interface LoadNewsPageController : UIViewController <UIWebViewDelegate>
@property(nonatomic, weak) IBOutlet UIWebView *webView;
@end



@implementation LoadNewsPageController {

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.webView.delegate = self;

    NSLog(@"%@", self.newsObject.url);
    NSURL *websiteUrl = [NSURL URLWithString:@"http://google.com"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
    [self.webView loadRequest:urlRequest];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    if ([[webView stringByEvaluatingJavaScriptFromString:@"document.readyState"] isEqualToString:@"complete"]) {
        // UIWebView object has fully loaded.
        NSLog(@"Dom ready");

    }
}


@end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文