web-dev-qa-db-fra.com

canOpenURL en échec pour les schémas d'URL à l'échelle du système

J'utilise iOS 9b5.

Dans mon application, si un appareil peut émettre un appel téléphonique, je souhaite colorer le texte en bleu pour le rendre lisible. Sinon, je le laisse noir.

Afin de déterminer les capacités du périphérique, j'utilise:

[[UIApplcation sharedApplication] canOpenURL:@"telprompt://5555555555"]  

Comme nous le savons tous, iOS 9 requiert la création de liste blanche de tous les modèles d'URL que nous utiliserons dans notre application comme mesure de confidentialité.

J'ai ceci dans mon Info.plist:

<key>LSApplicationQueriesSchemes</key>  
<array>  
  <string>telprompt</string>  
</array>  

Quoi que je fasse, j'obtiens toujours canOpenURL: failed pour l'URL: "telprompt: //" - error: "(null)". J'ai essayé tel: // et sms: // et je n'arrive pas à éviter cet avertissement syslog.

Est-ce que quelqu'un connaît un moyen de détecter si un appareil peut émettre un appel téléphonique sans déclencher ces avertissements?

25
djibouti33

Ce que j’ai découvert jusqu’à présent, c’est que si la console enregistre -canOpenURL: failed for URL: "xxx://" - error: "(null)", cela fonctionne réellement. Dès qu'il y a une erreur autre que null, cela peut ne pas fonctionner. Si l'erreur est "This app is not allowed to query for scheme xxx", vous devez ajouter ce schéma à la liste .plist de votre application:

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

Comportement étrange que la sortie de la console ressemble à une erreur bien qu’il n’y en ait pas.

9
endowzoner

Je pense que vous devrez peut-être essayer ceci sur un appareil réel ou simplement l'essayer à nouveau. Je viens de recevoir ce travail sur mon iPhone 5, il semble que vous n’ayez même pas besoin de l’ajouter aux LSApplicationQueriesSchemes. Si l'application est construite avec Xcode 7 Beta 6 et que vous utilisez canOpenURL ou openURL comme ci-dessous, elle semble fonctionner parfaitement sur votre appareil. 

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:555-555-5555"]]

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]]

Sur le sim iOS, j'obtiens toujours l'erreur:
LaunchServices: ERROR: Il n'y a pas de gestionnaire enregistré pour le schéma d'URL tel
- canOpenURL: échec de l'URL: "tel: 555-555-5555" - erreur: "Cette application n'est pas autorisée à interroger ce système"

7
Polar Bear

J'ai la même erreur dans les appareils IOS9. J'ai donc utilisé l'extrait de code ci-dessous pour éviter cette erreur.

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];
}
5
Anooj VM

Dans iOS9, j'utilise ce code et cela fonctionne: 

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];
    }

Mon info.pliste

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>telprompt</string>
    <string>tel</string>
</array>
2
Rincha

Comme iOS9 désapprouve stringByAddingPercentEscapesUsingEncoding, vous pouvez utiliser les éléments suivants pour nettoyer le télprompt: 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];
}
2
Steve Forbes

essaye celui-là:

NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@", phone_number]]];
0
Muhammad Jabbar

Essayez d'exécuter ceci sur un appareil réel au lieu d'un simulateur. Inutile d'ajouter LSApplicationQueriesSchemes pour le schéma tel.

0
maxkonovalov