iCloud Driveにあるファイルを「作成中アプリにコピー」した際、正しくDocumentフォルダにコピーされるようにしたいです。
iOSでファイルを受信し、Documentフォルダにコピーするアプリを作成しています。
DropBoxなどのアプリからファイルを受信した場合、正しくコピーが行われるのですが、ファイルアプリのiCloud Driveからファイルを受信した際、処理に失敗します。
試しに受信したURLをNSFileManager.fileExistsAtPath()を使いチェックしたところ、falseが帰ってきました。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { // DropBoxアプリから受信した場合、「存在する」が返ってくる // URL例)file:///private/var/mobile/Containers/Data/Application/H5B5745R-K029-43K7-K62E-7680HEL8GH8E/Documents/Inbox/sample.txt // ファイルアプリ(iCloud Drive)から受信した場合、「存在しない」が返ってくる // URL例)/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/sample.txt NSLog(@"URLチェック: %@", [[NSFileManager defaultManager] fileExistsAtPath:url.path] ? @"存在する" : @"存在しない"); return YES; }
コピーを行っている処理は次の通りです。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options { NSArray* filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentDir = [filePaths firstObject]; NSString* toPath = [documentDir stringByAppendingPathComponent:@"sample.txt"]; NSURL* toURL = [NSURL fileURLWithPath:toPath]; if ([[NSFileManager defaultManager] fileExistsAtPath:toPath]) { [[NSFileManager defaultManager] removeItemAtPath:toPath error:nil]; } NSError* error = nil; if ([[NSFileManager defaultManager] copyItemAtURL:url toURL:toURL error:&error]) { NSLog(@"ファイルをコピーしました"); } else { NSLog(@"ファイルのコピーに失敗しました"); NSLog(@"%@", error.localizedDescription); } return YES; }
iCloud DriveからのURLではコピーに失敗し、次のエラーが返ってきました。
ファイル“sample.txt”を表示するためのアクセス権がないため、開けませんでした。
iCloud Driveから受信したURLでも正しくコピーできるようにするための手順をご教示頂けますと幸いです。
よろしくお願い申し上げます。
試したこと
シミュレータでは上記のコードで正しくコピーが行われました。
実機でのみ発生する問題のようです。
補足情報(FW/ツールのバージョンなど)
Xcode 10.0
iPhoneX iOS ver.12.0.1
あなたの回答
tips
プレビュー