如何从我的iPhone应用程序在Twitter应用程序中打开twitter页面?

时间:2012-01-23 16:09:40

标签: iphone url twitter

我想用twitter app打开的页面:

  

https://twitter.com/#!/PAGE

要打开Twitter应用程序,我使用以下代码:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];

但是这段代码似乎没有按预期工作,我只推出了没有我要展示的页面的推特应用程序。

5 个答案:

答案 0 :(得分:39)

您正在寻找以下网址:

twitter:///user?screen_name=PAGE

请注意,并未在所有设备上安装Twitter。您应该检查openURL方法的结果。如果失败,请使用常规网址在Safari中打开该页面。

答案 1 :(得分:14)

我知道对这个问题的反应很晚,我同意,穆拉特的回答是绝对正确的。 只需添加一个支票,如下所示:

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

if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
        [[UIApplication sharedApplication] openURL:urlApp];
    }

我希望这有助于某人。干杯!! :)

答案 2 :(得分:12)

以下代码在twitter app上打开twitter页面(如果已安装),否则在safari上打开twitter:

NSURL *twitterURL = [NSURL URLWithString:@"twitter://user?screen_name=username"];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL])
    [[UIApplication sharedApplication] openURL:twitterURL];
else
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.twitter.com/username"]];

请勿忘记更换用户名'用你的名字。

答案 3 :(得分:2)

@Alexey:如果您只想知道如何从您的应用程序启动Twitter,请执行以下操作:

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://"]];
   if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
        [[UIApplication sharedApplication] openURL:urlApp];
   }else{
        UIAlertView *appMissingAlertView = [[UIAlertView alloc] initWithTitle:@"Twitter App Not Installed!" message:@"Please install the Twitter App on your iPhone." delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Ok",nil];
        [appMissingAlertView show];
        [appMissingAlertView release];
    }

答案 4 :(得分:2)

这是Swift所需的完整代码。我使用的是Swift 4,但我相信Swift 3也是如此。

let Username =  "YOUR_USERNAME_HERE" 
let appURL = NSURL(string: "twitter:///user?screen_name=\(Username)")!
let webURL = NSURL(string: "https://twitter.com/\(Username)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
      application.open(appURL as URL)
    } else {
        // if Instagram app is not installed, open URL inside Safari
        application.open(webURL as URL)
    }

不要忘记添加使用canOpenURL所需的信息键: Info Keys Needed

相关问题