2

目的:我想在運行時將iPhone應用程序首選項如何在運行時更改iphone應用程序首選項設置?

在Setting.Bundle我有標題=版本和默認值= 1.0

所以在運行時是否有可我不得不新版本更改iPhone應用程序首選項默認值。但我無法這樣做。它顯示,而不是1.1

我不知道什麼是錯在我的代碼1.0

代碼:

NSString *settingsBundle = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"bundle"]; 
if(!settingsBundle) { 
    NSLog(@"Could not find Settings.bundle"); 
    return; 
} 

NSDictionary *settings = [NSDictionary dictionaryWithContentsOfFile:[settingsBundle stringByAppendingPathComponent:@"Root.plist"]]; 
NSArray *preferences = [settings objectForKey:@"PreferenceSpecifiers"]; 

NSMutableDictionary *defaultsToRegister = [[NSMutableDictionary alloc] initWithCapacity:[preferences count]]; 
for(NSDictionary *prefSpecification in preferences) { 
    NSString *key = [prefSpecification objectForKey:@"Key"]; 
    if(key) { 

     if([key isEqualToString:@"name_preference"]) 
     { 
      NSString *a=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 
      [defaultsToRegister setObject:a forKey:key]; 
     } 
     else { 
      [defaultsToRegister setObject:[prefSpecification objectForKey:@"DefaultValue"] forKey:key]; 

     } 


    } 
} 

[[NSUserDefaults standardUserDefaults] registerDefaults:defaultsToRegister]; 

回答

2

你的包是隻讀的,所以你想寫的任何東西都需要外化。如果您願意,可以使用標準默認值來保留所有信息。這是非常簡單的:

NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]; 
NSString *build = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]; 
[standardDefaults setObject: build forKey:@"MyAppBuild"]; 
[standardDefaults setObject: version forKey:@"MyAppVersion"]; 
[standardDefaults synchronize]; 

此外,您可以在plist中複製到設備的文件系統和讀寫/來自:

閱讀:

+ (NSDictionary *) loadAppData { 
    NSString *finalPath; 

    // Load "defaults" plist 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *writablePath = [documentsPath stringByAppendingPathComponent:@"AppSettings.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    if([fileManager fileExistsAtPath: writablePath]){ 
     finalPath = writablePath; 
    } 
    else{ 
     NSString *path = [[NSBundle mainBundle] bundlePath]; 
     finalPath = [path stringByAppendingPathComponent:@"AppSettings.plist"]; 
    } 

    return [NSDictionary dictionaryWithContentsOfFile: finalPath]; 
} 

寫作:

+ (void) saveAppData: (NSDictionary *) appData { 

    // Load "defaults" plist 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsPath = [paths objectAtIndex:0]; 
    NSString *writablePath = [documentsPath stringByAppendingPathComponent:@"AppSettings.plist"]; 

    // Save it 
    [appData writeToFile: writablePath atomically: YES]; 
} 
0

我難以理解的問題,你的代碼是不容易跟隨(我從來不使用registerDefaults和它所做的工作的文檔有點模糊)

包是隻讀的。您無法在運行時更新軟件包。

「所以在運行時,如果有新版本可用,我必須更改iphone應用程序偏好默認值。」 - 不是真的。版本值將已在捆綁包中(取自app.plist)

我使用以下代碼行從包中讀取應用程序版本值: NSString * version = [[[NSBundle mainBundle] infoDictionary] objectForKey:(的NSString *)kCFBundleVersionKey];

相關問題