如何检测打开“twitter://”URL 是不是会成功?

Posted

技术标签:

【中文标题】如何检测打开“twitter://”URL 是不是会成功?【英文标题】:How to detect if opening a "twitter://" URL will succeed?如何检测打开“twitter://”URL 是否会成功? 【发布时间】:2012-07-19 17:05:32 【问题描述】:

我想使用 Twitter 应用程序从我的网络应用程序发送推文,而不是在安装 twitter 客户端时使用 twitter 按钮。

如果我在 iPhone 或 Mac 上安装了 Twitter 应用程序,则在将 Web 应用程序重定向到“twitter://”网址时会打开此应用程序。 (见http://handleopenurl.com/scheme/twitter)

但如果我只想在未安装 Twitter 应用程序的地方显示 Twitter 按钮,我该如何检查呢?假设我有这个代码。 twitter_app_installed() 应该如何?有没有办法检查window.location="twitter://..."; 是否不起作用?

<div class="twitter-button"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://google.com" data-text="Hello World">Tweet</a></div>
<div class="twitter-app-button"><a href="twitter://post?message=Hello%20World%20http://google.com">Tweet</a></div>

<script>
function twitter_app_installed()  /* check if window.location="twitter://"; works */
$(document).ready(function() 
  if (twitter_app_installed()) $('.twitter-app-button').show();
  else $('.twitter-button').show();
);
</script>

Plus:是否有正确的方法将 URL 添加到 twitter:// 方案?

【问题讨论】:

相关:***.com/q/627916/559745 【参考方案1】:

似乎无法进行这种类型的检查。

我认为(在 iPhone 上)你能做的最好的事情是:

a) 在首次用户访问时始终显示“下载应用程序”

b) 在按钮单击事件上执行类似的操作:

setTimeout(function ()  
    window.location = "your store url here";    
, 25);
document.cookie="is_app_installed=true"
window.location = "appname://";

只有在未安装应用时才会触发设置的超时

c) 从第二个用户访问,您可以使用cookie来检查应用程序是否已安装。

这个方法有很多不足(如果用户卸载应用程序或不在 iTunes 上安装它会失败),但可能是你唯一能做的。

【讨论】:

以上是关于如何检测打开“twitter://”URL 是不是会成功?的主要内容,如果未能解决你的问题,请参考以下文章

如何从 Android 中的电子邮件 URL 打开已安装的应用程序? [复制]

如何检测链接是不是有效?

如何在本地测试 twitter API?

如何检测 JavaScript 中的哈希后 URL 是不是已更改

如何检测 URL 是不是已粘贴/输入到 UITextView? Swift iOS [关闭]

如何获取特定 Twitter 状态的图像 url - Twitter API