利用web界面操做iDevice時,在處理Web請求的方法web
-(void)calloutHandler { NSString *telNum = @"123456"; AppDelegate *appdelegate = [[UIApplication sharedApplication] delegate]; InitialViewControler *initialViewController = (InitialViewController *)appDelegate.window.rootViewController; initialViewController.telNum = telNum; [initialViewController callout]; }
InitialViewController
中的處理方法app
- (void)callout { NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", self.telNum]; UIWebView *phoneCallWebView = [[UIWebView alloc] init]; [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]]; [self.view addSubview:phoneCallWebView]; }
問題來了,使用UIWe不View處理打電話這個動做是爲了電話結束的時候可以返回應用界面,同時又沒有使用網上說的那個可能會上不了AppStore的方法(沒試過)。關於代碼打電話能夠看這篇文章。若是使用這種方法,在UIWebView *phoneCallWebView = [[UIWebView alloc] init]
這句就跑不動了,報tried to obtain the web lock from a thread other than the main thread or the web thread. UIKit should not be called from a secondary thread
balabala。大致意思就是操做UIKit必須在主線程上。因此咱們使用GCD把UIWebView *phoneCallWebView = [[UIWebView alloc] init]
提到主線程上來,方法以下async
- (void)callout { NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", self.telNum]; dispatch_async(dispatch_get_main_queue(), ^{ UIWebView *phoneCallWebView = [[UIWebView alloc] init]; [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]]; [self.view addSubview:phoneCallWebView]; } }
就能夠了。不想用或者用不了GCD的,能夠參考這篇文章spa