Make a phone call from your mobile app

Make a phone call from your mobile app

This tutorial will cover how to make a phone call directly from your mobile app by deep linking to the native phone app. It will also contain a validation for removing special characters because this approach allows only numbers to be entered.

Swift

    let stringArray = phoneString.componentsSeparatedByCharactersInSet(
        NSCharacterSet.decimalDigitCharacterSet().invertedSet)  
        let phoneNumber = stringArray.joinWithSeparator("")
            if let url = NSURL(string: "tel://\(phoneNumber)") {
                UIApplication.sharedApplication().openURL(url)
            }

Objective – C

  NSString *phNo = [[phoneString componentsSeparatedByCharactersInSet:
                [[NSCharacterSet decimalDigitCharacterSet] invertedSet]] 
            	componentsJoinedByString:@""];
NSURL *phoneUrl = [NSURL URLWithString:[NSString  stringWithFormat:@"tel:%@",phNo]];
    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl]) {
        [[UIApplication sharedApplication] openURL:phoneUrl];
    }

If you want to prompt the user before making the call using the following code telprompt:

Leave a Reply

Your email address will not be published. Required fields are marked *