Help us understand the problem. What is going on with this article?

Swift で UIActivityViewController を利用してfacebookやTwitterにシェアをする

More than 5 years have passed since last update.

Safari などでシェアボタンを押すとTwitter や Facebook へのシェアができるようになっています。これを実装したいと思って調べました。
※ ちなみに、Storyboardを使っています

share.gif

UIActivityViewController を利用する

アプリケーションからTwitter などのシェアボタン(って言うのかわからないですが…)を呼び出す場合は、UIActivityViewController を利用します

UIButton で呼び出す

UIbutton(ここでは ButtonShare )にaddTarget で、function を呼び出します。

ButtonShare.addTarget(self, action: "share_action:", forControlEvents: .TouchUpInside)

UILongPressGestureRecognizer で呼び出す

画像を長押しのときも、呼び出したいです。

// 長押し
var myLongPressGesture = UILongPressGestureRecognizer(target: self, action: "lognPress:")

// 長押し - 2.0秒
myLongPressGesture.minimumPressDuration = 2.0
// 長押し - 許容範囲15px
myLongPressGesture.allowableMovement = 150

// 画像に長押しがあったときの対応
SampleImage.userInteractionEnabled = true
SampleImage.addGestureRecognizer(myLongPressGesture)

ツィートした結果

ソース(Swift)

Social を import するのを忘れずに。

import UIKit
import Social

class DetailViewController: UIViewController {

    @IBOutlet weak var detailDescriptionLabel: UILabel!
    @IBOutlet weak var ButtonShare: UIButton!
    @IBOutlet weak var SampleImage: UIImageView!

    func configureView() {
        if let detail: AnyObject = self.detailItem {
            if let label = self.detailDescriptionLabel {
                label.text = detail.description
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.configureView()

        // 長押し
        var myLongPressGesture = UILongPressGestureRecognizer(target: self, action: "lognPress:")

        // 長押し - 2.0秒
        myLongPressGesture.minimumPressDuration = 2.0

        // 長押し - 許容範囲15px
        myLongPressGesture.allowableMovement = 150

        // 画像に長押しがあったときの対応
        SampleImage.userInteractionEnabled = true
        SampleImage.addGestureRecognizer(myLongPressGesture)

        // share
        ButtonShare.addTarget(self, action: "share_action:", forControlEvents: .TouchUpInside)
    }

    func lognPress(sender: UILongPressGestureRecognizer) {
        println("lognPress")
        share_action(self)
    }

    func share_action(sender: AnyObject) {
        println("share_action")

        let secondActivityItem = "iOSからのテストです #桜"
        let firstActivityItem:UIImage = SampleImage.image! as UIImage

        let activityViewController = UIActivityViewController(
            activityItems: [firstActivityItem, secondActivityItem],
            applicationActivities: nil)


        activityViewController.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:Array!, error:NSError!) in
            if (completed) {
                // ここに完了後の処理を書く
                println("完了!")
            }
        }

        self.presentViewController(activityViewController, animated: true, completion: nil)
    }

実行環境

  • Xcode 6.0
  • iOS 8
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした