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

Unable to find On-Demand Resources embedded in App bundle

下载后我使用URLForResource查找点播资源,目前可以正常使用。

由于 Apple 在审查期间无法下载 ODR 的一些问题,我现在不得不暂时将我的按需资源 AssetPacks 嵌入到 App Bundle 中,更改为 XCode“嵌入资产包在产品包”从假到真。

问题是,当我使用相同的 URLForResource 方法时,现在 returns 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, returning it in callback: '%@'", res);
                  [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
                }
                @catch(id anException) {
                // ...

您通过嵌入 on-demand 资源来回避使用服务器作为 ODR 下载源这一事实改变了您访问它们的方式 。您仍然 必须有一个 NSBundleResourceRequest 并调用 beginAccessingResourcesWithCompletionHandler: 并访问完成处理程序中的资源,就像您没有打开产品中嵌入资产包时所做的那样捆绑。

所以您的代码应该保持不变,而不考虑在产品包中嵌入资产包的价值。如果相同的代码在 Embed Asset Packs In Product Bundle 为 NO 时有效,但在为 YES 时无效,则您的代码开头是错误的。