2017-07-14 95 views
1

如何斯威夫特發送消息,並鏈接到WhatsApp的3如何發送消息並鏈接到Swift 3中的WhatsApp?

我使用這個代碼: Code

消息errore到控制檯:

...failed for URL: "whatsapp://send?text=Check" - error: "This app is not allowed to query for scheme whatsapp"

謝謝

+1

[使用雨燕從您的應用程序發送消息的WhatsApp?](HTTPS的可能重複: //stackoverflow.com/questions/32042702/sending-message-to-whatsapp-from-your-app-using-swift) –

回答

4

你應該試試這個:

注意:您必須將WhatsApp應用程序安裝到您的設備中。

斯威夫特3

var documentInteractionController: UIDocumentInteractionController = UIDocumentInteractionController() 

@IBAction func whatsappShareText(_ sender: AnyObject) 
{ 
    let originalString = "First Whatsapp Share" 
    let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) 

    let url = URL(string: "whatsapp://send?text=\(escapedString!)") 

    if UIApplication.shared.canOpenURL(url! as URL) 
    { 
     UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil) 
    } 
} 

@IBAction func whatsappShareLink(_ sender: AnyObject) 
{ 
    let originalString = "https://www.google.co.in" 
    let escapedString = originalString.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) 
    let url = URL(string: "whatsapp://send?text=\(escapedString!)") 

    if UIApplication.shared.canOpenURL(url! as URL) 
    { 
     UIApplication.shared.open(url! as URL, options: [:], completionHandler: nil) 
    } 
} 

在您的應用程序 「的info.plist」 添加該代碼

<key>LSApplicationQueriesSchemes</key> 
<array> 
    <string>whatsapp</string> 
</array> 
相關問題