iOS共享扩展名 - 发送大型视频



因此,我正在尝试发送一个大型视频文件(超过100 MB(,并且每当我使用dataWithContentsOfURL访问视频文件时,扩展名终止。这可以与较小的文件一起工作。

我应该如何解决它?

if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie]){
            [itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeMovie options:nil completionHandler:urlHandler];
}


NSItemProviderCompletionHandler urlHandler = ^(NSURL *item, NSError *error) {
  if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeVideo] | [itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeMovie])
  {
        NSData *fileData = [NSData dataWithContentsOfURL:item]
        // ----> fileData WORKS for small files.
        // ----> for large files, extension QUITS - without any trace - and control does not proceed after this. This may be due to memory pressure?  
        [_shareExtensionActionsManager sendTextMessage:contentText attachmentData:fileData attachmentName:@"video-1" toChatEntity:_selectedItem completion:^(BOOL success) 
               {
                 [self.extensionContext completeRequestReturningItems:nil completionHandler:^(BOOL expired) {
    exit(0);
}];
         }];  
   } 
};

来自应用程序扩展文档:

用户在您的应用程序扩展程序中完成任务后倾向于立即返回到主机应用程序。如果该任务涉及潜在的冗长上传或下载,则需要确保在终止扩展后它可以完成。

在您的应用程序扩展名调用完成后,QuestReTurningItems:postemionhandler:要告诉主机应用程序已完成请求,系统可以随时终止您的扩展名。

您需要使用nsurlsession创建启动背景任务的URL会话。

如果您的扩展程序完成后未运行,则系统将在背景中启动您的包含应用,并在AppDelegate中调用application:handleEventsForBackgroundURLSession:completionHandler:

您还需要设置一个共享容器,您的扩展程序和包含应用程序都可以访问。为此,您需要使用NsurlsessionConfiguration的 sharedContainerIdentifier 属性来指定容器的标识符,以便您以后访问它。

>

以下是文档的示例,显示了您如何实现这一目标:

NSURLSession *mySession = [self configureMySession];
   NSURL *url = [NSURL URLWithString:@"http://www.example.com/LargeFile.zip"];
   NSURLSessionTask *myTask = [mySession downloadTaskWithURL:url];
   [myTask resume];
- (NSURLSession *) configureMySession {
    if (!mySession) {
        NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@“com.mycompany.myapp.backgroundsession”];
// To access the shared container you set up, use the sharedContainerIdentifier property on your configuration object.
config.sharedContainerIdentifier = @“com.mycompany.myappgroupidentifier”;
        mySession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
    }
    return mySession;
}

这是一个可能有助于进一步帮助的相关资源。

应用程序扩展可能没有此任务的内存能力。

运行应用程序扩展的内存限制明显低于前景应用程序上施加的内存限制。在两个平台上,系统都可以积极终止扩展,因为用户希望在主机应用程序中返回其主要目标。某些扩展可能比其他扩展范围较低:例如,小部件必须特别有效,因为用户可能同时打开多个小部件。

https://developer.apple.com/library/content/documentation/general/conpectual/coneptual/extensibilitypg/extensioncreation.htensioncreation.htmll#/apple_ref/apple_ref/doc/uid/tp40014214-ch5-sw1-sw1-sw1

最新更新