gpt4 book ai didi

iphone - 阻塞主线程直到 UIWebView 完成加载

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:51:36 24 4
gpt4 key购买 nike

我试图阻塞主线程直到 UIWebView 完成加载,但是在调用 [UIWebView loadRequest:] 之后调用 [NSThread sleepForTimeInterval:] 使加载永远不会complete:webViewDidFinishLoad 委托(delegate)在 sleep 结束后被调用。

为什么会这样?以及如何阻塞主线程?
我正在从 [ViewController willRotateToInterfaceOrientation:] 调用 loadPage,我想做的是阻止 iOS 界面旋转,直到加载另一个方向的 webview .

- (void)loadPage {
UIWebView* webview2 = [[UIWebView alloc] initWithFrame:rect];
webview2.delegate = self;

NSString* htmlPath = ..... ; // Path to local html file
NSURL *newURL = [[[NSURL alloc] initFileURLWithPath: htmlPath] autorelease];
NSURLRequest *newURLRequest = [[[NSURLRequest alloc] initWithURL: newURL] autorelease];

[webview2 loadRequest:newURLRequest];
[NSThread sleepForTimeInterval:10.0f]; // Why this blocks the uiwebview thread ??
[self.view addSubview:webview2];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@"Finished loading!");
}

最佳答案

我认为您正在尝试解决错误的问题。

你不想阻塞主线程。首先,整个 UI 是在主线程上绘制的,因此看起来您的应用程序已挂起。其次,UIWebView 是 UI 的一部分(这可能就是您所做的不起作用的原因)。

那么,怎么办?

看看下面的方法:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

当您开始加载您的页面时,您可以从此方法返回 NO。加载完成后,您可以正常返回值。 UIWebView 有一个委托(delegate)告诉您状态。

关于iphone - 阻塞主线程直到 UIWebView 完成加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10484629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com