Skip to content

fix: iOS 18 error preview, black screen #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 102 additions & 98 deletions src/ios/FileOpener2.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,119 +33,123 @@ @implementation FileOpener2
NSString* callbackId = nil;

- (void) open: (CDVInvokedUrlCommand*)command {
callbackId = command.callbackId;
NSString *path = [command.arguments objectAtIndex:0];
NSString *contentType = [command.arguments objectAtIndex:1];
BOOL showPreview = YES;

if ([command.arguments count] >= 3) {
showPreview = [[command.arguments objectAtIndex:2] boolValue];
}

CDVViewController* cont = (CDVViewController*)[super viewController];
self.cdvViewController = cont;
NSString *uti = nil;

if ([contentType length] == 0) {
NSArray *dotParts = [path componentsSeparatedByString:@"."];
NSString *fileExt = [dotParts lastObject];

uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
} else {
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)contentType, NULL);
}

dispatch_async(dispatch_get_main_queue(), ^{
NSURL *fileURL = NULL;
NSString *decodedPath = [path stringByRemovingPercentEncoding];

if ([path isEqualToString:decodedPath]) {
NSLog(@"Path parameter not encoded. Building file URL encoding it...");
fileURL = [NSURL fileURLWithPath:[path stringByReplacingOccurrencesOfString:@"file://" withString:@""]];;
} else {
NSLog(@"Path parameter already encoded. Building file URL without encoding it...");
fileURL = [NSURL URLWithString:path];
}

localFile = fileURL.path;

NSLog(@"looking for file at %@", fileURL);
NSFileManager *fm = [NSFileManager defaultManager];

if (![fm fileExistsAtPath:localFile]) {
NSDictionary *jsonObj = @{@"status" : @"9",
@"message" : @"File does not exist"};
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
docController.UTI = uti;

//Opens the file preview
CGRect rect;

if ([command.arguments count] >= 4) {
NSArray *positionValues = [command.arguments objectAtIndex:3];

if (![positionValues isEqual:[NSNull null]] && [positionValues count] >= 2) {
rect = CGRectMake(0, 0, [[positionValues objectAtIndex:0] floatValue], [[positionValues objectAtIndex:1] floatValue]);
} else {
rect = CGRectMake(0, 0, 0, 0);
}
} else {
rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
}

BOOL wasOpened = NO;

if (showPreview) {
wasOpened = [docController presentPreviewAnimated: NO];
} else {
CDVViewController* cont = self.cdvViewController;
wasOpened = [docController presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
}

if (wasOpened) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
} else {
NSDictionary *jsonObj = [ [NSDictionary alloc]
initWithObjectsAndKeys :
@"9", @"status",
@"Could not handle UTI", @"message",
nil
];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}
});
callbackId = command.callbackId;
NSString *path = [command.arguments objectAtIndex:0];
NSString *contentType = [command.arguments objectAtIndex:1];
BOOL showPreview = YES;

if ([command.arguments count] >= 3) {
showPreview = [[command.arguments objectAtIndex:2] boolValue];
}

CDVViewController* cont = (CDVViewController*)[super viewController];
self.cdvViewController = cont;
NSString *uti = nil;

if ([contentType length] == 0) {
NSArray *dotParts = [path componentsSeparatedByString:@"."];
NSString *fileExt = [dotParts lastObject];

uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExt, NULL);
} else {
uti = (__bridge NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, (__bridge CFStringRef)contentType, NULL);
}

dispatch_async(dispatch_get_main_queue(), ^{
NSURL *fileURL = NULL;
NSString *decodedPath = [path stringByRemovingPercentEncoding];

if ([path isEqualToString:decodedPath]) {
NSLog(@"Path parameter not encoded. Building file URL encoding it...");
fileURL = [NSURL fileURLWithPath:[path stringByReplacingOccurrencesOfString:@"file://" withString:@""]];;
} else {
NSLog(@"Path parameter already encoded. Building file URL without encoding it...");
fileURL = [NSURL URLWithString:path];
}

localFile = fileURL.path;

NSLog(@"looking for file at %@", fileURL);
NSFileManager *fm = [NSFileManager defaultManager];

if (![fm fileExistsAtPath:localFile]) {
NSDictionary *jsonObj = @{@"status" : @"9",
@"message" : @"File does not exist"};
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return;
}

docController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
docController.delegate = self;
docController.UTI = uti;

//Opens the file preview
CGRect rect;

if ([command.arguments count] >= 4) {
NSArray *positionValues = [command.arguments objectAtIndex:3];

if (![positionValues isEqual:[NSNull null]] && [positionValues count] >= 2) {
rect = CGRectMake(0, 0, [[positionValues objectAtIndex:0] floatValue], [[positionValues objectAtIndex:1] floatValue]);
} else {
rect = CGRectMake(0, 0, 0, 0);
}
} else {
rect = CGRectMake(0, 0, cont.view.bounds.size.width, cont.view.bounds.size.height);
}

BOOL wasOpened = NO;

if (showPreview) {
wasOpened = true;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self->docController presentPreviewAnimated:YES];
});
} else {
CDVViewController* cont = self.cdvViewController;
wasOpened = [self->docController presentOpenInMenuFromRect:rect inView:cont.view animated:YES];
}


if (wasOpened) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: @""];
} else {
NSDictionary *jsonObj = [ [NSDictionary alloc]
initWithObjectsAndKeys :
@"9", @"status",
@"Could not handle UTI", @"message",
nil
];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:jsonObj];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}
});
}

@end

@implementation FileOpener2 (UIDocumentInteractionControllerDelegate)
- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}

- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller {
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}

- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
UIViewController *presentingViewController = self.viewController;
UIViewController *presentingViewController = self.viewController;

if (presentingViewController.view.window != [UIApplication sharedApplication].keyWindow) {
presentingViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
}
if (presentingViewController.view.window != [UIApplication sharedApplication].keyWindow) {
presentingViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
}

while (presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed]) {
presentingViewController = presentingViewController.presentedViewController;
}
while (presentingViewController.presentedViewController != nil && ![presentingViewController.presentedViewController isBeingDismissed]) {
presentingViewController = presentingViewController.presentedViewController;
}

return presentingViewController;
return presentingViewController;
}

@end