Skip to content

Instantly share code, notes, and snippets.

@matsuhisa
Created April 12, 2015 14:48
Show Gist options
  • Save matsuhisa/41ed0624a484edeca40c to your computer and use it in GitHub Desktop.
Save matsuhisa/41ed0624a484edeca40c to your computer and use it in GitHub Desktop.
Swift で UIActivityViewController を利用してfacebookやTwitterにシェアをする ref: http://qiita.com/matsuhisa@github/items/7a599e0276cfabe1090e
ButtonShare.addTarget(self, action: "share_action:", forControlEvents: .TouchUpInside)
// 長押し
var myLongPressGesture = UILongPressGestureRecognizer(target: self, action: "lognPress:")
// 長押し - 2.0秒
myLongPressGesture.minimumPressDuration = 2.0
// 長押し - 許容範囲15px
myLongPressGesture.allowableMovement = 150
// 画像に長押しがあったときの対応
SampleImage.userInteractionEnabled = true
SampleImage.addGestureRecognizer(myLongPressGesture)
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment