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

我想使用 Twitter 应用程序打开的页面:

我想使用 Twitter 应用程序打开的页面:

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

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

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

但是这个代码似乎并不像预期的那样工作,我只得到了 Twitter 应用程序没有我想要显示的页面。

40

您正在寻找以下 URL:

twitter:///user?screen_name=PAGE

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

14

以下代码在 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"]];

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

14

我知道它对这个问题的回答相当晚,我同意,Murat 的答案是绝对正确的。

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

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

6

这是 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)
    }

Don't forget to add the Info keys needed to use canOpenURL: Info Keys Needed

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(205)
更改iPhone键盘的颜色(使用 Phonegap/Cordova)
上一篇
WearOS和iOS伴侣应用程序之间的直接通信
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(48条)