2017-08-10 5 views
0

whatsapp、facebook、twitterなどのソーシャルアプリで件名やメッセージを共有しようとしていますが、コードの下は動作しません。ソーシャルアプリがユーザアプリケーションにインストールされていない場合は、AppStoreにリダイレクトする必要があります。親切に私のコードをチェックしてください。howsapp、facebook like swiftのようなソーシャルメディアアプリでデータを共有する方法

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のURLは、 "sendWhatspp"という関数を呼び出すときに、 "whatsapp://" –

+0

@MartinRで始まらないことを強く想定しています。それは私のiPhoneにインストールされている "WhatsAppがインストールされていません"というイベントになります。 –

答えて

0

あなたのsendWhatsappコードに問題はないと思います。 what.appをInfo.plist LSApplicationQueriesSchemesに追加しましたか?それを機能させるために追加する必要があります。

FacebookとTwitterで共有する。彼らは4.0

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

スウィフト異なっている:Twitterに

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

:ちょうどSLServiceTypeTwitterでSLServiceTypeFacebookを交換してください。

+0

ありがとうwhatsappが今働いています。しかし、私はtwiiterとfacebookに取り組んでいます。 info.plistにFBを追加しました。 –

+0

"fb:// send?text = \(msg)"私はそのURLを使用しています。私はそれが何かdiffernetでなければならないと思う –

+0

あなたはポストを共有しようとしているまたはFacebookのメッセンジャーを介してメッセージを送信していますか? – Sherman

関連する問題