LoginSignup
2
3

More than 5 years have passed since last update.

自分のアプリから、iOS版GoogleDriveに遷移させたい

Last updated at Posted at 2017-04-24

はじめに

こちらは Xcode8.3.1 Swift3.0 で開発したものになります。

googleスプレッドシートを開こうとして、WebViewでもSafariでも開けない問題にぶつかりました。
そこでgoogleドライブのアプリで見ると、開けることが発見した!
そちらに今回、メモ書き程度にまとめていきたいと思いますmm

修正する箇所

  • plist
  • ソースコード

この二点を改修すれば、遷移するようになります。

plistについて

下記の内容を追加してください

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>googledrive</string>
        <string>itms-apps</string>
    </array> 

こちらでは外部アプリを呼び出すための準備になります。

googledrive → googleDrive遷移

itms-apps → AppStoreに遷移

できるbundleIDになります。これを予め、書いていないと遷移できませんでした。。。

ソースコード

下記の内容を追加します!

// googleDriveが開けるかどうかを確認
if UIApplication.shared.canOpenURL(URL(string:"googledrive://")!) {

                //Googledriveある場合
                let urlStr:String = "googledrive://" + 遷移させたいURL
                UIApplication.shared.openURL(URL(string:urlStr)!)

            } else {

          // GoogleDriveがインストールされていない場合
                let alert: UIAlertController = UIAlertController(title: "", message: "こちらはGoogleDriveをインストールしないと閲覧することができません。今からインストールしますか?", preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default) { (UIAlertAction) in
            //OKボタンアクション
            ////AppleStoreのGoogledriveダウンロードページを開く
                    let appStoreURL:String = "itms-apps://itunes.apple.com/app/id507874739"
                    UIApplication.shared.openURL(URL(string:appStoreURL)!)
                }
                let cancelAction = UIAlertAction(title: NSLocalizedString("cancel", comment: ""), style: .cancel)
                alert.addAction(okAction)
                alert.addAction(cancelAction)
                self.present(alert, animated: true, completion: nil)
            }

こちらでアプリをインストールしていない方も、スムーズもインストールできるようになります。
ポイントは、Alertを出したところです。そうすることで、インストールしていない場合にないも通知なしで、AppStoreに遷移することを防いでいます。

まとめ

今回の困った点は、2点で

  • GoogleDriveのBundleIDを調べること
  • appStoreのURLを探すこと

ぜひ困ったら使ってください!
少しずつQiitaに記事を載せ、定期的にOutPutしていきます!
また間違い等があった、教えてくださいmm

2
3
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
2
3