【问题标题】:iOS: call mobile phone screen with phone number like on AndroidiOS:使用电话号码呼叫手机屏幕,如 Android
【发布时间】:2017-06-27 19:27:09
【问题描述】:

当我们在应用程序中按下电话图标时,是否可以像在 Android 上一样进行操作?

在 Android 上,我们将看到输入电话号码的电话屏幕。
在 iOS 上,它会立即拨打电话号码。

示例:
Android:点击某些应用呼叫按钮 -> 带有预先输入号码的原生 Android 电话呼叫屏幕 -> 手动点击呼叫按钮
iOS: 相同的应用调用按钮点击 -> 立即调用电话号码
我需要知道,是否可以像在 Android 上一样将第二步添加到 iOS 中。

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    是的,可以通过如下方式使用 openUrl,我用 swift 写了你可以在 objc 中尝试同样的操作

    // 迅速

     func makePhoneCall(_ phoneNumber: String?) {
        if phoneNumber != nil && phoneNumber?.length ?? 0 > 0 {
            let charSet: CharacterSet = CharacterSet(charactersIn: "0123456789-+()").inverted
            let cleanedString: String? = (phoneNumber!.components(separatedBy: charSet) as NSArray).componentsJoined(by:"")
            let escapedPhoneNumber: NSString? = cleanedString?.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) as NSString?
            let phoneURLString: String = String(format:"telprompt:%@", escapedPhoneNumber!)
            let phoneURL: URL? = URL(string: phoneURLString)
            if phoneURL != nil {
                if UIApplication.shared.canOpenURL(phoneURL!) == true {
                    UIApplication.shared.openURL(phoneURL!)
                } else {
                    // show alert or something
                }
            }
        }
    }
    

    // 对象

    -(void) makePhoneCall:(NSString *)phoneNumber {
    if(phoneNumber.length > 0) {
        NSCharacterSet *charSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet];
        NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:charSet] componentsJoinedByString:@""];
        NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters:  [NSCharacterSet URLQueryAllowedCharacterSet]];
        NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
        NSURL *url = [NSURL URLWithString:phoneURLString];
        if([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        } else {
            // show alert // or something
        }
    
    }
    }
    

    【讨论】:

    • 此方法将立即拨打电话号码,无需像 Android 上那样的中间屏幕
    • 请注意,telprompt: 方案没有记录在案,因此未来无法证明,因为 Apple 可以随时选择将其删除。
    • 如果你想像android一样,你需要在调用make call函数之前设计一个屏幕
    • 我已经在 iPhone 上检查了 tel:// 和 telprompt://,两者都与简单的警报对话框同样有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多