利用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