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

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

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

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

Swift

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

Q&A

0回答

1296閲覧

DropboxAPI上でファイルを上書きできない

Laz0530

総合スコア15

Xcode

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

Swift

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

1グッド

2クリップ

投稿2018/01/17 04:51

Dropbox APIをSwiftから使っています。
iOS向けのアプリを作成中で、具体的に述べると、テキストフィールドに任意の文字を入力し、アップロードボタンを押すとDropbox上にテキストファイルとして保存されるようなアプリを作っています。
現在、Xcode内のコードに適当なファイル名を書き込んで実行すると、そのファイル名でテキストファイルが作られる段階まで来ております。
しかし、上書き保存ができないため、実行するたびにコードで別のファイル名をつけなくてはならない状況です。
今のコードの書き方に問題があるはずなのですが、どこを直せば上書きできるようになるのかわかりません。
どなたか解決策を教えていただけませんか?
わかりにくい文章ですみません。
質問などございましたら追記希望をお気軽にしてください。

ちなみにコードは以下のようになっております。
回答よろしくお願い致します。

AppDelegate.swift import UIKit import SwiftyDropbox @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { DropboxClientsManager.setupWithAppKey("wj3f1f4046e0ana") return true } func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool { if let authResult = DropboxClientsManager.handleRedirectURL(url as URL) { switch authResult { case .success(let token): print("Success! User is logged into Dropbox.") case .error(let error, let description): print("Error: (description)") case .cancel: break } } return false } /* 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() let messageLabel = UILabel() 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(self.signInDropbox), for: .touchUpInside) self.view.addSubview(signInButton) //ダウンロードボタンを追加 let downLoadButton = UIButton(type: UIButtonType.system) downLoadButton.frame = CGRect(x:10, y:200, width:100, height:30) downLoadButton.setTitle("Down Load", for: .normal) downLoadButton.addTarget(self, action: #selector(self.downloadDropbox), for: .touchUpInside) self.view.addSubview(downLoadButton) messageLabel.frame = CGRect(x:10,y: 230,width: 300,height: 30) self.view.addSubview(messageLabel) } //アップロードの処理を追加 @objc func uploadToDropbox() { let sss: String? = textField.text let fileData: Data? = sss!.data(using: .utf8) if let client = DropboxClientsManager.authorizedClient { client.files.upload(path: "/upfile3.txt", input: fileData!).response{ response, error in 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("next.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)}) } @objc func downloadDropbox() { if let client = DropboxClientsManager.authorizedClient { /* 省略 */ client.files.download(path: "/sample.txt").response { response, error in if let (metadata, url) = response { print("Downloaded file name: (metadata.name)") print("Downloaded file data: (url)") // ファイル内容を表示{ let str: String? = String(data: url, encoding: .utf8) self.messageLabel.text = str; self.textField.text = "読み込み成功" } else { print(error!) } } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
DrqYuto👍を押しています

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

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

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

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

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

ryochin

2018/01/18 04:10

「上書き保存ができない」どこにできなくて(デバイス上? Dropbox 上?)、なにが起こるのでしょうか(エラー内容、プログラムが落ちる等)。
Laz0530

2018/01/18 08:24

ご質問ありがとうございます。 Dropbox上に上書きができないという意味です。 エラーは出ず、プログラムが落ちるのみです。 上書きができないので、ファイル名をいちいち書き換えて実行しなくてはいけません。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問