0

嘿大家。我正在开发一个在消费者第一次使用时显示模式的应用程序。我有一个 .plist 文件,一旦按下 Save 按钮,它将存储我请求的信息。我可以很好地读取 .plist 文件,当我运行我的保存方法时,它似乎可以正常工作,但 .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];
}

这一切都运行良好,没有打嗝,但文件永远不会更新。我希望它更新,以便我可以在整个应用程序中使用此信息并更新标志,以便在输入数据后它不会再次显示视图。如果您需要更多信息,请告诉我。提前致谢!

4

3 回答 3

2

您无法写入捆绑目录。

请改用 Caches 文件夹或 Documents 文件夹。有关如何修改 NSSearchPathForDirectoriesInDomains 或 NSHomeDirectory 函数的返回以查找这些文件夹的位置的信息,请参阅 iPhone 文档 - 放置数据的地方。

于 2009-12-18T19:13:38.840 回答
0

除此之外,您可以在模拟器中写入包,即使您不能在设备上 - 这导致我浪费了大量时间在模拟器和设备之间进行调试

于 2010-03-19T15:50:00.427 回答
0

如果这对任何人都有用,这是我用来做类似事情的一些代码。我使用它在第一次运行时复制一些示例文档,或者在新版本大于上一个版本时(如果有保证)可选地(注释部分)复制。

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);

    }
于 2010-04-10T05:21:42.620 回答