Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@catlan
Last active August 7, 2019 21:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catlan/3ab8e0ac44b3e9170ae306b0ac9a967f to your computer and use it in GitHub Desktop.
Save catlan/3ab8e0ac44b3e9170ae306b0ac9a967f to your computer and use it in GitHub Desktop.
Pages.app like moveToURL:completionHandler: impl. NSIsRelatedItemType key in Info.plist is required
- (void)moveToURL:(NSURL *)url completionHandler:(void (^ __nullable)(NSError * __nullable))completionHandler
{
NSError *error = nil;
NSURL *adjustedURL = nil;
if (![[url pathExtension] isEqualToString:@"testextension"]) {
adjustedURL = [url URLByAppendingPathExtension:@"testextension"];
}
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
[coordinator coordinateWritingItemAtURL:url options:8 error:&error byAccessor:^(NSURL *newURL) {
NSLog(@"newURL %@", newURL);
BOOL isReachable = [newURL checkResourceIsReachableAndReturnError:NULL];
if (isReachable == NO) {
[[NSData data] writeToURL:newURL atomically:NO];
}
[coordinator itemAtURL:newURL willMoveToURL:adjustedURL];
if (isReachable == NO) {
[[NSFileManager defaultManager] removeItemAtURL:newURL error:NULL];
}
[coordinator itemAtURL:newURL didMoveToURL:adjustedURL];
}];
NSString *fileType = [self fileType];
[self saveToURL:adjustedURL ofType:fileType forSaveOperation:0x1 completionHandler:^(NSError * _Nullable errorOrNil) {
[coordinator coordinateWritingItemAtURL:url options:0x1 error:0x0 byAccessor:^(NSURL *newURL) {
NSError *remvoeError = nil;
if ([[NSFileManager defaultManager] removeItemAtURL:newURL error:&remvoeError] == NO) {
NSLog(@"remvoeError %@", error);
}
}];
}];
NSLog(@"error %@", error);
[self performAsynchronousFileAccessUsingBlock:^(void (^ _Nonnull fileAccessCompletionHandler)(void)) {
[super moveToURL:adjustedURL completionHandler:^(NSError *moveError) {
completionHandler(moveError);
fileAccessCompletionHandler();
}];
}];
}
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>testextension</string>
</array>
<key>CFBundleTypeIconFile</key>
<string></string>
<key>CFBundleTypeName</key>
<string>DocumentType</string>
<key>CFBundleTypeOSTypes</key>
<array>
<string>????</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>NSDocumentClass</key>
<string>Document</string>
<key>NSIsRelatedItemType</key>
<true/>
</dict>
</array>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment