问题
i want to share content and images on social media using SLComposeViewController in swift. i have also done it in objC but i am new in swift development.please suggest me if there is any common class for this or Any library to share content on social media in swift.
回答1:
There is no need of library. What you need to do is:
1) Just import the Social
framework.
2) Code to post on 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) Code to post on 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)
回答2:
Source: 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.
}
}
回答3:
UIActivityViewController class is provide to sharing option to anywhere.
We can share the content in facebook and twitter.but for instagram sharing we have to use UIActivityViewController.
Import social like
import Social
Facebook : Share With Image,Content and 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 : Share With Image,Content and 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")
}
}
来源:https://stackoverflow.com/questions/35263309/how-to-share-content-on-facebook-twitter-and-instagram-using-slcomposeviewcontr