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

Ale*_*xey 21 iphone twitter url

我想用twitter app打开的页面:

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

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

NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];
Run Code Online (Sandbox Code Playgroud)

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

mur*_*rat 39

您正在寻找以下网址:

twitter:///user?screen_name=PAGE
Run Code Online (Sandbox Code Playgroud)

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

  • 此答案的URL中显示了三个正斜杠.用两个为我工作. (6认同)

App*_*304 14

我知道它对这个问题的反应非常晚,我同意,穆拉特的答案是绝对正确的.只需添加支票如下:

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

if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
        [[UIApplication sharedApplication] openURL:urlApp];
    }
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人.干杯!!:)


Bas*_*awy 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"]];
Run Code Online (Sandbox Code Playgroud)

不要忘记用您的名字替换'username'.

  • 还记得将LSApplicationQueriesSchemes添加到info.plist。 (2认同)

小智 5

这是 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)
    }
Run Code Online (Sandbox Code Playgroud)

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

  • 你能告诉我你是如何找到它的吗?有没有官方文档 (2认同)