質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

0回答

712閲覧

XcodeでDropboxにファイルがアップロードできない

Laz0530

総合スコア15

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

1グッド

2クリップ

投稿2018/01/15 04:53

編集2018/01/15 04:56

DropboxAPIをSwiftから使おうと思っています。
作りたいアプリは、テキストフィールドに入力した簡単な文字をテキストファイルとしてDropboxに保存できるというものです。

https://qiita.com/keonheon/items/b08a63552d2cd46cb477

上のサイトを参考にコードを書いて実行してみました。
書き込んだコードは下の通りです。

AppDelegate.swift import UIKit import SwiftyDropbox @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? /* func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { DropboxClientsManager.setupWithAppKey("wj3flf4046e0ana") // Override point for customization after application launch. return true }*/ func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { DropboxClientsManager.setupWithAppKey("wj3f1f4046e0ana") return true } func application(_ app: UIApplication, openURL url: URL,options: [String : AnyObject]) -> Bool{ if let authResult = DropboxClientsManager.handleRedirectURL(url) { switch authResult{ case .success(let token): print("Success! User is logged into Dropbox with token: (token)") case .error(let error, let description): print("Error(error):(description)") case .cancel: print("Authorization flow was manually canceled by user!") } } return false } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. // Saves changes in the application's managed object context before the application terminates. } // MARK: - Core Data stack }
ViewController.swift import UIKit import SwiftyDropbox class ViewController: UIViewController { let textField = UITextField() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. //テキスト入力欄を追加 textField.frame = CGRect(x:10, y:70, width:300, height:30) textField.borderStyle = UITextBorderStyle.roundedRect self.view.addSubview(textField) //テキスト保存ボタンを追加 let saveButton = UIButton(type: UIButtonType.system) saveButton.frame = CGRect(x:10, y:110, width:100, height:30) saveButton.setTitle("Save", for: .normal) saveButton.addTarget(self, action: #selector(ViewController.saveText), for: .touchUpInside) self.view.addSubview(saveButton) //アップロードボタンを追加 let uploadButton = UIButton(type: UIButtonType.system) uploadButton.frame = CGRect(x:10, y:150, width:100, height:30) uploadButton.setTitle("Upload", for: .normal) uploadButton.addTarget(self, action: #selector(ViewController.uploadToDropbox), for: .touchUpInside) self.view.addSubview(uploadButton) let signInButton = UIButton(type: UIButtonType.system) signInButton.frame = CGRect(x:10, y:30, width:100, height:30) signInButton.setTitle("Sign In", for: .normal) // signInButton.addTarget(self, action: Selector(("signInDropbox")), for: .touchUpInside) signInButton.addTarget(self, action: #selector(self.signInDropbox), for: .touchUpInside) self.view.addSubview(signInButton) } @objc func uploadToDropbox() { let tmpURL = NSURL(fileURLWithPath: NSTemporaryDirectory()) let fileURL = tmpURL.appendingPathComponent("sample.txt") let pathD = fileURL!.path if let client = DropboxClientsManager.authorizedClient { /*client.files.upload(path: "/sample.txt", mode: Files.WriteMode.overwrite, autorename: true, clientModified: NSDate(), mute: false, body: fileURL).response { response, error in*/ client.files.upload(path: "/sample.txt", input: fileURL!).response{ response, error in //client.files.copy(fromPath: pathD, toPath: "/sample_2.txt" ).response{ response, error in self.textField.text = "アップロード成功" if let metadata = response { print("Uploaded file name: (metadata.name)") self.textField.text = "アップロード成功" } else { print(error!) } } } } @objc func saveText(){ let tmpURL = NSURL(fileURLWithPath: NSTemporaryDirectory()) let fileURL = tmpURL.appendingPathComponent("sample.txt") do { try textField.text?.write(to: fileURL!, atomically: true, encoding: String.Encoding.utf8) textField.text = "書き出し成功" print("Save text file") } catch { // 失敗 print("Save text fail") } } @objc func signInDropbox(){ if let _ = DropboxClientsManager.authorizedClient{ DropboxClientsManager.unlinkClients() } //DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: {(url: URL) -> Void in UIApplication.shared.openURL(url)}) DropboxClientsManager.authorizeFromController(UIApplication.shared, controller: self, openURL: {(url: URL) -> Void in UIApplication.shared.openURL(url)}) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }

実行するとDropboxに飛び認証画面が出てきて、許可するとアプリの画面に戻りました。そしてテキストフィールドに文字を打ち込み、Saveボタンを押すとテキストフィールドに「書き出し成功」の文字が表示されるようになりました。
問題はここからで、Uploadボタンを押すとテキストフィールドに入力した文字がテキストファイルとしてDropboxにアップロードされるはずなのですが、アップロードできませんでした。ボタンが正常に機能しているかどうか確かめるために、ボタンを押すとテキストフィールドに「アップロード成功」と表示されるようにコードを書いたのですが、表示されませんでした。コードの場所が悪いのかと思い、self.textField.text = "アップロード成功"
を二箇所に書いたのですが表示されませんでした。
つまり、このボタンは機能していないということです。

Uploadボタンを押すと、テキストフィールドに入力した文字がテキストファイルとしてDropboxに保存されるようにするにはどうしたら良いでしょうか?
どなたかご教授よろしくお願いします。

如何せん初心者のためわかりにくい点もあるかと思います。
その際は質問していただければ幸いです。
どうかお願いします。

DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問