如果下载,如何将URL重定向到Soundcloud或youtube应用程序

时间:2014-07-27 10:53:01

标签: ios objective-c youtube soundcloud

因此,如果用户已将其下载,则需要访问Twitter应用程序,如果没有,则需要访问safari(浏览器)。我写这段代码:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://user?screen_name=foo"]];

if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
    [[UIApplication sharedApplication] openURL:urlApp];
}else{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.twitter.com/foo"]];
}

如果我想对facebook做同样的事情,我会这样做:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.facebook.com/TragicClothing"]];

但是当我想用Soundcloud(和Youtube)做这件事时会发生什么?

他们两个都有平等twitter://user?screen_name=foo吗?

  

请记住,我想转到Soundcloud(和Youtube)的用户页面(foo

     

我的youtube代码:

NSString *channelName = @"channel";

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

2 个答案:

答案 0 :(得分:0)

这是打开应用程序并检查是否已安装的正确模式:

如果您有tex区域或标签,则必须使用:

NSString *tweetUser;

tweetUser = self.myLabel.text;

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"twitter://user?screen_name=%@", tweeUser]]];

正确的方法是:

- (IBAction)tweet:(id)sender {

    if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"twitter://app"]]){

            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://user?screen_name=<neme here>"]];

    }else{

        [self notInstalledAlert];
    }
}

- (UIAlertView*) notInstalledAlert {

    return [[UIAlertView alloc]
            initWithTitle:@"Twitter Not Installed!"
            message:@"Twitter must be installed on the device in order to open app"
            delegate:nil
            cancelButtonTitle:@"Ok"
            otherButtonTitles:nil];
}

例如youtube有:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"youtube://app"]]){

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"youtube://user/donotrecords"]];

这是soundcloud,但您需要用户ID而不是名称:

if ([[UIApplication sharedApplication] canOpenURL: [NSURL URLWithString:@"soundcloud://app"]]){

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"soundcloud:users:id"]];

答案 1 :(得分:0)

在SoundCloud iOS应用中访问用户页面的URL方案为soundcloud://users/{USER_ID}