Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Microsoft Office document directly from my own iOS app for in-place editing

I want to be able to edit in-place Office files hosted on my own server. By in-place I mean to open the file in Office, edit it, and get the changes sent back to my app/server directly.

I have partially achieved this by implementing a File Provider Extension to reveal my files in the Files app (iOS 11+), where it would automatically launch the Office app and get changes back to be uploaded to my server. I can also browse to my files from Office's Open menu and edit happens in-place.

I would to trigger this flow directly from my app. Any ideas?

Me and many have tried to follow Microsoft's Integrate with Office guide, but it only works for files hosted in FileShare or OneDrive. I want to edit files hosted in my own servers.

like image 286
Rivera Avatar asked Oct 17 '22 13:10

Rivera


1 Answers

I managed to make it work (iOS 11+):

First you need to have a working File Provider extension for your app. Which I had already.

Then added my extension's File Provider and File Provider Item classes to my main app. The only thing that didn't work out of the extension was trying to access the File Provider's documentStorageURL. I fixed this by implementing a getter inside my custom class:

- (NSURL *)documentStorageURL
{
    return NSFileProviderManager.defaultManager.documentStorageURL;
}

Then I could initialize it and use it from my main app:

// I created a singleton for my custom FileProvider class
NSURL* url = [FileProvider.sharedFileProvider URLForItemWithPersistentIdentifier:item.itemIdentifier];

// Calling providePlaceholderAtURL is key
[FileProvider.sharedFileProvider providePlaceholderAtURL:url completionHandler:^(NSError * _Nullable error)
 {
     if (error)
     {
         // ...
     }

     // This will download the requested file from my server
     [FileProvider.sharedFileProvider startProvidingItemAtURL:url completionHandler:^(NSError * _Nullable error)
      {
          [APP_DELEGATE hideHUD];
          
          if (error)
          {
              // ...
          }
          
          // Now I can use the url to start a UIDocumentInteractionController
          UIDocumentInteractionController * controller = [UIDocumentInteractionController interactionControllerWithURL:url];
          // Call presentOpenInMenuFromRect ...
      }];
}];

This makes Word (and other Office and apps) display "Open in Word" instead of "Copy to Word", allowing in-place editing directly from my app.

Using my extension's classes and calling providePlaceholderAtURL (which creates hidden (.myfile.docx.icloud files) magically makes Word believe that the file is coming from the Files app.

like image 156
Rivera Avatar answered Nov 15 '22 05:11

Rivera