accessing and downloading on demand resources iOS9

若如初见. 提交于 2019-12-01 19:32:45

First, check if the resources are available. Else download them.

Here is the swift code I use

let tags = NSSet(array: ["tag1","tag2"])
let resourceRequest = NSBundleResourceRequest(tags: tags as! Set<String>)
resourceRequest.conditionallyBeginAccessingResourcesWithCompletionHandler {(resourcesAvailable: Bool) -> Void in
    if resourcesAvailable {
        // Do something with the resources
    } else {
        resourceRequest.beginAccessingResourcesWithCompletionHandler {(err: NSError?) -> Void in
            if let error = err {
                print("Error: \(error)")
            } else {
                // Do something with the resources
            }
        }
    }
}

I also found this guide very helpful.

Most of information is available in Apple documentation.

Basically you need make this:

NSSet *tagsSet = [NSSet setWithObjects:@"resourceTag1", @"resourceTag2", nil];
NSBundleResourceRequest *request = [[NSBundleResourceRequest alloc] initWithTags:tagsSet];
[request conditionallyBeginAccessingResourcesWithCompletionHandler:^(BOOL resourcesAvailable) {
    if (resourcesAvailable) {
        // Start using resources.
    } else {
        [request beginAccessingResourcesWithCompletionHandler:^(NSError * _Nullable error) {
            if (error == nil) {
                // Start using resources.
            }
        }];
    }
}];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!