2

我想在 swift 中使用 SLComposeViewController 在社交媒体上分享内容和图像。我也在 objC 中做过,但我是 swift development 的新手。如果有任何通用类或任何库可以在社交媒体上快速共享内容,请建议我。

4

3 回答 3

7

不需要图书馆。你需要做的是:

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)
于 2016-02-08T06:15:10.290 回答
1

来源: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.
 }
}
于 2017-06-29T08:44:16.833 回答
0

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")
}
}

在此处输入图像描述 在此处输入图像描述

于 2018-03-09T11:07:36.683 回答