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

在...创建的问题 Mon, Jul 17, 2017 12:00 AM

对于我的某个应用,我想与WhatsApp联系人分享数据。我在StackOverflow上尝试了一些解决方案,但无法获得精确的解决方案。经过一些试验可以实现我想要的,所以在这里分享,以供将来参考。

    
13
4个答案                              4 跨度>                         
 
 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")
                }
            }
    }

注意:文本需要进行URL编码。您可以通过互联网使用任何开源工具或在iOS中使用addsPercentEncoding(withAllowedCharacters :)功能来获取它。 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!)")
    
21
2018-07-22 06:57:34Z
  1. 你好@Pandurang Yachwad。我如何分享链接你能给我看一个演示。 please.or是否有一个Url的长度我们可以分享,因为我试图分享www.Google.com哪个有效但我的网址太长所以我在文本区域变得空白。
    2016-10-12 09:57:50Z
  2. @ AvinashMishra我认为URL长度没有任何限制。只需检查该网址是否直接在浏览器中运行。有时坏网址导致问题。
    2016-10-12 17:19:01Z
  3. 在浏览器中工作正常而不是在这里。
    2016-10-13 04:07:29Z
  4. @ AvinashMishra如果你分享你的代码,我可以看一下,让你知道这是什么问题
    2016-10-13 14:13:09Z
  5. 我已经在答案中发布我的代码请检查并对不起新问题,因为我无法发布新问题。这就是为什么我做了这个抱歉。
    2016-10-14 12:37:38Z
  6. 醇>

Swift 3.0

尝试使用此代码访问应用程序中的watsapp。它非常适合我。

 
@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")
            }
        }
    }
}
    
6
2017-04-20 07:53:13Z
  1. 是否有字符限制?
    2017-10-04 08:39:20Z
  2. 醇>

除了上面的解决方案,从iOS 9开始,我们需要在info.plist中添加whatsapp到LSApplicationQueriesSchemes键。在此之后它才对我有用。

    
3
2018-08-04 05:15:45Z

我的代码看起来像这样

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

工作正常但是当我把这个URL

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

然后它不工作请检查Thanks.HelpWill Appriciated。

    
2
2016-10-14 12:35:41Z
来源放置 这里