从iOS应用中打开Telegram与Bot的聊天界面。

12
我正在尝试从我的应用程序中打开Telegram,以便用户可以与我创建的机器人交谈。到目前为止,它正在工作,但我找到的唯一打开机器人聊天的方法是使用 https://telegram.me/MyBot 网址。但是这样会打开Safari,然后要求用户是否要在Telegram应用程序中打开。最初只有第一次问一次,然后就一直通过safari并自动打开telegram。但它停止了,现在每次都会加载Safari,有时甚至不显示弹出窗口询问用户是否可以打开Telegram应用程序。

有没有办法使用'tg://'URL(应该直接打开Telegram应用程序)打开与机器人的聊天?只看到带电话号码的有效示例。尝试了不同的方式,但完全没有成功...

任何帮助都将是伟大的。

提前致谢!

4个回答

26

Swift 3/4/5+

这正是您所需要的:

let botURL = URL.init(string: "tg://resolve?domain=MyBot")

if UIApplication.shared.canOpenURL(botURL!) {
    UIApplication.shared.openURL(botURL!)
} else {
  // Telegram is not installed.
}

不要忘记将Telegram的URI模式添加到info.plist中:
<key>LSApplicationQueriesSchemes</key>
<array>
   <string>tg</string>
</array>

我遇到了这个问题,使用了您建议的代码,但是当我在 Telegram 中打开链接时,出现了以下错误:对不起,此用户似乎不存在。但是我可以从 Android 应用程序中打开此 URL。 - ava

7

适用于Swift 4.2+和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)
    }
}

2
  1. open info.plist and add LSApplicationQueriesSchemes in this .plist change type from string to array and add key = item0 , value = telegram
  2. 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)
    
    
    
    
    }
    

0

你可以通过链接打开tg

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!)
            }
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接