如何在Swift中点击按钮打开Whatsapp或其他iOS应用程序?
问题描述:
我想从app_A打开一个iOS app_B,然后想要将app_B的数据放回到app_A中,在下面尝试打开whatsapp,但没有奏效。请帮助如何在iOS中实现这一点,在此先感谢。如何在Swift中点击按钮打开Whatsapp或其他iOS应用程序?
@IBAction func clickMe(_ sender: UIButton) {
let url = "https://api.whatsapp.com/send?00919599****** "
let whatUpUrl = NSURL(string: url)
if UIApplication.shared.canOpenURL(whatUpUrl! as URL){
UIApplication.shared.openURL(whatUpUrl! as URL)
} else {
//redirect to safari because the user doesn't have Whatsapp installed
UIApplication.shared.openURL(NSURL(string: "http://whatsapp.com/")! as URL)
}
}
答
WEBURL链接打开聊天
let whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9512347895&text=Invitation")
if UIApplication.shared.canOpenURL(whatsappURL!) {
UIApplication.shared.openURL(whatsappURL!)
}
注:在info.plist中
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
</array>
答
添加URL方案添加这Info.plist中
然后在你想打开whatsapp的按钮上使用这个。
@IBAction func whatsappButtonPressed(_ sender: Any) {
var str = "Hello to whatsapp"
str = str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))!
let whatsappURL = NSURL(string: "whatsapp://send?text=\(str)")
if UIApplication.shared.canOpenURL(whatsappURL! as URL) {
UIApplication.shared.openURL(whatsappURL! as URL)
} else {
showAlert(message: "Whatsapp is not installed on this device. Please install Whatsapp and try again.")
}
}
同样,对于其他应用: 替换 - NSURL(字符串: “WhatsApp的://发送文本=(STR)?”)与 NSURL(字符串: “APPNAME://”)。 您还需要将该应用程序添加到Info.plist,如上所述。
谢谢!
@Sudhir&Ankit,谢谢,伙计们,有怎么了打开的,但我也需要从第二个应用程序回拨我的第一个应用程序,你可以请建议 – equanimousFly
请你详细说明一下吗? –
@ Ankit:我想要app_B的callBack响应数据不需要它是wats up。例如当我从app_A打开app_B时,app_B执行一些任务,然后将响应返回给app_A。 – equanimousFly