因此,当我要转到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/