找到你要的答案

Q:How can I unload NSBundle?

Q:我怎么能把NSBundle?

I have a scenario in which I have localization files to be downloaded from server. I download the file at start of application and store it in folder in bundle. Like if its English file I store it in bundle under folder 'en.lproj' with name Localizable.strings.

Now when file is downloaded, I load the bundle using bundleWithPath: method. Once it is loaded I show localization using method

NSLocalizedStringFromTableInBundle(key, @"Localizable", bundle, @"");

Till here it works fine. Now if the file is updated from server I again download the English file and I replace the old file with new one. But the application shows the localization from old file which was replaced. Can anyone please tell me if we can reload the bundle again so that the updates are reflected.

In the above scenario the application is not killed when file is updated. If I kill the application and restart it then the changes are reflected. But I want the changes to be reflected without killing the application.

Is there any method to unload the loaded NSBundle?

我有一个场景,我有从服务器下载的本地化文件。我在应用程序开始时下载文件并将其存储在文件夹中。如果我的英语文件存储在文件夹下的EN束。lproj '的名字localizable.strings。

Now when file is downloaded, I load the bundle using bundleWithPath: method. Once it is loaded I show localization using method

NSLocalizedStringFromTableInBundle(关键,@“本地化”,捆绑,“”“);

Till here it works fine. Now if the file is updated from server I again download the English file and I replace the old file with new one. But the application shows the localization from old file which was replaced. Can anyone please tell me if we can reload the bundle again so that the updates are reflected.

In the above scenario the application is not killed when file is updated. If I kill the application and restart it then the changes are reflected. But I want the changes to be reflected without killing the application.

有什么方法来卸载NSBundle?

answer1: 回答1:

Got the answer..

This code helped. This is used to flush the loaded bundle from cache.

BOOL FlushBundleCache(NSBundle *prefBundle) {
    // Before calling the function, we need to check if it exists
    // since it was weak-linked.
    if (_CFBundleFlushBundleCaches != NULL) {
        NSLog(@"Flushing bundle cache with _CFBundleFlushBundleCaches");
        CFBundleRef cfBundle =
        CFBundleCreate(nil, (CFURLRef)[prefBundle bundleURL]);
        _CFBundleFlushBundleCaches(cfBundle);
        CFRelease(cfBundle);
        return YES; // Success
    }
    return NO; // Not available
}

得到了答案..

此代码帮助。这是用来冲洗加载束从缓存。

BOOL FlushBundleCache(NSBundle *prefBundle) {
    // Before calling the function, we need to check if it exists
    // since it was weak-linked.
    if (_CFBundleFlushBundleCaches != NULL) {
        NSLog(@"Flushing bundle cache with _CFBundleFlushBundleCaches");
        CFBundleRef cfBundle =
        CFBundleCreate(nil, (CFURLRef)[prefBundle bundleURL]);
        _CFBundleFlushBundleCaches(cfBundle);
        CFRelease(cfBundle);
        return YES; // Success
    }
    return NO; // Not available
}
ios  objective-c  localization  nsbundle