2015-08-17 159 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

是否有任何字符限制? –

相關問題