Developer’s guide to On Demand Resources

Click To Download Full Source Code

Now a days, App size is increased as it contains more number of images in bundle. It becomes a bad user experience while downloading a large sized app from app store. To resolve this issue, Apple introduced On Demand Response feature from iOS 9 versions.

You can download resources when you need in app.

Ex. We develop games with multiple level and on each level there are different images. Here what happens is once user reaches next level then we have to download images only for that particular level & so on.

Let’s add new asset catalogs and add some images which we will load later in app.

Step: 1

Create new file

Screen1

Step: 2

then select asset catalog

Screen2

Now we have to specify the asset which we will use as On Demand Resource in app.

  1. Select Project -> Targets -> Navigate to Resource Tags
  2. Click on + button to add assets – > give any name as it’ call “tag” to identify during downloading the assets.
    Screen3
  3. click + & adds you newly added assets catalog.
    Screen4

It will also add tags in assets’ catalogs for each image which we added in resource tags.
Screen5

Now you have to use NSDataAsset request to load assets when you need in your app.

Let’s understand each line of code

  1. We declare NSBundleResourceRequest object global to use in class level.
  2. Create request with our tag which we given in resources tags tab.
  3. We can start downloading the asset packs for the specified tags by calling beginAccessingResourcesWithCompletionHandler(_:). This method will access all resources with the specified tags and will automatically start a download if needed.

Here we have to check whether the resources are already downloaded or not.So we have to call

A resource request can have a “loading priority” and “preservation priority”. These are numeric priorities between 0.0 and 1.0 that give priorities to the loading of the resources and life of the stored resources.

By lowering the loading priority you can free up memory and increase app speed, thus resulting in lower download speeds. In contrast you can increase the loading priority putting more strain on the user’s device but achieving greater download speeds.

Ex.

When a resource is no longer needed, it’s important to call “endAccessingResources” on the resource request to let the system know the resources can be purged from memory to free up space.

To test On Demand Resources

Go to the “Debug Navigator” and tapping on “Disk”. There’s a section found towards the bottom that will list your On Demand Resources via tag and show their size and status, such as “Downloaded” or “In Use”.

Screen6

Click To Download Full Source Code

Chirag Daslaniya

Chirag Daslaniya | Mobile App Developer

Chirag Daslaniya is a passionate Mobile Application Developer, especially for iOS at Yudiz Solutions Pvt. Ltd. – a leading iPhone App Development company. He is zealous about making easy-to-use and user-friendly applications using simple yet striking interfaces.

mm

Chirag Daslaniya Sr. iOS Developer

Chirag Daslaniya is a passionate Mobile Application Developer, especially for iOS at Yudiz Solutions Pvt. Ltd. – a leading iPhone App Development company. He is zealous about making easy-to-use and user-friendly applications using simple yet striking interfaces.

Comments are closed.

Top