LoginSignup
5
8

More than 5 years have passed since last update.

[iOS] SwiftShareBubbles で SNS へシェアする導線を作る

Posted at

TL;DR

SwiftShareBubblesという SNS へのシェアや Safari を開く導線を簡単に導入するライブラリを作ったので使ってくださいという記事です。

ShareBubble.gif

SwiftShareBubbles

使い方

Cocoapods か Carthage でインストールします。

Cocoapods

pod 'SwiftShareBubbles'

Carthage

github "takecian/SwiftShareBubbles"

シェアをしたい ViewController の viewDidLoad の中でセットアップをします。

class ViewController: UIViewController, SwiftShareBubblesDelegate {

    var bubbles: SwiftShareBubbles?

    override func viewDidLoad() {
        super.viewDidLoad()
        bubbles = SwiftShareBubbles(point: CGPoint(x: view.frame.width / 2, y: view.frame.height / 2), radius: 100, in: view)
        bubbles?.showBubbleTypes = [Bubble.twitter, Bubble.line, Bubble.safari]
        bubbles?.delegate = self
    }

ここでは例として Twitter, LINE, Safari への Bubble(導線)を用意しました。
他にも Facebook や Youtube への Bubble も追加できます。サポートしている Bubble を全て追加するとこんな感じになります。

All.gif

ちょっと radius を大きくしないと入りきらないですね・・。

そして Bubble を選択された時のメソッドを実装します。

// SwiftShareBubblesDelegate
    func bubblesTapped(bubbles: SwiftShareBubbles, bubbleId: Int) {
        if let bubble = Bubble(rawValue: bubbleId) {
            print("\(bubble)")
            switch bubble {
            case .safari:
                UIApplication.shared.openURL(URL(string: "SOME_URL"))
            case .twitter:
                if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeTwitter) {
                    guard let composer = SLComposeViewController(forServiceType: SLServiceTypeTwitter) else { return }
                    composer.setInitialText("test test")
                    present(composer, animated: true, completion: nil)
                }
                break
            case .line:
                // TODO
                break
            default:
                break
            }
        } else {
            // custom case
        }
    }

簡単ですね。

自作の Bubble を使用する

ライブラリで用意されている Bubble だけでなく自作の Bubble を使うこともできます。

ShareAttirbute を使って Bubble を作成し、customBubbleAttributes にセットすれば OK です。

let customBubbleId = 100

    override func viewDidLoad() {
        super.viewDidLoad()
        ...
        let customAttribute = ShareAttirbute(bubbleId: customBubbleId, icon: UIImage(named: "Custom")!, backgroundColor: UIColor.white)
        bubbles?.customBubbleAttributes = [customAttribute]
        ...
    }

SNS 上で拡散してもらってアプリの認知度を広めることは大事なことなので SwiftShareBubbles を使ってシェアしてもらってみてはいかがでしょうか。

SwiftShareBubbles

5
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
8