Last active
May 10, 2024 15:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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(); | |
}]; | |
}]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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