[英]How to Share on Facebook and Twitter App in Swift 3 without SDK
我需要在Facebook和Twitter應用程序上分享一些鏈接,如果在按鈕上安裝在設備上。我可以使用Whatsapp共享相同的代碼(代碼如下)。我想知道我是否也可以在Facebook和Twitter應用程序中執行相同操作。
@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.")
}
}
如果您有義務不使用FB / Twitter SDK。 然后你可以嘗試使用活動控制器並從你的應用程序共享。 在這里,您將獲得所有可能的共享選項。
var activityViewController:UIActivityViewController?
textField .text = "Some Test"
@IBAction func shareText(sender: UIButton) {
activityViewController = UIActivityViewController(
activityItems: [textField.text as NSString],
applicationActivities: nil)
presentViewController(activityViewController, animated: true, completion: nil)
}
首先,您需要從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)")
}
以上適用於Swift 3+讓我知道它是否適合您。 干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.