17
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Swift - UIActivityViewControllerで特定の方法でシェアされたのを判定する

Posted at

シンプル家計簿 - Moneybook で、特定の方法でシェアしてくれた時にだけなにかしてあげるというのをUIActivityViewControllerを使って実装してました。
ブレークポイントを使ってUIActivityType.rawValueで対象の方法を調べれます。

    let url = URL(string: "https://itunes.apple.com/jp/app/APP_ID?mt=8")!
    let activityVC = UIActivityViewController(activityItems: ["このアプリいいよ!", "シンプル家計簿 - Moneybook", url], applicationActivities: nil)
    present(activityVC, animated: true, completion: nil)
    
    // シェアしてくれたかの判定
    activityVC.completionWithItemsHandler = { (activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, activityError: Error?) in
        guard completed else {
            return
        }
        
        // シェア対象の一覧
        let targets = [
            "com.apple.UIKit.activity.PostToTwitter",
            "com.apple.UIKit.activity.PostToFacebook",
            "jp.naver.line.Share"
        ]
        
        // シェアされた方法は対象の方法か判定
        if let type = activityType, targets.index(of: type.rawValue) != nil {
            // 対象の方法でシェアしてくれた、なにかしてあげる
            print("ありがとう!これあげる!")
        } else {
            // 対象外の方法でシェアされた
            print("TwitterかfacebookかLINEでシェアしてね!")
        }
    }

17
11
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
17
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?