2009-12-18 82 views
0

嘿大家。我正在開發一款應用程序,第一次顯示消費者使用它的模式。我有一個.plist文件,它將存儲我請求的信息,一旦保存按鈕被按下。我可以很好地從.plist文件中讀取,並且當我運行我的保存方法時,它會SEEMS正常工作,但.plist文件未更新。我不太確定這裏的問題。我顯示這樣的模式。iPhone應用程序沒有將值保存到.plist文件

- (void) getConsumerInfo { 
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"]; 
NSMutableDictionary *plistConsumerInfo = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

NSString *hasAppeared = [plistConsumerInfo objectForKey:@"HasAppeared"]; 

if(hasAppeared != kHasAppeared) { 
    ConsumerInfoViewController *tmpConsumerInfoVC = [[ConsumerInfoViewController alloc] 
       initWithNibName:@"ConsumerInfoView" bundle:nil]; 
    self.consumerInfoViewController = tmpConsumerInfoVC; 
    [self presentModalViewController:consumerInfoViewController animated:YES]; 
    [tmpConsumerInfoVC release]; 
} 
} 

這是在應用程序啓動時由第一視圖手機中的viewDidLoad方法調用的。在consumerInfoViewController中,我有輸入數據的文本框,當按下Save按鈕時,它調用這個方法。

- (IBAction)saveConsumerInfo:(id)sender { 
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"consumer.plist"]; 
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 

NSString *tmpDiversName = txtDiversName.text; 
NSString *tmpLicenseType = txtLicenseType.text; 
NSString *tmpLicenseNum = txtLicenseNumber.text; 
NSString *tmpHasAppeared = @"1"; 
NSString *tmpNumJumps = @"3"; 

[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpDiversName] forKey:@"ConsumerName"]; 
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseType] forKey:@"LicenseType"]; 
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpLicenseNum] forKey:@"LicenseNumb"]; 
[plistDict setValue:[[NSString alloc] initWithFormat:@"%@", tmpNumJumps] forKey:@"NumJumps"]; 
[plistDict setValue:[[NSString alloc] initWithFormat:@"%d", tmpHasAppeared] forKey:@"Show"]; 
[plistDict writeToFile:filePath atomically: YES]; 
NSLog([[NSString alloc] initWithContentsOfFile:filePath]); 
[self dismissModalViewControllerAnimated:YES]; 
} 

這一切都運行良好,沒有打嗝,但該文件是永遠不會更新。我希望它能夠更新,以便我可以在整個應用程序中使用這些信息並更新標記,以便在輸入數據後不會再顯示視圖。請讓我知道是否有任何您需要的信息。提前致謝!

回答

2

您無法寫入捆綁軟件目錄。

改爲使用Caches文件夾或Documents文件夾。有關如何修改NSSearchPathForDirectoriesInDomains或NSHomeDirectory函數的返回信息以查找這些文件夾的位置(用於放置數據的位置),請參閱iPhone文檔。

+0

太棒了!那是它,但我想我有另一個問題。我可以去查看文件系統上的plist文件,並且值在那裏,但它似乎創建了2個更多的值密鑰對。奇怪的。我希望它使用我導入到資源文件夾中的plist文件。我想我也必須弄清楚這一點。感謝你的回答! – Trent 2009-12-18 20:20:11

+1

如果是我,我會使用Caches或Documents文件夾中缺少plist文件作爲應用程序從未*成功完全啓動的FLAG,並且只在應用程序之後在Caches/Documents中創建plist文件和/或用戶*成功完全*完成初始的「第一個應用程序運行」工作流程。 – martinr 2009-12-18 20:29:24

0

只需添加到這一點,你可以寫在模擬器捆綁,即使你不能在設備上 - 這引起了我的憎惡埃及人,模擬器和設備之間的時間調試堆

0

如果這對任何人都有用處,這是我用來做類似的一些代碼。 I use it在第一次運行時複製一些示例文檔,或者在新版本大於最新版本(如果有保證的情況下)時複製(備註部分)複製。

BOOL firstrun() 
{ 
    CFStringRef firstRunKey = CFSTR("lastVersion"); 

    CFNumberFormatterRef formatter; 

    CFStringRef currentVersionString; 
    CFMutableStringRef currentVersionMutableString; 
    CFNumberRef currentVersion; 

    CFStringRef lastVersionRunString; 
// CFMutableStringRef lastVersionRunMutableString; 
// CFNumberRef lastVersionRun; 



    currentVersionString = CFBundleGetValueForInfoDictionaryKey(CFBundleGetMainBundle(), kCFBundleVersionKey); 
    currentVersionMutableString = CFStringCreateMutableCopy(NULL, 0, currentVersionString); 
    CFStringFindAndReplace(currentVersionMutableString, CFSTR("."), CFSTR(""), CFRangeMake(0, CFStringGetLength(currentVersionString)), 0); 
    formatter = CFNumberFormatterCreate(NULL, NULL, kCFNumberFormatterNoStyle); 
    currentVersion = CFNumberFormatterCreateNumberFromString(NULL, formatter, currentVersionMutableString, NULL, kCFNumberFormatterParseIntegersOnly); 

    lastVersionRunString = CFPreferencesCopyAppValue(firstRunKey, kCFPreferencesCurrentApplication); 

    if (lastVersionRunString == NULL) 
    { 
     // first run 
     NSFileManager *f = [NSFileManager defaultManager]; 
     NSBundle *b = [NSBundle mainBundle]; 
     NSError *e; 

     NSArray *xs = [NSArray arrayWithObjects: @"Welcome", 
           @"A Child's Garden of Verses", 
            @"Address to a Haggis", 
               @"Sonnet 18", nil]; 

     for (id x in xs) 
     { 
      BOOL s = [f copyItemAtPath: [b pathForResource: x ofType: @"txt"] 
        toPath: [DocumentManager pathForDocumentWithName: x] 
        error: &e]; 

      if (s == YES) 
      { 
       NSLog(@"Copied first run file %@ successfully.", x); 
      } 
      else { 
       NSLog(@"Failed to copy %@.", x); 
      } 
     } 
    } // else { 
     // The following is to enable support for new releases needing to show new information 
/* 
     lastVersionRunMutableString = CFStringCreateMutableCopy(NULL, 0, lastVersionRunString); 
     CFStringFindAndReplace(lastVersionRunMutableString, 
           CFSTR("."), 
           CFSTR(""), 
           CFRangeMake(0, CFStringGetLength(lastVersionRunMutableString)), 
           0); 
     lastVersionRun = CFNumberFormatterCreateNumberFromString(NULL, formatter, lastVersionRunMutableString, NULL, kCFNumberFormatterParseIntegersOnly); 

     CFComparisonResult cr = CFNumberCompare(lastVersionRun, currentVersion, NULL); 
*/  

// } 

    // Update last run version 
    CFPreferencesSetAppValue(firstRunKey, currentVersionString, kCFPreferencesCurrentApplication); 
    CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication); 

    CFRelease(currentVersionMutableString); 
    CFRelease(formatter); 
    CFRelease(currentVersion); 

    return (lastVersionRunString == NULL); 

    } 
相關問題