直接从我自己的iOS应用程序打开Microsoft Office文档以进行就地编辑

时间:2018-09-07 13:48:04

标签: ios office365 ms-office edit-in-place fileprovider-extension

我希望能够编辑自己服务器上托管的 Office文件。就地而言,我的意思是在Office中打开文件,对其进行编辑,然后将更改直接发送回我的应用程序/服务器。

我通过实现File Provider Extension在“文件”应用程序(iOS 11+)中显示我的文件来部分实现了这一目标,该程序将自动启动Office应用程序并获取更改并上传回我的服务器。我还可以从Office的“打开”菜单浏览到我的文件,然后就地进行编辑。

我想直接从我的应用触发此流程。有什么想法吗?

Me和many尝试遵循Microsoft的Integrate with Office指南,但仅适用于FileShare或OneDrive中托管的文件。我要编辑托管在自己服务器中的文件。

1 个答案:

答案 0 :(得分:1)

我设法使其能够正常运行(iOS 11 +):

首先,您需要为您的应用提供有效的文件提供程序扩展。我已经有了。

然后将扩展程序的File Provider和File Provider Item类添加到我的主应用程序。 在扩展中无法正常工作的唯一事情是尝试访问文件提供程序的documentStorageURL。我通过在自定义类中实现getter来解决此问题:

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

然后我可以将其初始化并从主应用程序中使用它:

// 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 ...
      }];
}];

这使Word(以及其他Office和应用程序)显示“在Word中打开”而不是“复制到Word”,从而允许直接从我的应用程序进行就地编辑。

神奇地使用扩展程序的类并调用providePlaceholderAtURL(它会创建隐藏的.myfile.docx.icloud文件)使Word相信该文件来自“文件”应用。

相关问题