iOS10.2以上和以下打电话,防止多次点击实现

本文介绍了一种兼容iOS10.2及以上版本的拨打电话方法,通过一行代码实现,并防止了因多次点击导致的多次弹框问题。在iOS10.2以下系统,直接使用URL Scheme进行拨打;在iOS10.2及以上系统,通过UIAlertController确认后进行拨打,确保用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//一行代码调用拨打电话,可兼容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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值