【问题标题】:How to open twitter page in twitter app from my iphone app?如何从我的 iphone 应用程序中打开 twitter 应用程序中的 twitter 页面?
【发布时间】:2012-02-16 23:36:43
【问题描述】:

我想用 twitter 应用打开的页面:

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

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

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

但是这段代码似乎没有按预期工作,我只启动了 twitter 应用程序而没有我想要显示的页面。

【问题讨论】:

    标签: iphone url twitter


    【解决方案1】:

    您正在寻找以下网址:

    twitter:///user?screen_name=PAGE
    

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

    【讨论】:

    • 此答案的 URL 中显示了三个正斜杠。使用两个对我有用。
    • 对于像https://twitter.com/intent?text=hello 这样的意图,这个网址会是什么样子?
    【解决方案2】:

    我知道它对这个问题的回答很晚,我同意 Murat 的回答是绝对正确的。 只需添加如下检查:

    NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=PAGE]];
    
    if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
            [[UIApplication sharedApplication] openURL:urlApp];
        }
    

    我希望这对某人有所帮助。干杯!! :)

    【讨论】:

      【解决方案3】:

      如果已安装以下代码,则在 twitter 应用上打开 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"]];
      

      不要忘记将“用户名”替换为您的姓名。

      【讨论】:

      • 还记得将 LSApplicationQueriesSchemes 添加到 info.plist。
      【解决方案4】:

      这是 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 Twitter app is not installed, open URL inside Safari
              application.open(webURL as URL)
          }
      

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

      【讨论】:

      • 你能告诉你是怎么找到的吗?有没有官方文档
      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-11
        • 2011-10-17
        • 1970-01-01
        • 2011-11-03
        相关资源
        最近更新 更多