[英]How to open twitter page in twitter app from my iphone app?
我想用twitter app打开的页面:
要打开Twitter应用程序,我使用以下代码:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];
但是这段代码似乎没有按预期工作,我只推出了没有我要显示的页面的推特应用程序。
您正在寻找以下网址:
twitter:///user?screen_name=PAGE
请注意,并未在所有设备上安装Twitter。 你应该检查openURL
方法的结果。 如果失败,请使用常规网址在Safari中打开该页面。
我知道它对这个问题的反应非常晚,我同意,穆拉特的答案是绝对正确的。 只需添加支票如下:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:@"%@", @"twitter:///user?screen_name=PAGE]];
if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
[[UIApplication sharedApplication] openURL:urlApp];
}
我希望这可以帮助别人。 干杯!! :)
如果已经安装了以下代码在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"]];
不要忘记用您的名字替换'username'。
@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];
}
这是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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.