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

UIActivityViewControllerでSafariを開くがデフォルトでない件

More than 1 year has passed since last update.

UIActivityViewControllerとは

IMG_3689.jpg

アプリでよくあるこういうやつ
詳しい使い方は
[Swift]UIActivityの使い方まとめ
を見ていただければわかると思います。

ただこの機能、デフォルトで「Safariで開く」がなくすごく不便だった・・
ほとんどのキュレーションアプリでみるからデフォルトかと思ってたらみんな自分で実装してたのか。。

実装自体はすごく簡単

まずUIActivityを継承したActivityを作成

CustomActivity.swift
import UIKit

public class CustomActivity: UIActivity {

    // タイトル
    override public var activityTitle: String? {
        return "title"
    }

  // 画像
    override public var activityImage: UIImage? {
       return UIImage(name: "imageName")
    }

    /*
    // 動作させるかどうか
    // ex.URLを開けるならtrue、開けないならfalse
    */
    override public func canPerform(withActivityItems activityItems: [Any]) -> Bool {
        return true
    }

    // 動く直前にしたい動作
    override public func prepare(withActivityItems activityItems: [Any]) {
    }

    // 選択されたときにしたい処理
    override public func perform() {
        self.activityDidFinish(true)
    }

}

それをぶちこんで遷移するだけ

   let activity = UIActivityViewController(activityItems: [""], applicationActivities: [CustomActivity()])
   present(activity, animated: true, completion: nil)

ライブラリにしてみた

ただSafariで開くはめちゃめちゃ需要高そうだし苦しむ人がいるのでは。。と思いライブラリを作成してみました。

KSSafariActivity

   let activity = UIActivityViewController(activityItems: [""], applicationActivities: [KSSafariActivity(title: "open safari", url: URL(string: "https://hogehoge")!)])
   present(activity, animated: true, completion: nil)

だけで

image.png

ActivityViewControllerにSafariで開くをい追加できます!ぜひ使ってください〜〜ヽ(・∀・)ノ

kohei1218
iOS、Android、Rails、たまにLaravelのペーペーエンジニア 至らないところが多いですがご指摘いただけると助かります。。。
uzabase
企業活動の意思決定を支える情報インフラの提供
https://www.uzabase.com/
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
ユーザーは見つかりませんでした