Last active
July 15, 2016 05:26
-
-
Save gitbricho/4d1738ae1d1767edd421fc10644135d5 to your computer and use it in GitHub Desktop.
Dropbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftyDropbox | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func application(application: UIApplication, | |
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// アプリからユーザーの Dropbox アカウントへのリンクを処理するために | |
// DropboxAuthManager を作成する | |
Dropbox.setupWithAppKey("APP-KEY") | |
return true | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// 登録したURLスキーム(db-APP_KEY) により、 | |
// ユーザーが認証処理を行った後に呼ばれる | |
func application(app: UIApplication, openURL url: NSURL, | |
options: [String : AnyObject]) -> Bool { | |
if let authResult = Dropbox.handleRedirectURL(url) { | |
switch authResult { | |
case .Success(let token): | |
print("認証成功! ユーザーは以下のトークンで Dropbox にログインしました: \(token)") | |
case .Error(let error, let description): | |
print("認証失敗 \(error): \(description)") | |
} | |
} | |
return true | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
// MARK: action | |
//Dropboxへリンク | |
@IBAction func link(sender: AnyObject) { | |
if let _ = Dropbox.authorizedClient { | |
// | |
} else { | |
Dropbox.authorizeFromController(self) | |
} | |
} | |
//Dropboxからアンリンク | |
@IBAction func unlink(sender: AnyObject) { | |
if let _ = Dropbox.authorizedClient { | |
Dropbox.unlinkClient() | |
userAccount.text="Dropboxへログインしてください!" | |
} | |
} | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
@IBAction func runTest(sender: AnyObject) { | |
if let client = Dropbox.authorizedClient { | |
let files = client.files | |
//5:テストフォルダのコンテンツ一覧を表示 | |
let test5 = { | |
print(">> コンテンツ一覧: \(self.FOLDER_PATH)") | |
files.listFolder(path: self.FOLDER_PATH).response { | |
response, error in | |
if let result = response { | |
print(result) | |
} else if let callError = error { | |
print(callError) | |
} | |
} | |
} | |
//4:テストファイルをコピー | |
let test4 = { | |
let toPath = self.FILE_PATH + "_copy" | |
print(">> コピー: \(self.FILE_PATH) -> \(toPath)") | |
files.copy(fromPath: self.FILE_PATH, | |
toPath: toPath).response { | |
response, error in | |
if let result = response { | |
print(result) | |
test5() | |
} else if let callError = error { | |
print(callError) | |
} | |
} | |
} | |
//3:テストファイルを作成(アップロード) | |
let test3 = { | |
print(">> アップロード: \(self.FILE_PATH)") | |
files.upload(path: self.FILE_PATH, | |
input: self.TEST_DATA).response { | |
response, error in | |
if let result = response { | |
print(result) | |
test4() | |
} else if let callError = error { | |
print(callError) | |
} | |
} | |
} | |
//2:テストフォルダを作成 | |
let test2 = { | |
print(">> フォルダ作成: \(FOLDER_PATH)") | |
files.createFolder(path: self.FOLDER_PATH).response { | |
response, error in | |
if let result = response { | |
print(result) | |
test3() | |
} else if let callError = error { | |
print(callError) | |
} | |
} | |
} | |
//1:テストフォルダを削除 | |
print(">> コンテンツ削除: \(FOLDER_PATH)") | |
files.delete(path: FOLDER_PATH).response { | |
response, error in | |
if let result = response { | |
print(result) | |
test2() | |
} else if let callError = error { | |
print(callError) | |
} | |
} | |
} | |
} | |
@IBAction func runGetMetaData(sender: AnyObject) { | |
if let client = Dropbox.authorizedClient { | |
// ファイル(またはフォルダ)のメタデータの取得 | |
client.files.getMetadata(path: self.FILE_PATH).response { | |
response, error in | |
print("*** メタデータの取得 ***") | |
if let metadata = response { | |
if let file = metadata as? Files.FileMetadata { | |
print("ファイルパス: \(file.pathLower)") | |
print("ファイルサイズ: \(file.size)") | |
} else if let folder = metadata as? Files.FolderMetadata { | |
print("フォルダパス: \(folder.pathLower)") | |
} | |
} else { | |
print("メタデータ取得エラー:\(error!)") | |
} | |
} | |
} | |
} | |
@IBAction func runDownload(sender: AnyObject) { | |
if let client = Dropbox.authorizedClient { | |
client.files.download(path: self.FILE_PATH).response { | |
response, error in | |
if let (metadata, data) = response { | |
print("*** ダウンロード ***") | |
print("ファイル名: \(metadata.name)") | |
if let decodedString = NSString(data: data, encoding: NSUTF8StringEncoding) { | |
print("ファイルデータ: \(decodedString)") | |
} | |
} else { | |
print("ダウンロードエラー:\(error!)") | |
} | |
} | |
} | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import SwiftyDropbox | |
class ViewController: UIViewController { | |
@IBOutlet weak var userAccount: UILabel! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
} | |
override func viewDidAppear(animated: Bool) { | |
if let client = Dropbox.authorizedClient { | |
client.users.getCurrentAccount().response { | |
response, error in | |
if let account = response { | |
self.userAccount.text = "Hello \(account.name.givenName)!" | |
} else { | |
self.userAccount.text = | |
"Dropbox へログインしてください" | |
print(error!) | |
} | |
} | |
} | |
} | |
... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
<key>LSApplicationQueriesSchemes</key> | |
<array> | |
<string>dbapi-8-emm</string> | |
<string>dbapi-2</string> | |
</array> | |
... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
platform :ios, '8.0' | |
use_frameworks! | |
target 'DropboxDemo' do | |
pod 'SwiftyDropbox' | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> コンテンツ削除: /DbxTest/testFolder | |
{ | |
id = "id:K3jXKFg8SFAAAAAAAAAAAQ"; | |
name = testFolder; | |
"path_display" = "/DbxTest/testFolder"; | |
"path_lower" = "/dbxtest/testfolder"; | |
} | |
>> フォルダ作成: /DbxTest/testFolder | |
{ | |
id = "id:AFV5LHU91vAAAAAAAAAAAQ"; | |
name = testFolder; | |
"path_display" = "/DbxTest/testFolder"; | |
"path_lower" = "/dbxtest/testfolder"; | |
} | |
>> アップロード: /DbxTest/testFolder/testFile | |
{ | |
"client_modified" = "2016-07-15T03:44:59Z"; | |
id = "id:Bk_m6P0mY-AAAAAAAAAAAQ"; | |
name = testFile; | |
"path_display" = "/DbxTest/testFolder/testFile"; | |
"path_lower" = "/dbxtest/testfolder/testfile"; | |
rev = 103f3ec127; | |
"server_modified" = "2016-07-15T03:44:59Z"; | |
size = 27; | |
} | |
>> コピー: /DbxTest/testFolder/testFile -> /DbxTest/testFolder/testFile_copy | |
{ | |
"client_modified" = "2016-07-15T03:44:59Z"; | |
id = "id:hZpqXvsjzFAAAAAAAAAAAQ"; | |
name = "testFile_copy"; | |
"path_display" = "/DbxTest/testFolder/testFile_copy"; | |
"path_lower" = "/dbxtest/testfolder/testfile_copy"; | |
rev = 113f3ec127; | |
"server_modified" = "2016-07-15T03:45:03Z"; | |
size = 27; | |
} | |
>> コンテンツ一覧: /DbxTest/testFolder | |
{ | |
cursor = "AAGjyq5Ba2AO1Qh_7RPsrXnANMy43qQ-iXxdg-Aom607KVad3FVZ-zNm64vkAiuDxnk8S9KFSDrRL9XLhqQAjAGAc8YXIFwSPF8sO4TJGlxTUFf1BSnk8v1USU3i-9HIwJLM-fgpajfcA03vSxvSzQJt97Gk5HBjWhQvIYRoS_6qzA"; | |
entries = ( | |
{ | |
".tag" = file; | |
"client_modified" = "2016-07-15T03:44:59Z"; | |
id = "id:Bk_m6P0mY-AAAAAAAAAAAQ"; | |
name = testFile; | |
"path_display" = "/DbxTest/testFolder/testFile"; | |
"path_lower" = "/dbxtest/testfolder/testfile"; | |
rev = 103f3ec127; | |
"server_modified" = "2016-07-15T03:44:59Z"; | |
size = 27; | |
}, | |
{ | |
".tag" = file; | |
"client_modified" = "2016-07-15T03:44:59Z"; | |
id = "id:hZpqXvsjzFAAAAAAAAAAAQ"; | |
name = "testFile_copy"; | |
"path_display" = "/DbxTest/testFolder/testFile_copy"; | |
"path_lower" = "/dbxtest/testfolder/testfile_copy"; | |
rev = 113f3ec127; | |
"server_modified" = "2016-07-15T03:45:03Z"; | |
size = 27; | |
} | |
); | |
"has_more" = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment