-2

首先,我对 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

谢谢!

4

1 回答 1

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];
   }
于 2013-08-01T06:44:57.147 回答