iOS / Swift - 有没有办法将我们的应用程序链接为WhatsApp消息?

时间:2021-04-11 15:06:22

I have my app running on my iPhone, and I'm able to send some data to my WhatsApp from my App. I went through the following to do the same :

我的应用程序在我的iPhone上运行,我可以从我的应用程序向WhatsApp发送一些数据。我经历了以下相同的事情:

Sending message to WhatsApp from your app using Swift?

使用Swift从您的应用程序向WhatsApp发送消息?

Is there a way we can create a URL that would open our own app directly if it is installed? Could someone suggest any place where I could understand how it could be done?

有没有办法我们可以创建一个URL,如果安装了它,将直接打开我们自己的应用程序?有人可以建议我能理解如何做到的地方吗?

1 个解决方案

#1


1  

Something like this should work:

像这样的东西应该工作:

let originalString = "Some text you want to send"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let url = NSURL(string: "whatsapp://send?text="+encodedString!)

if UIApplication.sharedApplication().canOpenURL(url!) {

    UIApplication.sharedApplication().open(url as URL, options: [:]) { (success) in
                if success {
                    print("WhatsApp accessed successfully")
                } else {
                    print("Error accessing WhatsApp")
                }
            }

} else {

    print("whatsapp not installed")
}

#1


1  

Something like this should work:

像这样的东西应该工作:

let originalString = "Some text you want to send"
let encodedString = originalString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
let url = NSURL(string: "whatsapp://send?text="+encodedString!)

if UIApplication.sharedApplication().canOpenURL(url!) {

    UIApplication.sharedApplication().open(url as URL, options: [:]) { (success) in
                if success {
                    print("WhatsApp accessed successfully")
                } else {
                    print("Error accessing WhatsApp")
                }
            }

} else {

    print("whatsapp not installed")
}