accessing and downloading on demand resources iOS9

时间:2015-07-28 22:46:54

标签: ios objective-c iphone ios9 on-demand-resources

I am trying to implement new iOS9 feature app thinning, I understood how tag an image and enable on demand resource in Xcode 7 but I don't understand how to implement NSBundleResourceRequest in my app, can someone help me, that would greatly appreciated

2 个答案:

答案 0 :(得分:5)

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.
            }
        }];
    }
}];

答案 1 :(得分:4)

首先,检查资源是否可用。否则下载它们。

以下是我使用的swift代码

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
            }
        }
    }
}

我还发现this guide非常有帮助。