首先,我是Obj-C的新手,主要来自php javascrript背景。我之前的应用程序都在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
谢谢!
答案 0 :(得分:0)
我想你用它......
-(void) viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSLog(@"%@",selection);
NSURL *URL = [NSURL URLWithString:@"http://www.example.com"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:URL];
[webview loadRequest:requestObj];
}
-(void) showAlertforwait{
UIActivityIndicatorView *activity= [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake(125, 80, 30, 30)];
activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
alert= [[UIAlertView alloc]initWithTitle:@"Process"
message:@"Please Wait ..."
delegate: self
cancelButtonTitle: nil
otherButtonTitles: nil];
[alert addSubview:activity];
[activity startAnimating];
[alert show];
}
-(void)webViewDidStartLoad:(UIWebView *)webView {
[self showAlertforwait];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[alert dismissWithClickedButtonIndex: 0
animated: YES];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
NSLog(@"error");
[alert dismissWithClickedButtonIndex: 0
animated: YES];
}