Reputation: 255
I'm trying to open Telegram from my app, for the users to talk with a bot I made. So far, it is working but the only way I found to get the bot chat opened was using the https://telegram.me/MyBot url. But this way, it opens Safari, and then the user is asked if he wants to open it on the Telegram app. Initially it was asking one time, and then, after the first time, it was just passing thru safari and opening Telegram automatically. But it stopped and now, every single time it loads Safari and some times, it even doesn't shows the popup asking the user if it can open the Telegram app.
Is there any way to use that 'tg://' url (that should open directly the Telegram app) to open a chat with a bot? Only saw working examples with phone numbers. Tried in different ways but no success at all...
Any help would be great.
Thanks in advance!
Upvotes: 13
Views: 11811
Reputation: 2799
For Swift 4.2+ and iOS 9+
let screenName = "und3rflow" // <<< ONLY CHANGE THIS ID
let appURL = NSURL(string: "tg://resolve?domain=\(screenName)")!
let webURL = NSURL(string: "https://t.me/\(screenName)")!
if UIApplication.shared.canOpenURL(appURL as URL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil)
}
else {
UIApplication.shared.openURL(appURL as URL)
}
}
else {
//redirect to safari because user doesn't have Telegram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil)
}
else {
UIApplication.shared.openURL(webURL as URL)
}
}
Upvotes: 7
Reputation: 988
Swift 3/4/5+
This is exactly what you looking for:
let botURL = URL.init(string: "tg://resolve?domain=MyBot")
if UIApplication.shared.canOpenURL(botURL!) {
UIApplication.shared.openURL(botURL!)
} else {
// Telegram is not installed.
}
Don't forget to add URI schema's of Telegram to info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>tg</string>
</array>
Upvotes: 27
Reputation: 789
You can open tg through the link
let botURL = URL.init(string: "https://t.me/\("bot_or_user_name")")
if UIApplication.shared.canOpenURL(botURL!) {
UIApplication.shared.openURL(botURL!)
} else {
let urlAppStore = URL(string: "itms-apps://itunes.apple.com/app/id686449807")
if(UIApplication.shared.canOpenURL(urlAppStore!))
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(urlAppStore!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(urlAppStore!)
}
}
}
Upvotes: 0
Reputation: 21
write this code for open telegram
let url = URL(string: "instagram://user?username=fastteb")
if(UIApplication.shared.canOpenURL(url!))
{
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
}else
{
let alert = UIAlertController(title: "Error", message: "you don't have instagram,you need to install instagram", preferredStyle: .alert)
let action = UIAlertAction(title: "Download And Install", style: .default, handler: { (UIAlertAction) in
let urlAppStore = URL(string: "itms-apps://itunes.apple.com/app/id389801252")
if(UIApplication.shared.canOpenURL(urlAppStore!))
{
UIApplication.shared.open(urlAppStore!, options: [:], completionHandler: nil)
}
})
let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alert.addAction(action)
alert.addAction(actionCancel)
self.present(alert, animated: true, completion: nil)
}
Upvotes: 2