2013-08-01 206 views
-2

首先,我對Obj-C非常陌生,主要來自php javascrript背景。我以前的應用程序都在Phonegap中。我面臨的問題是我需要加載一個webview,但蘋果不斷拒絕它,因爲看門狗定時器踢在ios 6上。所以我試圖添加webview到一個輔助線程與主線程上的我的紡紗器,但後來我得到這個錯誤。iOS UIWebView崩潰

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

您應該更新在主線程UI,這裏是[鏈接] [1]左右。 [1]:http://stackoverflow.com/questions/4164984/iphone-update-ui-always-on-main-thread – siutsin

+0

我猜刪除所有GCD語句將做到這一點,因爲webViewDidStartLoad和webViewDidFinishLoad是異步的,您可以正確顯示並移除加載指示器。 – satheeshwaran

+0

'UIKit'不是線程安全的,即它只能在主線程上處理。此外,所有'UIKit'委託函數都會在主線程中調用,因此不需要在主線程上檢查/顯式調用它們。 – Amar

回答

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