[英]How to Share on Facebook and Twitter App in Swift 3 without SDK
I need to share some link on facebook and twitter app if installed in device on a button tap.I can share the same using Whatsapp (code is below).I want to know if i can do the same in facebook and twitter app also. 我需要在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.")
}
}
If you are bound not to use FB / Twitter SDK . 如果您有义务不使用FB / Twitter SDK。 then you can try using activity controller and share from your app . 然后你可以尝试使用活动控制器并从你的应用程序共享。 Here you will get every possible share option . 在这里,您将获得所有可能的共享选项。
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)
}
First you need to get the FBSDK from Facebook's developer website. 首先,您需要从Facebook的开发者网站获取FBSDK。
Then you can do something like this: 然后你可以做这样的事情:
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()
}
}
Don't forget to set the delegate methods: 不要忘记设置委托方法:
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)")
}
The above works for Swift 3+ Let me know if it works for you. 以上适用于Swift 3+让我知道它是否适合您。 Cheers 干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.