Here is the basic problem: I needed a view loading mechanism that attempts to create a view from a downloaded NIB in Documents, then falls back to the main bundle, if the view could not be created.
I've gone through a lot of research and trial and error, before getting this to work, so I wanted to share the solution with others.
Here are the steps:
1) Create NIBs in the main bundle in the normal fashion. I recommend using a file group that points to a folder, to keep all the assets together that will be be used for the downloaded bundle. Let’s call it NIB_Resources.
To create a NIB under a folder in the Project Navigator:
2) Add a target for the asset bundle.
3) Add the assets to the asset bundle.
4) Build the asset bundle.
5) Zip the asset bundle
6) Upload the asset bundle to a location that you have download access to.
7) Download the zipped asset bundle:
The code below is tucked away in convenience functions, in a convenience file that handles a lot of low-level File System operations. The FS prefix refers to File System.
FSDownloadTempFileWithURLString
can be called from a secondary thread, before returning to the main thread.I use the
NSData
synchronous method,initWithContentsOfURL:
, because the call is likely to be made from a secondary thread. The basic strategy is to download the zip file to a temporary location (the Caches directory is usually a good candidate for this purpose), before doing any necessary preparations and unzipping the file to the Documents directory. The approach of defining inline static operations in a header file was adopted from Apple.8) Unzip the download to the Documents directory, using
SSZipArchive
or something similar:9) Finally, try to load a view from a NIB file in the bundle in the Documents directory, and fall back to the main bundle.
The
FSResourceNib
operation below can be called from a view controller that is trying to load a view from a Nib, like so: