Swift言語:Dropbox APIを使ってみました
概要
Developers - Dropboxを参考にDropbox APIを使ってみました。
公式サイトのInstallに書かれているPodfileでインストールされる、SwiftyDropbox (2.0.3)/Alamofire(3.14)のバージョンではデータ取得に失敗します。
SwiftyDropboxでDropboxの簡単なファイル操作をするの記事と同じ、SwiftyDropbox(1.0.2)/Alamofire (2.0.2)をインストールするとうまく動作しました。
インストール
うまくデータが取得出来るバージョン
$ cat Podfile platform :ios, '8.0' use_frameworks! pod 'SwiftyDropbox', '~> 1.0.1' [~/Desktop/iPhoneApps/swift/HelloDropbox]$ pod install Updating local specs repositories Analyzing dependencies Downloading dependencies Installing Alamofire (2.0.2) Installing SwiftyDropbox (1.0.2) Generating Pods project Integrating client project Pod installation complete! There is 1 dependency from the Podfile and 2 total pods installed.
データ取得に失敗するバージョン
$ cat Podfile platform :ios, '8.0' use_frameworks! pod 'SwiftyDropbox' [~/Desktop/iPhoneApps/swift/HelloDropbox]$ pod install Updating local specs repositories Analyzing dependencies Downloading dependencies Installing Alamofire (3.1.4) Installing SwiftyDropbox (2.0.3) Generating Pods project Integrating client project Pod installation complete! There is 1 dependency from the Podfile and 2 total pods installed.
以下のエラーメッセージが出力されます。
message String? "Error in call to API function \"users/get_current_account\": request body: could not decode input as JSON" Some
実装
公式サイトのTutorialを参考に、Application.swiftとViewController.swiftに処理を追加します。
実行
以下のメッセージが出た場合には、Dropboxアプリがインストールされていない可能性があります。
-canOpenURL: failed for URL: "dbapi-2://1/connect" - error: "(null)",-canOpenURL: failed for URL: "dbapi-2://1/connect" - error: "(null)",
接続
以下のメッセージがコンソールに出力された場合には接続成功です。ここで、APIKEYはアプリのキーです。
db-APIKEY Success! User is logged into Dropbox with token: XXXXX-XXXXX-XXXX 2016-01-11 09:06:24.673
データ取得
Tutorialのコードが成功するとコンソールに情報が出力されます。
*** Get current account *** Hello ユーザアカウント! *** List folder *** Folder contents: ...