Sending message to WhatsApp from your app using Swift?

前端 未结 6 1284

For one of my app, I wanted to share data to WhatsApp contacts. I tried few solutions overs the StackOverflow but couldn\'t get exact solution. After some trials could achie

相关标签:
6条回答
  • 2020-12-13 21:06

    Addition to above solutions, starting from iOS 9, we need to add whatsapp to LSApplicationQueriesSchemes key in info.plist. After this only it worked for me.

    0 讨论(0)
  • 2020-12-13 21:08

    Swift 5

    Code

    let urlWhats = "whatsapp://send?text=\("Hello World")"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
          if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                     UIApplication.shared.open(whatsappURL as URL)
                 } 
                 else {
                     print("please install watsapp")
                 }
          }
    }
    
    0 讨论(0)
  • 2020-12-13 21:13

    Swift 5

    Please follow the below steps for sharing on WhatsApp through URL Schemes

    1. Add this code in your app "info.plist"

    <key>LSApplicationQueriesSchemes</key>
    <array>
      <string>whatsapp</string>
    </array>

    1. For sharing text and URL

    Code

        @IBAction func whatsappShareText(_ sender: AnyObject) {
    
        
            let message = "First Whatsapp Share & https://www.google.co.in"
            var queryCharSet = NSCharacterSet.urlQueryAllowed
            
            // if your text message contains special characters like **+ and &** then add this line
            queryCharSet.remove(charactersIn: "+&")
            
            if let escapedString = message.addingPercentEncoding(withAllowedCharacters: queryCharSet) {
                if let whatsappURL = URL(string: "whatsapp://send?text=\(escapedString)") {
                    if UIApplication.shared.canOpenURL(whatsappURL) {
                        UIApplication.shared.open(whatsappURL, options: [: ], completionHandler: nil)
                    } else {
                        debugPrint("please install WhatsApp")
                    }
                }
            }
        }
    

    Happy Coding!

    0 讨论(0)
  • 2020-12-13 21:21

    Swift 3.0

    Try with this code for access watsapp in your application. Its working perfectly for me.

    @IBAction func sendButtonAction(_ sender: Any)
    {
        let date = Date()
        let msg = "Hi my dear friends\(date)"
        let urlWhats = "whatsapp://send?text=\(msg)"
    
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                    UIApplication.shared.openURL(whatsappURL as URL)
                } else {
                    print("please install watsapp")
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-13 21:21

    My code is Looking Like this

    let encodeQuizStr = "Check Out The Quiz With link \n http://www.proprofs.com "
    
            let urlQuizStringEncoded = encodeQuizStr.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
    
            guard let whatsAppUrl = NSURL(string: "whatsapp://send?text="+urlQuizStringEncoded!) else { return }
    
            if UIApplication.shared.canOpenURL(whatsAppUrl as URL) {
    
                if #available(iOS 10.0, *) {
                    print(urlQuizStringEncoded!)
                    UIApplication.shared.open(whatsAppUrl as URL, options: [:], completionHandler: nil)
                } else {
    
                    UIApplication.shared.openURL(whatsAppUrl as URL)
    
                }
            }
            else{
    
    
                ProjectUtility.AlertWith(self, message: " What's App is Not Available.", Title: "Sorry")
            }
    

    working fine But When I put This URL

    ("http://www.proprofs.com/quiz-school/story.php?title=pq-find-out-which-ice-age-character-you-are ")
    

    Then Its Not Working Please Check Thanks.HelpWill Be Appriciated.

    0 讨论(0)
  • 2020-12-13 21:22
     var url  = NSURL(string: "whatsapp://send?text=Hello%20Friends%2C%20Sharing%20some%20data%20here...%20!")
    
    //Text which will be shared on WhatsApp is: "Hello Friends, Sharing some data here... !"
    
        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")
                    }
                }
        }
    

    Note: text needs to be URL encoded. You can get it using any of the open source tools over internet or using addingPercentEncoding(withAllowedCharacters:) function in iOS. e.g.

    var urlString = "Hello Friends, Sharing some data here... !"
    var urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
    var url  = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)")
    
    0 讨论(0)
提交回复
热议问题