2

SLComposeViewControllerを使ってSNSでコンテンツや画像をSwiftでシェアしたい。私はobjCでもそれを行いましたが、私は迅速な開発に慣れていません.これに共通のクラスがあるかどうか、またはソーシャルメディアでコンテンツを迅速に共有するためのライブラリがあるかどうかを提案してください.

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

enter image description here enter image description here

于 2018-03-09T11:07:36.683 に答える