"Open In" with a dynamically generated file does not work in iOS 8

I use this code to save some PDF data to a file, send it to another application using the Open In menu, and then delete the file when it is done:

- (void)openIn:(NSData *)fileData {
    // save the PDF data to a temporary file
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
    NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
    BOOL result = [fileData writeToFile:filePath atomically:TRUE];
    if (result) {
        NSURL *URL = [NSURL fileURLWithPath:filePath];
        UIDocumentInteractionController *controller = [[UIDocumentInteractionController interactionControllerWithURL:URL] retain];
        controller.delegate = self;
        [controller presentOpenInMenuFromBarButtonItem:self.openInButton animated:TRUE];
    }
}

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
    // when the document interaction controller finishes, delete the temporary file
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
    NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}

This works fine before iOS 8. Now the file is created, and I can verify that it contains the correct content, the "Open" menu appears, I can select the application, and the delegate method launches and clears the file, But instead of switching iOS to the selected application and copying file in it, as before, the "Open" menu just closes when I select an application, and the file is not copied.

, UIDocumentInteractionController . , fileData, . - iOS 8 , UIDocumentInteractionController .

- , ?

+4
2

, iOS 8. DidDismissOpenInMenu , , . , , , . , , ; -, , , .

, , , DocumentInteractionController, , , :

- (void)openIn:(NSData *)fileData {
    // save the PDF data to a temporary file
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
    NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
    BOOL result = [fileData writeToFile:filePath atomically:TRUE];
    if (result) {
        self.sendingFile = FALSE;
        NSURL *URL = [NSURL fileURLWithPath:filePath];
        UIDocumentInteractionController *controller = [[UIDocumentInteractionController interactionControllerWithURL:URL] retain];
        controller.delegate = self;
        [controller presentOpenInMenuFromBarButtonItem:self.openInButton animated:TRUE];
    }
}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application {
    // the user chose to send the file, so we shouldn't clean it up until that done
    self.sendingFile = TRUE;
}

- (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller {
    if (!self.sendingFile) {
        // the user didn't choose to send the file, so we can clean it up now
        [self openInCleanup];
    }
}

- (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application {
    // the user chose to send the file, and the sending is finished, so we can clean it up now
    [self openInCleanup];
    self.sendingFile = FALSE;
}

- (void)openInCleanup {
    // delete the temporary file
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf", self.name];
    NSString *filePath = [NSString stringWithFormat:@"%@/Documents/%@", NSHomeDirectory(), fileName];
    [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}

iOS 11

iOS 11 , , , - , , . iOS 11 , - , . Documents openInCleanup , tmp tmp . iOS. openInCleanup, tmp applicationDidFinishLaunching:

// clear the tmp directory, which will contain any files saved for Open In
NSString *tmpDirectory = [NSString stringWithFormat:@"%@/tmp", NSHomeDirectory()];
NSArray *tmpFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:tmpDirectory error:NULL];
for (NSString *tmpFile in tmpFiles) {
    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", tmpDirectory, tmpFile] error:NULL];
}
+7

:
, iOS 8, Mail.app. Dropbox ..

, - :
Controller :

interactionController.annotation = @"Some text"

Dropbox . - . .

+1

All Articles