canOpenURL failing for system-wide URL schemes

后端 未结 7 1929
-上瘾入骨i
-上瘾入骨i 2020-12-16 10:18

I\'m running iOS 9b5.

In my app, if a device can make a phone call, I want to color the text blue so it looks tappable. If not, I leave it black.

In order to

相关标签:
7条回答
  • 2020-12-16 10:50

    What I discovered so far is, that if the console logs -canOpenURL: failed for URL: "xxx://" - error: "(null)", it actually works. As soon as there is any other error than null, it may not work. If the error is "This app is not allowed to query for scheme xxx", then you have to add this scheme to your app's .plist:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>xxx</string>
    </array>
    

    Strange behavior that the console output looks like an error although there is none, indeed.

    0 讨论(0)
  • 2020-12-16 10:53

    try this one:

    NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@", phone_number]]];
    
    0 讨论(0)
  • 2020-12-16 10:57

    In iOS9 I'm using this code and it works:

    NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"AssistanceCallMISDN"];
        assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
        assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    
        NSURL *phoneUrl = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:assistanceNumber]];
        NSURL *phoneFallbackUrl = [NSURL URLWithString:[@"tel://" stringByAppendingString:assistanceNumber]];
    
        if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) {
            [UIApplication.sharedApplication openURL:phoneUrl];
        } else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) {
            [UIApplication.sharedApplication openURL:phoneFallbackUrl];
        } else
        {
            [[[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %@", assistanceNumber] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
            [_viewEmergency setHidden:YES];
        }
    

    My Info.plist

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>telprompt</string>
        <string>tel</string>
    </array>
    
    0 讨论(0)
  • 2020-12-16 11:02

    As iOS9 deprecates stringByAddingPercentEscapesUsingEncoding, the following can be used to clean the telprompt: URL.

    NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
    //NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
    NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
    
    if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
        [[UIApplication sharedApplication] openURL:phoneURL];
    }
    
    0 讨论(0)
  • 2020-12-16 11:03

    I got the same error in IOS9 devices. So I have used below code snippet to avoid this error.

    NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
    NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
    NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
    
    if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
        [[UIApplication sharedApplication] openURL:phoneURL];
    }
    
    0 讨论(0)
  • 2020-12-16 11:12

    Try running this on a real device instead of simulator. No need to add LSApplicationQueriesSchemes for the tel scheme.

    0 讨论(0)
提交回复
热议问题