Phone call number with hashtag on iOS

后端 未结 3 1454
长发绾君心
长发绾君心 2020-12-07 04:58

How do I make a call to this number *199*123456789# on iOS?

I used the following code but it doesn\'t work.

[[UIApplication sharedApplication] openUR         


        
相关标签:
3条回答
  • 2020-12-07 05:51

    You need to use tel:// not just tel:

    0 讨论(0)
  • 2020-12-07 06:01

    Replace * with %2A and # with %23:

    NSURL *tel = [NSURL URLWithString:@"tel:%2A199%2A123456789%23"];
    [[UIApplication sharedApplication] openURL:tel];
    
    0 讨论(0)
  • 2020-12-07 06:04

    Reposted and modified from my answer to the now-closed question "iOS - I want to call phone number "#51234" in Xcode useing telprompt":

    At least as of iOS 11, one can dial numbers with a hashtag (#) or asterisk (*).

    Make calls with these characters by first encoding the phone number, then adding the tel: prefix, and finally turning the resulting string into a URL.

    Swift 4, iOS 11

    // 1) set up the dial sequence as a String
    let dialSequence = "*199*123456789#"
    
    // 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
    guard let encodedDialSequence =
        dialSequence.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) else {
        print("Unable to encode the dial sequence.")
        return
    }
    
    // 3) add the `tel:` url scheme to the front of the encoded string
    // NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
    let dialURLString = "tel:\(encodedDialSequence)"
    
    // 4) set up the URL with the scheme+encoded number string
    guard let dialURL = URL(string: dialURLString) else {
        print("Couldn't convert the dial string into an URL.")
        return
    }
    
    // 5) dial the URL
    UIApplication.shared.open(dialURL, options: [:]) { success in
        if success { print("SUCCESSFULLY OPENED DIAL URL") }
        else { print("COULDN'T OPEN DIAL URL") }
    }
    

    Objective-C, iOS 11

    // 1) set up the dial sequence as a String
    NSString *dialSequence = @"*199*123456789#";
    
    // 2) "percent encode" the dial sequence with the "URL Host Allowed" character set
    NSCharacterSet *urlHostAllowed = [NSCharacterSet URLHostAllowedCharacterSet];
    NSString *encodedDialSequence = [dialSequence stringByAddingPercentEncodingWithAllowedCharacters:urlHostAllowed];
    
    // 3) add the 'tel:' url scheme to the front of the encoded string
    // NOTE: the '//' part is optional: either 'tel:' or 'tel://' will work.
    NSString *dialURLString = [NSString stringWithFormat:@"tel:%@", encodedDialSequence];
    
    // 4) set up the URL with the scheme+encoded number string
    NSURL *dialURL = [NSURL URLWithString:dialURLString];
    
    // 5) set up an empty dictionary for the options parameter
    NSDictionary *optionsDict = [[NSDictionary alloc] init];
    
    // 6) dial the URL
    [[UIApplication sharedApplication] openURL:dialURL
                                       options:optionsDict
                             completionHandler:^(BOOL success) {
                                 if (success) { NSLog(@"SUCCESSFULLY OPENED DIAL URL"); }
                                 else { NSLog(@"COULDN'T OPEN DIAL URL"); }
                             }];
    
    0 讨论(0)
提交回复
热议问题