-1

我在下载后使用URLForResource来查找按需资源,它目前可以正常工作。

由于 Apple 在审核期间无法下载 ODR 的一些问题,我现在必须暂时将我的 On-Demand Resources AssetPacks 嵌入到 App Bundle 中,将 XCode 中的“Embed Asset Packs In Product Bundle”从 false 更改为 true。

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

4

1 回答 1

0

您通过嵌入按需资源来回避将服务器用作 ODR 下载源这一事实不会改变访问它们的方式。您仍然必须有一个 NSBundleResourceRequest 并调用beginAccessingResourcesWithCompletionHandler:和访问完成处理程序中的资源,就像您没有打开在 Product Bundle 中嵌入资产包时所做的一样。

因此,您的代码应该保持不变,而不考虑 Embed Asset Packs In Product Bundle 的值。如果当 Embed Asset Packs In Product Bundle 为 NO 时相同的代码有效,但当它为 YES 时无效,则您的代码一开始是错误的。

于 2020-10-05T22:24:35.320 回答