我想使用 Twitter 应用程序打开的页面:
https://twitter.com/#!/PAGE要打开 Twitter 应用程序,我使用以下代码:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];
但是这个代码似乎并不像预期的那样工作,我只得到了 Twitter 应用程序没有我想要显示的页面。
您正在寻找以下 URL:
twitter:///user?screen_name=PAGE
请注意,Twitter 并非安装在所有设备上。您应该检查openURL
方法的结果。如果失败,请使用常规 url 在 Safari 中打开页面。
以下代码在 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"]];
不要忘记将“用户名”替换为您的姓名。
我知道它对这个问题的回答相当晚,我同意,Murat 的答案是绝对正确的。
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=PAGE]];
if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
[[UIApplication sharedApplication] openURL:urlApp];
}
我希望这对某人有所帮助。干杯!:)
这是 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)
}
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(48条)