2017-08-10 16 views
0

我試圖在社交應用程序中分享主題和消息,如whatsapp,facebook,twitter等,但下面的代碼無法正常工作。如果社交應用程序未安裝到用戶應用程序,則應將其重定向到AppStore。請檢查我的代碼。如何在社交媒體應用程序中分享數據,如使用swift的whatsapp,facebook

static func sendWhatspp(msg:String) 
{ 
    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 { 
       AFWrapper.showError("Error!", msg: "WhatsApp is not Installed") 
      } 
     } 
    } 
} 


static func sendTwitter(msg:String) 
{ 
    let urlWhats = "twitter://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 { 
       AFWrapper.showError("Error!", msg: "Twitter is not Installed") 
      } 
     } 
    } 
} 

static func sendFB(msg:String) 
{ 
    let urlWhats = "facebook://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 { 
       AFWrapper.showError("Error!", msg: "Facebook is not Installed") 
      } 
     } 
    } 
} 

static func sendAll(msg:String) 
{ 
    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 { 
       AFWrapper.showError("Error!", msg: "No Share app Installed") 
      } 
     } 
    } 
} 
+0

請確定「不工作」 –

+0

我強烈認爲Twitter和Facebook網址不以「whatsapp://」 –

+0

@MartinR開頭,當我調用該函數「sendWhatspp」時。它對我說「WhatsApp沒有安裝」事件它安裝在我的iphone 6中。 –

回答

0

我認爲你的sendWhatsapp代碼沒有問題。您是否將WhatsApp添加到您的Info.plist LSApplicationQueriesSchemes中?你需要添加它才能工作。

分享到臉書和推特。他們是不同的

import Social 

if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) { 
    var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
    self.presentViewController(fbShare, animated: true, completion: nil) 
} else { 
    var alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil)) 
    self.presentViewController(alert, animated: true, completion: nil) 
} 

雨燕4.0:

if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) { 
     var fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook) 
     self.present(fbShare, animated: true, completion: nil) 
    } else { 
     let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)) 
     self.present(alert, animated: true, completion: nil) 
    } 

Twitter的:只是SLServiceTypeTwitter取代SLServiceTypeFacebook。

+0

感謝whatsapp現在正在工作。但我正在嘗試twiiter和facebook。我在info.plist中添加了FB。 –

+0

「fb:// send?text = \(msg)」我正在使用該網址。我認爲它應該是不同的 –

+0

你想通過Facebook Messenger分享帖子或發送消息嗎? – Sherman

相關問題