iOS导入字典从一个plist到另一个plist

时间:2013-08-01 08:57:34

标签: iphone ios objective-c email plist

我正在尝试让我的应用从电子邮件中导入设置。这将起作用的方式是用户将向自己发送settings.properties文件,该文件实际上是plist,看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Viewer</key>
    <array>
        <string>username</string>
        <string>password</string>
        <string>http://www.example.com</string>
        <string>/example</string>
        <string>urlparam=whatever</string>
    </array>
</dict>

我可以使用NSLog正确打开并阅读文件,但是我需要将dictionary导入到我的应用主Data.plist中,而不会覆盖已存在的内容,然后从中删除导入的plist Documents / Inbox文件夹。

任何线索都会很棒:)

//修改

我已根据以下评论更新了我的方法。它现在只有在我的apps文档目录中已经存在Data.plist文件时才有效。所以我需要创建它,如果它不存在,然后添加我的.properties文件..

这是我目前正在使用的代码。

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
if (url){

        NSDictionary *openedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];

        // get paths from root direcory
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        // get documents path
        NSString *documentsPath = [paths objectAtIndex:0];
        // get the path to our Data/plist file
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
        NSDictionary *originalDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

        NSMutableDictionary *newDictionary = [originalDictionary mutableCopy];
        for (NSString *key in openedDictionary) {
            if (!newDictionary[key]) {
                newDictionary[key] = openedDictionary[key];
            }
        }

        [newDictionary writeToFile:plistPath atomically:YES];

    }
    NSError *error = nil;
    if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
        NSLog(@"error while deleting: %@", [error localizedDescription]);
    }
    return YES;
}

2 个答案:

答案 0 :(得分:2)

首先,如果您的数据结构错误。你有字典而不是数组用于设置,它更具可读性,你可以更简单地解析它而不会出错。

我建议您将文件编辑到此

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>username</key>
    <string>username_value</string>
    <key>password</key>    
    <string>password_value</string>
    <key>url</key>
    <string>www.example.com</string>
    <key>url2</key>
    <string>/example</string>
    <key>urlparam</key>
    <string>urlparam=whatever</string>
</dict>

将plist转换为字典非常简单。

NSData* plistData = [source dataUsingEncoding:NSUTF8StringEncoding];
NSString *error;
NSPropertyListFormat format;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData:plistData mutabilityOption:NSPropertyListImmutable format:&format errorDescription:&error];

您应该阅读有关Serializing a Property List

的更多信息

转换plist后,比较这些值并进行适当的更改并将字典写入plist文件。

这是代码段:

-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    if (url){

        NSDictionary *openedDictionary = [NSDictionary dictionaryWithContentsOfURL:url];

        // get paths from root direcory
        NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
        // get documents path
        NSString *documentsPath = [paths objectAtIndex:0];
        // get the path to our Data/plist file
        NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
        NSDictionary *originalDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];

        NSMutableDictionary *newDictionary = [originalDictionary mutableCopy];
        for (NSString *key in openedDictionary) {
            if (!newDictionary[key]) {
                newDictionary[key] = openedDictionary[key];
            }
        }

        [newDictionary writeToFile:plistPath atomically:YES];

    }
    NSError *error = nil;
    if (![[NSFileManager defaultManager] removeItemAtURL:url error:&error]) {
        NSLog(@"error while deleting: %@", [error localizedDescription]);
    }
    return YES;
}

答案 1 :(得分:1)

首先尝试将主数据plist读入临时可变变量。然后将从导入的plist文件读取的数据附加到临时变量。现在将临时变量的数据写入主数据plist,覆盖整个数据。

相关问题