2013-08-26 3 views
3

Pour lancer la vidéo sur l'application youtube, j'utilise le code ci-dessous.lancer la chaîne youtube dans l'application youtube

NSURL *instagramURL = [NSURL URLWithString:@"youtube://foo"]; 
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) { 
    NSLog(@"opening youtube app..."); 
    NSString *stringURL = @"http://www.youtube.com/watch?v=H9VdapQyWfg"; 
    NSURL *url = [NSURL URLWithString:stringURL]; 
    [[UIApplication sharedApplication] openURL:url]; 
} else { 
    // open in UIWebView in WebViewViewController 
    WebViewViewController *secondView = [self.storyboard instantiateViewControllerWithIdentifier:@"webinterface"]; 

    secondView.headerLabel = @"YouTube"; 
    secondView.webPath = @"http://www.youtube.com/watch?v=H9VdapQyWfg"; 

    [self.navigationController pushViewController:secondView animated:YES]; 
} 

client changé l'esprit et de demander à la chaîne mise en application iPhone.

Pour les tests, j'ai utilisé lien http://www.youtube.com/user/richarddawkinsdotnet

mais quand j'utilise ce lien, au lieu de l'application YouTube, il ouvre toujours SAFARI. :(

Toute idée/suggestion sur comment puis-je ouvrir le canal dans l'application YouTube avec lien fourni?

Répondre

12

Vous êtes le code va mal parce que, bien que vous vérifiez si vous pouvez ouvrir l'URL de youTube plus ou moins correctement, vous ouvrez juste une adresse Web, qui s'ouvrira toujours dans Safari

Ceci est le code que je viens d'utiliser, qui fonctionne pour moi. l'instruction else si vous voulez revenir à l'utilisation d'un webviewcontroller car cela va ouvrir Safari si le y L'application Outube n'est pas installée.

NSString *channelName = @"TheNameOfTheChannel"; 

NSURL *linkToAppURL = [NSURL URLWithString:[NSString stringWithFormat:@"youtube://user/%@",channelName]]; 
NSURL *linkToWebURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/user/%@",channelName]]; 

if ([[UIApplication sharedApplication] canOpenURL:linkToAppURL]) { 
    // Can open the youtube app URL so launch the youTube app with this URL 
    [[UIApplication sharedApplication] openURL:linkToAppURL]; 
} 
else{ 
    // Can't open the youtube app URL so launch Safari instead 
    [[UIApplication sharedApplication] openURL:linkToWebURL]; 
} 
+0

Je vais vérifier cela et revenir à vous ... Je ne trouve cette réponse .. Hope this fonctionne ...: D: P –

+0

Cela ne fonctionne pas pour moi! Il va juste à la page vidéo de l'application et dit "erreur de lecture appuyez sur pour réessayer" Mais fonctionne très bien lors de l'ouverture en safari. – maxisme

+0

Si vous voulez ouvrir une URL dans une application native, vous devez utiliser comme ceci: NSURL * linkToAppURL = @ "youtube: //www.youtube.com/user/%@", channelName]; ..... le schéma suivant NE FONCTIONNERA PAS NSURL * linkToAppURL = @ "youtube: // user /% @", channelName]; –

1

Même réponse, mais plus courte:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"youtube://user/%channelName%"]]) { 
    NSURL *webURL = [NSURL URLWithString:@"http://www.youtube.com/user/%channelName%"]; 
    [[UIApplication sharedApplication] openURL: webURL]; 
} 

et twitter:

if (![[UIApplication sharedApplication] openURL: [NSURL URLWithString: @"twitter://user?screen_name=%channelName%"]]) { 
    NSURL *webURL = [NSURL URLWithString:@"http://twitter.com/%channelName%"]; 
    [[UIApplication sharedApplication] openURL: webURL]; 
} 

Et voici une liste plus exhaustive des applications:

http://wiki.akosma.com/IPhone_URL_Schemes

0

youtube://user/<channel-id> a cessé de travailler dans iOS 9 si je recherche et a trouvé ce travail bien maintenant dans iOS 9

youtube://www.youtube.com/channel/<channel-id>

Questions connexes