按钮的代码就不写了。直接写主要代码。
<key>LSapplicationQueriesSchemes</key><array><string>tel</string><string>telPRompt</string></array>
plist里加上述代码就是意思申请打电话权限
-(void)callPhoneStr:(NSString*)phoneStr {
NSString *str2 = [[UIDevicecurrentDevice] systemVersion];
if ([str2compare:@"10.2"options:NSNumericSearch] ==NSOrderedDescending || [str2compare:@"10.2"options:NSNumericSearch] ==NSOrderedSame)
{
NSLog(@">=10.2");
NSString* PhoneStr = [NSStringstringWithFormat:@"tel://%@",phoneStr];
if ([PhoneStrhasprefix:@"sms:"] || [PhoneStrhasPrefix:@"tel:"]) {
UIApplication * app = [UIApplicationsharedApplication];
if ([appcanOpenURL:[NSURLURLWithString:PhoneStr]]) {
[app openURL:[NSURLURLWithString:PhoneStr ]options:@{}completionHandler:nil ];
}
}
}else {
NSMutableString* str1 = [[NSMutableStringalloc]initWithString:phoneStr];//存在堆区,可变字符串
NSString * str = [NSStringstringWithFormat:@"是否拨打电话/n%@",str1];
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:strmessage: nilpreferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertActionactionWithTitle:@"呼叫"style:UIAlertActionStyleDestructivehandler:^(UIAlertAction *action) {
NSLog(@"点击了呼叫按钮10.2下");
NSString* PhoneStr = [NSStringstringWithFormat:@"tel://%@",phoneStr];
if ([PhoneStrhasPrefix:@"sms:"] || [PhoneStrhasPrefix:@"tel:"]) {
UIApplication * app = [UIApplicationsharedApplication];
if ([appcanOpenURL:[NSURLURLWithString:PhoneStr]]) {
[app UIApplication:[NSURLURLWithString:PhoneStr]];
}
}
}]];
[alert addAction:[UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction *action) {
NSLog(@"点击了取消按钮");
}]];
[selfpresentViewController:alertanimated:YEScompletion:nil];
}
}
一定要区分一下ios系统版本,因为api是不一样的,因为版本不一样,方法是不一样的。10.2那个里面的方法自己可以弹出一个弹出框,另外一个需要用UIAlertController来制作弹出框。
这里推荐一个
http://www.cocoachina.com/ios/20161021/17824.html
新闻热点
疑难解答