iOS 拨打电话优化

 

最近在测试项目发现项目中调用电话的在iOS 10上会出现卡顿,点击调用会有一小段延,现在是这样实现拨打电话的:

NSMutableString *str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_phoneNum];
        UIWebView *callWebview = [[UIWebView alloc] init];
        [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
        [self.view addSubview:callWebview];

 

优化如下:

+ (void)callPhoneStr:(NSString*)phoneStr  withVC:(UIViewController *)selfvc{
    NSString *str2 = [[UIDevice currentDevice] systemVersion];
    
    if ([str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedDescending || [str2 compare:@"10.2" options:NSNumericSearch] == NSOrderedSame)
    {
        NSLog(@">=10.2");
        NSString* PhoneStr = [NSString stringWithFormat:@"tel://%@",phoneStr];
        if ([PhoneStr hasPrefix:@"sms:"] || [PhoneStr hasPrefix:@"tel:"]) {
            UIApplication * app = [UIApplication sharedApplication];
            if ([app canOpenURL:[NSURL URLWithString:PhoneStr]]) {
                [app openURL:[NSURL URLWithString:PhoneStr]];
            }
        }
    }else {
         NSMutableString* str1 = [[NSMutableString alloc]initWithString:phoneStr];// 存在堆区,可变字符串
        if (phoneStr.length == 10) {
            [str1 insertString:@"-"atIndex:3];// 把一个字符串插入另一个字符串中的某一个位置
            [str1 insertString:@"-"atIndex:7];// 把一个字符串插入另一个字符串中的某一个位置
        }else {
            [str1 insertString:@"-"atIndex:3];// 把一个字符串插入另一个字符串中的某一个位置
            [str1 insertString:@"-"atIndex:8];// 把一个字符串插入另一个字符串中的某一个位置
        }
        NSString * str = [NSString stringWithFormat:@"是否拨打电话\n%@",str1];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:str message: nil preferredStyle:UIAlertControllerStyleAlert];
        // 设置popover指向的item
        alert.popoverPresentationController.barButtonItem = selfvc.navigationItem.leftBarButtonItem;
        // 添加按钮
        [alert addAction:[UIAlertAction actionWithTitle:@"呼叫" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSLog(@"点击了呼叫按钮10.2下");
            NSString* PhoneStr = [NSString stringWithFormat:@"tel://%@",phoneStr];
            if ([PhoneStr hasPrefix:@"sms:"] || [PhoneStr hasPrefix:@"tel:"]) {
                UIApplication * app = [UIApplication sharedApplication];
                if ([app canOpenURL:[NSURL URLWithString:PhoneStr]]) {
                    [app openURL:[NSURL URLWithString:PhoneStr]];
                }
            }
        }]];
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"点击了取消按钮");
        }]];
        [selfvc presentViewController:alert animated:YES completion:nil];
    }
    
}

 

转载于:https://www.cnblogs.com/LynnAIQ/p/6392496.html

### 如何在 UniApp 中于 iOS 平台实现拨打电话的功能 #### 功能概述 为了实现在iOS平台上点击按钮即可拨打特定电话号码,在UniApp中可以采用`plus.device.dial()`方法来完成此操作。对于iOS平台而言,通常情况下不会直接发起呼叫而是弹出确认对话框让用户决定是否要进行通话[^2]。 #### 示例代码 下面是一段用于展示如何创建一个能够触发拨号行为的按钮组件: ```html <template> <view class="container"> <!-- 定义了一个可点击区域 --> <button type="primary" @click="makePhoneCall">联系客服</button> </view> </template> <script> export default { methods: { makePhoneCall() { const phoneNumber = "123456789"; // 设置目标电话号码 if (/(iPhone|iPad|iPod)/i.test(navigator.userAgent)) { // 判断当前环境是否为iOS设备 plus.device.dial(phoneNumber, false); // 对于iOS,第二个参数设为false表示只显示拨号盘而不自动拨打 } else { window.location.href = `tel:${phoneNumber}`; // 非iOS环境下尝试其他方式处理 } } } } </script> ``` #### 注意事项 - **权限声明**:确保应用程序已经获得了必要的权限以便访问用户的通讯录或执行拨号动作。 - **用户体验考量**:考虑到安全性和隐私保护的原因,建议开发者始终给予用户最终的选择权而不是强制性地启动呼叫过程。 - **跨平台兼容性测试**:尽管上述逻辑适用于大多数情况下的iOS终端,但在实际项目部署前仍需针对具体机型做充分的适配与优化工作以保证最佳体验效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值