2015-08-17 101 views

回答

16
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().openURL(url!) 
    } 

注意:文本需要进行网址编码。您可以通过互联网上的任何开源工具或使用iOS中的stringByAddingPercentEncodingWithAllowedCharacters函数获取它。 例如

var urlString = "Hello Friends, Sharing some data here... !" 
var urlStringEncoded = urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet()) 
var url = NSURL(string: "whatsapp://send?text=\(urlStringEncoded!)") 
+0

Hi @ Pandurang Yachwad。我如何分享一个链接,你可以向我演示一个演示。 please.or是否有一个长度的URL我们可以分享,因为我试图分享www.Google.com哪些工作,但我的网址太长,所以我在文本区域变得空白。 –

+0

@AvinashMishra我不认为URL长度有任何限制。只需检查该网址是否可以直接在浏览器中运行。有时坏的网址会导致问题。 –

+0

在浏览器中正常工作不在此处。 –

1

我的代码看起来像这样

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

工作正常,但是当我把这个网址

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

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

5

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

是否有任何字符限制? –

相关问题