IOS UIWebView Crashes



首先,我对Obj-C非常陌生,主要来自phpjavascrript背景。我以前的应用程序都在Phonegap中。我面临的问题是,我需要加载一个网络视图,但苹果一直拒绝它,因为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

谢谢!

我想你用它…

 -(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];
   }

最新更新