ios - 如何将网址重定向到Soundcloud或youtube应用(如果已下载)

标签 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吗?

Please bare in mind that I want to go to a user page (foo) of Soundcloud (and Youtube)




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

最佳答案

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

如果您有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"]];

关于ios - 如何将网址重定向到Soundcloud或youtube应用(如果已下载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24980201/

相关文章:

iphone - 向 UITableView 子类添加属性

api - YouTube Data API v3 调用返回的最大结果是多少

iPhone 和 iPad 应用程序(同名 - 两个项目)...?

ios - 移动文件并覆盖

ios - JSON 解析 - 没有 URL session 任务恢复的替代方法是什么,因为它不是主线程的一部分?

objective-c - 单击行中任意位置展开 NSOutlineView 项目

ios - ECSlidingViewController 禁用滑动以在 UITableView 中删除

ios - 使用 Codable、swift 4 解析 JSON

jquery - 显示具有显示/隐藏DIV层的多个YouTube视频

youtube - 今天要获得Freebase主题吗?