//一行代码调用拨打电话,可兼容iOS10.2以上和以下,防止多次点击,多次弹框
[[NYSystemVM shareClient] callPhone:phone vc:self];
下面是NYSystemVM的实现。
NYSystemVM.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface NYSystemVM : NSObject
+ (NYSystemVM *)shareClient;
@property (nonatomic,assign) BOOL isDisableCall;//用来防止多次点击,多次弹框问题
//拨打电话
- (void)callPhone:(NSString *)phoneNum
vc:(UIViewController *)vc;
@end
NS_ASSUME_NONNULL_END
NYSystemVM.m
#import "NYSystemVM.h"
#define app_main_color [UIColor colorWithRed:28.0f/255.0f green:138.0f/255.0f blue:255.0f/255.0f alpha:1.0f] //app主色调 蓝色
@implementation NYSystemVM
+ (NYSystemVM *)shareClient
{
static NYSystemVM *_shareClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_shareClient = [[NYSystemVM alloc] init];
});
return _shareClient;
}
- (void)callPhone:(NSString *)phoneNum
vc:(UIViewController *)vc
{
if (self.isDisableCall) {
return;
}
if ([NYSystemVM validateCurrentMobileSystem10_2]) {
//iOS10.2以下 拨打电弧 系统主动弹框
self.isDisableCall = YES;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.isDisableCall = NO;
});
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNum]]];
}
else {
//iOS10.2以上 拨打电话 系统不主动弹框
self.isDisableCall = YES;
@weakify(self);
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:phoneNum message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
@strongify(self);
self.isDisableCall = NO;
}];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.isDisableCall = NO;
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phoneNum]]];
}];
[okAction setValue:app_main_color forKey:@"_titleTextColor"];
[cancelAction setValue:app_main_color forKey:@"_titleTextColor"];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
[vc presentViewController:alertController animated:YES completion:nil];
}
}
+(BOOL)validateCurrentMobileSystem10_2
{
NSString *str2 = [[UIDevice currentDevice] systemVersion];
if ([str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedDescending || [str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedSame) {
return YES;
}
return NO;
}
@end