ios 通过 URL 方案共享链接(例如通过 Telegram)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31356360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:49:32  来源:igfitidea点击:

Share a link via URL scheme (via Telegram for example)

iosswifturl-scheme

提问by farhad1985

I want to share a link via URL scheme, lets say, for Telegram.

我想通过 URL 方案共享一个链接,比如 Telegram。

I have created this:

我创建了这个:

tg://msg?text = www.example.com?t=12

tg://msg?text = www.example.com?t=12

The link, opens telegram but nothing else happens.

该链接打开电报,但没有其他任何反应。

I have used the same code for Viber, and it works:

我对 Viber 使用了相同的代码,它可以工作:

viber://forward?text = www.example.com?t=12

viber://forward?text = www.example.com?t=12

And it opens a new message in Viber with this text:

它会在 Viber 中打开一条带有以下文本的新消息:

www.example.com

www.example.com

In the other words, it cuts my URL.

换句话说,它削减了我的 URL。

Any ideas?

有任何想法吗?

回答by Mohebifar

You can also use telegram.me share link which falls back to webogram if a telegram app is not installed on the device.

如果设备上未安装电报应用程序,您还可以使用 telegram.me 共享链接回退到网络图。

https://telegram.me/share/url?url=<URL>&text=<TEXT>

https://telegram.me/share/url?url=<URL>&text=<TEXT>

回答by Mariam

This works with me:

这对我有用:

tg://msg?text=Mi_mensaje&to=+1555999

回答by Sivabalaa Jothibose

For Telegram share:

对于电报分享:

Objective C:

目标 C:

if([UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tg://msg?text=test"]){
 [UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tg://msg?text=test"]
}else{
 //App not installed.
}

Swift 3.0:

斯威夫特 3.0:

let urlString = "tg://msg?text=test"
let tgUrl = URL.init(string:urlString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
if UIApplication.shared.canOpenURL(tgUrl!)
    {
        UIApplication.shared.openURL(tgUrl!)
    }else
    {
       //App not installed.
    }

If you have used canOpenURL, then need to add in info.plist

如果你使用过canOpenURL,那么需要在info.plist中加入

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>tg</string>
</array>

回答by Francis Rodrigues

You can use the link telegram.mewhich will provide a preview page with an alert requesting to open the link in the application.

您可以使用该链接telegram.me,该链接将提供一个带有警报的预览页面,请求在应用程序中打开该链接。

https://telegram.me/share/url?url=<URL>&text=<TEXT>

enter image description here

在此处输入图片说明

The second option is calling the application link directly:

第二个选项是直接调用应用程序链接:

tg://msg_url?url=<url>&text=<encoded-text>

I particularly prefer the second option, which also works on desktop applications.

我特别喜欢第二个选项,它也适用于桌面应用程序。

回答by HoldOffHunger

You have the following options for a URL...

您有以下 URL 选项...

https://t.me/share/url?url={url}&text={text}
https://telegram.me/share/url?url={url}&text={text}
tg://msg_url?url={url}&text={text}

In case you want to confirm, here is the official API source: Core.Telegram.org: Widgets -> Sharing Button.

如果您想确认,这里是官方 API 来源:Core.Telegram.org: Widgets -> Sharing Button

If you are interested in watching a project that keeps track of these URLs, then check us out!: https://github.com/bradvin/social-share-urls#telegramme

如果您有兴趣观看跟踪这些 URL 的项目,请查看我们!:https: //github.com/bradvin/social-share-urls#telegramme

Social Share URLs

社交分享 URL

回答by HoldOffHunger

PHP

PHP

<a href="tg://msg?text=<?php echo rawurlencode($gotoURL); ?>">Link</a>


JavaScript


JavaScript

<script>TEXT="any text or url";</script>

<a onclick="window.location='tg://msg?text='+encodeURIComponent(TEXT);">Link</a>

回答by f0xik

To check if the Telegram is installed you can do the following (borrowed from the Whatsapp sharer module of ShareKit):

要检查是否安装了 Telegram,您可以执行以下操作(从 ShareKit 的 Whatsapp 共享模块借用):

BOOL isTelegramInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tg://msg?text=test"]];

iOS checks if there's any app installed which can handle the tg:// scheme, which is Telegram.

iOS 检查是否安装了任何可以处理 tg:// 方案的应用程序,即 Telegram。

回答by Vito Valov

Just tested, this way it works both opening telegram app or browser in case it's not installed:

刚刚测试过,这样它就可以在未安装的情况下打开电报应用程序或浏览器:

let webURL = NSURL(string: "https://t.me/<YOUR ID>")!
UIApplication.shared.open(webURL as URL)

回答by Andrejs Mivre?iks

Try using tg://share:

尝试使用tg://share

<a href="tg://share?url=www.example.com?t=12&text=Check out this url">Link</a>