iosswiftxcodebundleon-demand-resources

iOS On Demand resources download fine - but they cannot be found in the main bundle by the app at the point I need to use them


I've gone through the documentation and all the questions on SO related to On Demand assets in iOS and can't find an answer so I'm hoping you can help. I have an app that was able to access files fine (specifically video mp4 files that were in a group/folder with the name "GameVideos"). The code I used to do this is:

guard let url = Bundle.main.url(forResource: videoName, withExtension: "mp4") else {
   print("\(videoName) not found")
   return
}

let item = AVPlayerItem(url: url)
self.player1 = AVPlayer(playerItem: item)
self.videoNode1 = SKVideoNode(avPlayer: self.player1)

Now that I've switched to making the video 'On demand' I see the 'not found' message get printed. When it's not listed as an on demand resource, I didn't need to reference the subdirectory - it worked fine without it (though I tried adding it in just to see). I'm also happy that I've not misspelt the file name as that also worked fine.

When creating the On Demand resource - I've set this up with tags and use the following code to call the resources (this code is located in the GameViewController.swift and inside the "viewDidLoad" override - I'm building a SpriteKit game so there is only one view controller - all the other logic is handled in the SKScenes.):

let tags = NSSet(array: [
            "OD2",
            "OD3",
            "OD4",
            "OD5",
            "OD6",
            "OD7",
            "OD8",
        ])

  let resourceRequest = NSBundleResourceRequest(tags: tags as! Set<String>)

  resourceRequest.conditionallyBeginAccessingResources(completionHandler: {(resourcesAvailable: Bool) in
            if !resourcesAvailable  { // if resources not available - start download
                resourceRequest.beginAccessingResources(completionHandler: {(err: Error?) in
                    if let error = err {
                        print("Error getting On Demand Resources: \(error)")
                    }
                })
            }
        })

This code seems to work and I can see the On Demand packages being downloaded successfully in the Disk monitor on Xcode.

Other things I've tried: I've double checked the target membership of the video and it is correctly assigned.
I've tried calling the Bundle.Main.url with a subdirectory of the folder listed (this didn't work either) I've tried renaming the folder directory (as it had a space in the name) I've tried cleaning the build many times

None of the above has worked... what am I doing wrong?? Any guesses or pointers would be hugely appreciated.


Solution

  • So after lots of trial and error - I placed the variable 'resourceRequest' in the global scope and it works - the message on the XCode disk changes from "downloaded" to "in use". As you can see in my code, I had previously defined 'resourceRequest' within the GameViewController. I had thought this would work for a SpriteKit game as the GameViewController loads and then it controls all the scenes (so I didn't think it went out of scope). Someone smarter than me will hopefully say why this works - but for the minute - I'm just happy I can move on and launch my app (Apple's documentation on this stuff is terrible)