我想在swift中使用SLComposeViewController在社交媒体上分享内容和图像。我也是在objC中完成的,但我是快速开发的新手。请告诉我,如果有任何共同的类或任何库在swift社交媒体上分享内容。
不需要图书馆。你需要做的是:
1)只需导入Social
框架。
2)在Facebook
上发布的代码
let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
vc.setInitialText("Picture Text")
vc.addImage(detailImageView.image!)
vc.addURL(NSURL(string: "http://anyurl.com"))
presentViewController(vc, animated: true, completion: nil)
3)在Twitter上发布的代码
let vc = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
vc.setInitialText("Picture Text")
vc.addImage(detailImageView.image!)
vc.addURL(NSURL(string: "http://anyurl.com"))
presentViewController(vc, animated: true, completion: nil)
资料来源:https://www.youtube.com/watch?v=B_x-ccc8Iuc
import UIKit
import Social
class ViewController: UIViewController {
@IBAction func buttonAction(_ sender: Any) //Add Storyboard a button
{
//Alert
let alert = UIAlertController(title: "Share", message: "Share the poem of the day!", preferredStyle: .actionSheet)
//First action
let actionOne = UIAlertAction(title: "Share on Facebook", style: .default) { (action) in
//Checking if user is connected to Facebook
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook)
{
let post = SLComposeViewController(forServiceType: SLServiceTypeFacebook)!
post.setInitialText("Poem of the day")
post.add(UIImage(named: "img.png"))
self.present(post, animated: true, completion: nil)
} else {self.showAlert(service: "Facebook")}
}
//Second action
let actionTwo = UIAlertAction(title: "Share on Twitter", style: .default) { (action) in
//Checking if user is connected to Facebook
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter)
{
let post = SLComposeViewController(forServiceType: SLServiceTypeTwitter)!
post.setInitialText("Poem of the day")
post.add(UIImage(named: "img.png"))
self.present(post, animated: true, completion: nil)
} else {self.showAlert(service: "Twitter")}
}
let actionThree = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
//Add action to action sheet
alert.addAction(actionOne)
alert.addAction(actionTwo)
alert.addAction(actionThree)
//Present alert
self.present(alert, animated: true, completion: nil)
}
func showAlert(service:String)
{
let alert = UIAlertController(title: "Error", message: "You are not connected to \(service)", preferredStyle: .alert)
let action = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
UIActivityViewController类提供给任何地方的共享选项。
我们可以在facebook和twitter上分享内容。但是对于Instagram共享,我们必须使用UIActivityViewController。
像进口社交一样
import Social
Facebook:与图像,内容和URL共享。
@IBAction func facebookButtonShare(_ sender: Any) {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook) {
let facebookShare = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
if let facebookShare= facebookShare{
facebookShare.setInitialText("Excellent development")
facebookShare.add(UIImage(named: "myProduct.jpg")!)
facebookShare.add(URL(string: "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"))
self.present(facebookShare, animated: true, completion: nil)
}
} else {
print("Not Available")
}
}
Twitter:与图像,内容和URL共享。
@IBAction func tweetButtonShare(_ sender: Any) {
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {
let tweetShare = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
if let tweetShare = tweetShare {
tweetShare.setInitialText("Excellent development")
tweetShare.add(UIImage(named: "iOS.jpg")!)
tweetShare.add(URL(string: "https://stackoverflow.com/users/4600136/mr-javed-multani?tab=profile"))
self.present(tweetShare, animated: true, completion: nil)
}
} else {
print("Not Available")
}
}