首先,我对 Obj-C 很陌生,主要来自 php javascript 背景。我以前的应用程序都在 Phonegap 中。我面临的问题是我需要加载一个 webview,但 Apple 一直拒绝它,因为看门狗计时器在 ios 6 上启动。所以我尝试将 webview 添加到辅助线程,我的微调器在主线程上,但后来我得到了这个错误。
Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...
这是我的代码,如果有人能指出我正确的方向,我将不胜感激:)
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (dispatch_get_current_queue() == dispatch_get_main_queue()) {
NSLog(@"WebView is On Main Thread!");
} else {
NSLog(@"WebView is not On Main Thread!");
}
NSString *fullURL = @"http://www.example.com";
NSURL *url = [NSURL URLWithString: fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_mywebview.backgroundColor = [UIColor grayColor];
_mywebview.opaque=NO;
[_mywebview loadRequest:requestObj];
_mywebview.scalesPageToFit = YES;
});
}
- (void) webViewDidStartLoad:(UIWebView *)webView
{
dispatch_async(dispatch_get_main_queue(), ^{
if (dispatch_get_current_queue() == dispatch_get_main_queue()) {
NSLog(@"Spinner Start On Main Thread!");
} else {
NSLog(@"Spinner Start not On Main Thread!");
}
[_myActivity startAnimating];
});
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
dispatch_async(dispatch_get_main_queue(), ^{
if (dispatch_get_current_queue() == dispatch_get_main_queue()) {
NSLog(@"Spinner Stop On Main Thread!");
} else {
NSLog(@"Spinner Stop not On Main Thread!");
}
[_myActivity stopAnimating];
});
}
@end
谢谢!