2017-04-14 79 views
2

我需要分享一些鏈接的Facebook和Twitter應用程序,如果安裝在設備上的按鈕tap.I可以共享相同使用WhatsApp(代碼如下)。我想知道我是否也可以在Facebook和Twitter應用程序中做同樣的事情。如何分享Facebook和Twitter應用程序在Swift 3沒有SDK

@IBAction func whatsappbtn(_ sender: UIButton) { 

    var str = "This is the string which you want to share to WhatsApp" 
    str=str.addingPercentEncoding(withAllowedCharacters: (NSCharacterSet.urlQueryAllowed))! 
    let whatsappURL = URL(string: "whatsapp://send?text=\(str)") 
    if UIApplication.shared.canOpenURL(whatsappURL!) { 
     if #available(iOS 10.0, *) { 
      UIApplication.shared.open(whatsappURL!, options: [:], completionHandler: nil) 
     } else { 
      // Fallback on earlier versions 
     } 
    } else { 

     self.delegate?.alerting(msg: "Please install Whatsapp and try again.") 

    } 

} 

回答

5

如果你不想使用FB/Twitter SDK。那麼你可以嘗試使用活動控制器並從你的應用程序共享。在這裏你會得到每一個可能的份額選項。

var activityViewController:UIActivityViewController? 
textField .text = "Some Test" 

@IBAction func shareText(sender: UIButton) { 
    let activityViewController = UIActivityViewController(
     activityItems: [textField.text as NSString], 
     applicationActivities: nil) 

    presentViewController(activityViewController, animated: true, completion: nil) 
} 

enter image description here

1

首先,你需要從Facebook的開發者網站的FBSDK。

然後,你可以做這樣的事情:

func shareToFacebook() { 
     let inviteDialog = FBSDKAppInviteDialog() 

     if inviteDialog.canShow() { 

      guard let appLinkURL = URL(string: "YOUR LINK") else { return } 
      guard let previewImageURL = URL(string: "YOUR LINK IMAGE") else { return } 

      let inviteContent = FBSDKAppInviteContent() 
      inviteContent.appLinkURL = appLinkUrl 
      inviteContent.appInvitePreviewImageURL = previewImageURL 

      inviteDialog.content = inviteContent 
      inviteDialog.delegate = self 
      inviteDialog.show() 
     } 
    } 

不要忘記設置委託方法:

func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didCompleteWithResults results: [AnyHashable : Any]!) { 
     print("Did complete sharing.. ") 
    } 

    func appInviteDialog(_ appInviteDialog: FBSDKAppInviteDialog!, didFailWithError error: Error!) { 
     print("Error tool place in appInviteDialog \(error)") 
    } 

斯威夫特3+ 上述作品讓我知道如果它適合你。歡呼聲

+1

日Thnx,但我需要沒有SDK代碼。 –

相關問題