无法找到嵌入在应用程序捆绑包中的按需资源

问题描述

下载后,我使用URLForResource查找按需资源,该资源目前可以正常使用。

由于Apple在审核期间无法下载ODR的某些问题,我现在不得不将我的按需资源AssetPack临时嵌入到App Bundle中,将XCode“产品捆绑的嵌入式资产包”中的false更改为是的。

问题是这样做之后,当我使用相同的URLForResource方法时,它现在返回null。

NSURL *myDirectoryURL = [[NSBundle mainBundle] URLForResource:targetFolder withExtension:@""];

自从它们成为产品捆绑包的一部分以来,我不应该能够以这种方式找到它们吗?


更新: 来自非iOS开发人员在Cordova插件上工作的一些(丑陋的)工作代码。:)

    NSLog(@"[odr] calling nsbrr conditionallyBeginAccessingResourcesWithCompletionHandler..");
    @try
    {
      [odrRequest conditionallyBeginAccessingResourcesWithCompletionHandler:^(BOOL resourcesAvailable) {
        if (resourcesAvailable) {
          NSLog(@"[odr] already available (in-bundle) / downloaded.");
          @try
          {
            NSURL *myDirectoryURL = [[NSBundle mainBundle] URLForResource:targetFolder withExtension:@""];
            NSLog(@"[odr] Target directory URL: '%@'",myDirectoryURL);
            Nsstring *res = myDirectoryURL.absoluteString;
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsstring:res];
            NSLog(@"[odr] path found,returning it in callback: '%@'",res);
            [self.commandDelegate sendpluginResult:pluginResult callbackId:command.callbackId];
          }
          @catch(id anException) {
            Nsstring *errMsg = @"Error in path detection or calling callback (in already-downl-odr handler): ";
            errMsg = [errMsg stringByAppendingString:anException];
            NSLog(@"[odr] Exception: '%@'",errMsg);
            pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsstring:errMsg];
            [self.commandDelegate sendpluginResult:pluginResult callbackId:command.callbackId];
          }
        } else {
          NSLog(@"[odr] Starting to load (in-bundle) / download..");
          @try
          {
            [odrRequest beginAccessingResourcesWithCompletionHandler:^(NSError * _Nullable error) {
              if (error == nil) {
                NSLog(@"[odr] File load / download success.");
                @try
                {
                  NSURL *myDirectoryURL = [[NSBundle mainBundle] URLForResource:targetFolder withExtension:@""];
                  NSLog(@"[odr] Target directory URL: '%@'",myDirectoryURL);
                  Nsstring *res = myDirectoryURL.absoluteString;
                  pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsstring:res];
                  NSLog(@"[odr] path found,res);
                  [self.commandDelegate sendpluginResult:pluginResult callbackId:command.callbackId];
                }
                @catch(id anException) {
                // ...

解决方法

您通过嵌入按需资源来避免将服务器用作ODR下载源这一事实对您访问资源的方式没有任何影响。您还必须拥有一个NSBundleResourceRequest并调用beginAccessingResourcesWithCompletionHandler:并访问完成处理程序中的资源,就像您没有打开在产品捆绑包中嵌入资产包一样。

因此,无论产品包中嵌入资产包的价值如何,您的代码都应保持不变。如果在“将产品包中嵌入资产包”为“否”时,如果相同的代码有效,但为“是”时,如果相同的代码无效,则说明您的代码开头错误。