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

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

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

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

Q&A

解決済

1回答

5293閲覧

測定したデータをCSVファイルとしてOneDriveに送信したい(ver2)

yuki84

総合スコア23

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

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

Swift

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

0グッド

0クリップ

投稿2018/07/30 05:06

開発環境 Xcode9.4 / Swift4.1

測定したデータをCSVファイルにして他のアプリに送信する方法として、以前の質問で、一度ファイルとして保存してからそのパスを指定する方法を教えていただき、その方法で以下のようにコードを書きました。参考にしたサイトは下の方にまとめています。
しかし、実行して、OneDriveに送信しようとしたところ、ファイル名が表示されずOneDriveのアカウントも選択できない状態でした。
コンソール画面の出力を見たところ、CSV形式への変換やパスの生成はできているようです。
解決策をご教示頂けますでしょうか。

swift

1//ファイル送信ボタン 2 @IBAction func sendFile(_ sender: Any) { 3 createFile(fileArrData: tapData) 4 } 5 6 func createFile(fileArrData : [[String]]){ 7 8 var fileStrData:String = "" 9 let fileName = "tapdata4.7inch.csv" 10 11 //StringのCSV用データを準備 12 for singleArray in fileArrData{ 13 for singleString in singleArray{ 14 fileStrData += "\"" + singleString + "\"" 15 if singleString != singleArray[singleArray.count-1]{ 16 fileStrData += "," 17 } 18 } 19 fileStrData += "\n" 20 } 21 print(fileStrData) 22 23 // DocumentディレクトリのfileURLを取得 24 let documentDirectoryFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! 25 26 // ディレクトリのパスにファイル名をつなげてファイルのフルパスを作る 27 let FilePath = documentDirectoryFileURL.appendingPathComponent(fileName) 28 29 print("書き込むファイルのパス: (FilePath)") 30 31 do { 32 try fileStrData.write(to: FilePath, atomically: true, encoding: String.Encoding.utf8) 33 } catch let error as NSError { 34 print("failed to write: (error)") 35 } 36 37 let documentInteraction = UIDocumentInteractionController(url: URL(fileURLWithPath: "(FilePath)")) 38 39 if !documentInteraction.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true) 40 { 41 // 送信できるアプリが見つからなかった時の処理 42 let alert = UIAlertController(title: "送信失敗", message: "ファイルを送れるアプリが見つかりません", preferredStyle: .alert) 43 alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) 44 self.present(alert, animated: true, completion: nil) 45 } 46 47 }

以下コンソール出力

書き込むファイルのパス: file:///var/mobile/Containers/Data/Application/12E639CD-26D3-4250-A587-21987CA04755/Documents/tapdata4.7inch.csv 2018-07-30 13:15:03.258406+0900 LearningTask4.7inch[48044:3362663] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /private/var/containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles 2018-07-30 13:15:03.258612+0900 LearningTask4.7inch[48044:3362663] [MC] Reading from public effective user settings.

参考サイト
Swiftでファイル読み込みと保存
[Swift3.0] テキストをファイルに書き込んでアプリ内に保存する
Documents下に多次元配列からCSVファイルを作る
[Swift 3.0] iOSアプリ間でファイルの送受信を行う

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

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

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

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

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

guest

回答1

0

ベストアンサー

UIDocumentInteractionControllerのインスタンスをViewControllerのプロパティとして保持しておかないとうまくいかないようです。(参考URLの内容まま文章引用)
(遠い記憶を遡ると前に何かのケースで同じようなパターンがあったのを思い出しました、あやうくハマりかけました・・・。)

【Swift】UIDocumentInteractionControllerで指定したアプリが開かないとき

コメントへの回答追記:
UIDocumentInteractionControllerのurlの中身がおかしいのかもしれません。
保存できたコードを以下に貼りますので試してみてください。

class ViewController: UIViewController { private var tapData: [[String]] = [["11", "22"]] var documentInteraction: UIDocumentInteractionController! override func viewDidLoad() { super.viewDidLoad() } @IBAction func sendFile(_ sender: Any) { createFile(fileArrData: tapData) } func createFile(fileArrData : [[String]]){ var fileStrData:String = "" let fileName = "tapdata4.7inch.csv" //StringのCSV用データを準備 for singleArray in fileArrData{ for singleString in singleArray{ fileStrData += "\"" + singleString + "\"" if singleString != singleArray[singleArray.count-1]{ fileStrData += "," } } fileStrData += "\n" } print(fileStrData) // DocumentディレクトリのfileURLを取得 let documentDirectoryFileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last! // ディレクトリのパスにファイル名をつなげてファイルのフルパスを作る let FilePath = documentDirectoryFileURL.appendingPathComponent(fileName) print("書き込むファイルのパス: (FilePath)") do { try fileStrData.write(to: FilePath, atomically: true, encoding: String.Encoding.utf8) } catch let error as NSError { print("failed to write: (error)") } documentInteraction = UIDocumentInteractionController() documentInteraction.url = FilePath if !(documentInteraction?.presentOpenInMenu(from: self.view.frame, in: self.view, animated: true))! { // 送信できるアプリが見つからなかった時の処理 let alert = UIAlertController(title: "送信失敗", message: "ファイルを送れるアプリが見つかりません", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) self.present(alert, animated: true, completion: nil) } } }

投稿2018/07/30 08:58

編集2018/07/31 05:17
razuma

総合スコア1313

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

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

yuki84

2018/07/31 04:45

回答ありがとうございます。参考URLを見ながらコードを変更しましたが、OneDriveを選択するとファイル選択の画面が一旦出てすぐに閉じられていまいました。コンソールには以下のような出力があり、エラーが出ているようですが、どう対処すればいいでしょうか。 書き込むファイルのパス: file:///var/mobile/Containers/Data/Application/C8394065-40A2-42A4-9618-46F5E2BB4220/Documents/tapdata4.7inch.csv 2018-07-31 11:51:29.434386+0900 LearningTask4.7inch[49744:3608404] Couldn't get file size for file:/var/mobile/Containers/Data/Application/C8394065-40A2-42A4-9618-46F5E2BB4220/Documents/tapdata4.7inch.csv -- file:///: Error Domain=NSCocoaErrorDomain Code=260 "The file “tapdata4.7inch.csv” couldn’t be opened because there is no such file." UserInfo={NSURL=file:/var/mobile/Containers/Data/Application/C8394065-40A2-42A4-9618-46F5E2BB4220/Documents/tapdata4.7inch.csv -- file:///, NSFilePath=/file:/var/mobile/Containers/Data/Application/C8394065-40A2-42A4-9618-46F5E2BB4220/Documents/tapdata4.7inch.csv, NSUnderlyingError=0x1c025aaf0 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}} 2018-07-31 11:51:30.080283+0900 LearningTask4.7inch[49744:3608404] [default] [ERROR] Failed to determine whether URL /file:/var/mobile/Containers/Data/Application/C8394065-40A2-42A4-9618-46F5E2BB4220/Documents/tapdata4.7inch.csv (n) is managed by a file provider 2018-07-31 11:51:31.516827+0900 LearningTask4.7inch[49744:3608404] [core] SLRemoteComposeViewController: (this may be harmless) viewServiceDidTerminateWithError: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}
razuma

2018/07/31 06:53

回答追記しておきましたー。
yuki84

2018/07/31 07:24

documentInteraction = UIDocumentInteractionController() documentInteraction.url = FilePath の部分の書き方が違ってました。無事送信できました。ありがとうございます!
razuma

2018/07/31 07:27

無事動いたようでなによりですー。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問