ios - 在新线程中加载 UIWebView 内容

标签 ios multithreading uiwebview

我有四个选项卡 Controller ,其中一个在 View Controller 中有一个 UIWebView。当应用程序启动时,我创建了一个新线程来在我的 webView 中加载 Web 内容,因此当 Web 完成加载时,当用户切换到 View Controller 时,它已准备好供用户查看。

我的问题是,webVIew 根本没有在新创建的线程中加载请求,但是当我将 loadRequest 放入 viewDidLoad 时它正在工作。我花了整整一天的时间来寻找解决方案,但一点运气也没有。

这是我的代码:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self)
        [NSThread detachNewThreadSelector:@selector(doStuff) toTarget:self withObject:nil];

     return self;
}

- (void)doStuff
{
     NSLog(@"Starting a new thread ...");

     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     url = [NSURL URLWithString:@"http://www.myURL.com"];
     NSURLRequest *request = [NSURLRequest requestWithURL:url];
     [newsWebView loadRequest:request];

     [pool release];
 }

有人可以解决我的问题吗?非常感谢。

最佳答案

 - (void)doStuff
 {
 NSLog(@"Starting a new thread ...");

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 url = [NSURL URLWithString:@"http://www.myURL.com"];
 NSURLRequest *request = [NSURLRequest requestWithURL:url];

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    [newsWebView loadRequest:request];
 });
[pool release];
}

关于ios - 在新线程中加载 UIWebView 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11857858/

相关文章:

ios - 如何在 Core Animation 中平滑地更新动画的 toValue/end 点?

java - 是否可以同时使用线程消费者/生产者?

Python计算快,串口写入慢: Multithread or Multiprocess

ios - Swift - 以模态方式打开 WebView 链接

ios - 在 UIWebView/WKWebview 中禁用自动完成

ios - 使用 scrollView 和 imageView 的 bounds.height 属性计算 zoomScaleForHeight

ios - 让应用程序在终止时保存数据

java - 即使我正在处理另一个线程,UI 线程也会卡住

iphone - 在 Web View 中使用 YouTube 播放器的 iPhone 上为 "Movie Format Not Supported"

ios - 使用联系人框架创建新组,CNErrorDomain Code = 2