Monday, March 6, 2017

How to make a phone call programmatically in iOS?

I struggled lot to find this solution to make a call from iPhone using Objective C code.
Please use the below code to solve that issue.

Objective C:

NSString *phNo = @"911234567890; //91-countrycode remaining are mobile number.

NSString *phoneNumber = [NSString stringWithFormat:@"tel:%@",phNo];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];

Note: For iPad it won't work.

Swift:

let phoneNo = model.mobileNo
let newString:String = phoneNo.replacingOccurrences(of: " ", with: "")
let phoneUrl = URL(string: "telprompt:+91\(newString)")
if let callURL = phoneUrl {
     self.makeCall(callURL)

}

//makeCall function
func makeCall(_ phoneURL:URL) {
        if UIApplication.shared.canOpenURL(phoneURL) {
            UIApplication.shared.openURL(phoneURL)
        }
        else {
            let alertController = UIAlertController(title: "Warning!", message: "Not possible to make call", preferredStyle: .alert)
            let okAct = UIAlertAction(title: "OK", style: .default, handler: { (act) in
                print("OK")
            })
            alertController.addAction(okAct)
            self.present(alertController, animated: true, completion: nil)
        }
    }

No comments:

Post a Comment