最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中实现打电话

iOS crifan 2757浏览 0评论

最少要实现:

调用打电话界面

把电话号码填好

用户去自己打电话

最好要实现:

直接实现拨号

无需用户参与

swift phone call

nsurl – How to use openURL for making a phone call in Swift? – Stack Overflow

How to make phone calls in swift – Stack Overflow

->

https://www.ietf.org/rfc/rfc2806.txt

然后用代码:

            let phoneTelStr = "tel://" + self.curCustomerItem.phone
            gLog.verbose("phoneTelStr=\(phoneTelStr)")
            //phoneTelStr=tel://13800001111
            if let phoneTelNsurl = NSURL(string: phoneTelStr) {
                gLog.verbose("phoneTelNsurl=\(phoneTelNsurl)")
                //phoneTelNsurl=tel://13800001111
                let application:UIApplication = UIApplication.sharedApplication()
                if application.canOpenURL(phoneTelNsurl) {
                    UIApplication.sharedApplication().openURL(phoneTelNsurl)
                } else {
                    //canOpenURL: failed for URL: "tel://13800001111" – error: "This app is not allowed to query for scheme tel"
                    gLog.verbose("application can not open: \(phoneTelNsurl)")
                    self.noticeError("无法给\(self.curCustomerItem.phone)打电话", autoClear: true, autoClearTime: 1)
                }
            }

可以实现:

1.直接调用iPhone的打电话功能,进入打电话,直接拨通电话号码

2.打完电话后,返回之前的app:此处自己的app

然后再去把函数写成通用的:

/***************************************************************************
 * Phone functions
 ***************************************************************************/
func doPhoneCall(curPhone:String) -> Bool {
    var callOk = false
    if curPhone.notEmpty {
        let phoneTelStr = "tel://" + curPhone
        gLog.verbose("phoneTelStr=\(phoneTelStr)")
        //phoneTelStr=tel://13800001111
        if let phoneTelNsurl = NSURL(string: phoneTelStr) {
            gLog.verbose("phoneTelNsurl=\(phoneTelNsurl)")
            //phoneTelNsurl=tel://13800001111
            let application:UIApplication = UIApplication.sharedApplication()
            if application.canOpenURL(phoneTelNsurl) {
                callOk = true
                UIApplication.sharedApplication().openURL(phoneTelNsurl)
            } else {
                //Note:
                //in iOS Simulator will fail:
                //canOpenURL: failed for URL: "tel://13800001111" – error: "This app is not allowed to query for scheme tel"
                print("application can not open: \(phoneTelNsurl)")
            }
        }
    } else {
        print("can not call for empty phone")
    }
    return callOk
}

调用:

    func phoneCallCustomer(curButton:UIButton) {
        gLog.debug("curButton=\(curButton)")
        if self.curCustomerItem.phone.notEmpty {
            if doPhoneCall(self.curCustomerItem.phone) {
                gLog.verbose("call phone ok")
            } else {
                self.noticeError("无法给\(self.curCustomerItem.phone)打电话", autoClear: true, autoClearTime: 1)
            }
        }
    }

转载请注明:在路上 » [已解决]swift中实现打电话

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.174 seconds, using 22.08MB memory