从iOS应用程序打开电报聊天(使用Bot)

时间:2016-11-14 18:25:07

标签: ios swift3 telegram telegram-bot

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

有没有办法使用它' tg://' url(应该直接打开Telegram应用程序)打开与bot的聊天?只看到电话号码的工作示例。以不同的方式尝试但完全没有成功......

任何帮助都会很棒。

提前致谢!

4 个答案:

答案 0 :(得分:12)

Swift 3.0.1

这正是您所寻找的:

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

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

不要忘记将电报的URI架构添加到info.plist:

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

答案 1 :(得分:1)

  1. 打开info.plist并在此.plist中添加LSApplicationQueriesSchemes,将类型从字符串更改为数组,并添加键= item0,值=电报
  2. 为开放电报编写此代码

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

答案 2 :(得分:1)

对于Swift 4.2和iOS 9 +

let screenName =  "und3rflow"
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 the 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)
    }
}

答案 3 :(得分: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!)
            }
        }
    }
相关问题