【问题标题】:how to share content on facebook, twitter and instagram using SLComposeViewController swift如何使用 SLComposeViewController swift 在 facebook、twitter 和 instagram 上共享内容
【发布时间】:2018-03-09 19:07:36
【问题描述】:

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

【问题讨论】:

    标签: swift ios9 slcomposeviewcontroller socialshare


    【解决方案1】:

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

    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)
    

    【讨论】:

      【解决方案2】:

      来源: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 类提供到任何地方的共享选项。

        我们可以在 facebook 和 twitter 上分享内容。但是对于 instagram 分享,我们必须使用 UIActivityViewController。

        导入社交赞

        import Social
        

        Facebook:分享图片、内容和网址。

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

        【讨论】:

          猜你喜欢
          • 2014-07-11
          • 2015-07-20
          • 2017-07-31
          • 2018-04-23
          • 1970-01-01
          • 2013-04-13
          • 1970-01-01
          • 1970-01-01
          • 2012-10-19
          相关资源
          最近更新 更多