在本地模式与管理模式下编写 plist 时出现问题

发布于 2024-10-31 11:47:38 字数 874 浏览 6 评论 0原文

在我的应用程序中,我将用户首选项(适用于所有用户)存储到 plist 文件中, 它以捆绑形式附加。

问题是,它在管理模式下运行良好,但是当我运行应用程序时,它没有写入文件。我是否需要设置一些属性才能以本地模式写入 plist?或者根本不可能?

我编写该文件的代码如下:

-(void)SavePrefrence:(NSString *)fileName PrefrenceOption:(NSMutableDictionary *)pDict{
    NSString *filePath  = [[[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"] retain];

    NSDictionary *pTemp = [[NSDictionary alloc]initWithDictionary:pDict];
    bool bRet = [pTemp writeToFile:filePath atomically:YES]; 

    if(bRet==YES){
        NSLog(@"File Saved ");
    }
    else {
        NSLog(@"File not saved ");
    }


}

这是调用它的代码:

-(void)SaveListSettings:(NSMutableDictionary *)pListSettings{
    [ self SavePrefrence:@“MyList" PrefrenceOption:pListSettings];
    if(pListInfo)
        [pListInfo release];
    [self LoadListProfile];

}

In my application, I am storing user preferences (which are applicable for all users) into a plist file,
which is attached as a form of bundle.

The problem is, it runs fine in admin mode, but when I run the application, it's not writing the file. Do I need to set some attribute to write to the plist in local mode? Or is it not possible at all?

My code for writing the file is below:

-(void)SavePrefrence:(NSString *)fileName PrefrenceOption:(NSMutableDictionary *)pDict{
    NSString *filePath  = [[[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"] retain];

    NSDictionary *pTemp = [[NSDictionary alloc]initWithDictionary:pDict];
    bool bRet = [pTemp writeToFile:filePath atomically:YES]; 

    if(bRet==YES){
        NSLog(@"File Saved ");
    }
    else {
        NSLog(@"File not saved ");
    }


}

This is the code which calls it:

-(void)SaveListSettings:(NSMutableDictionary *)pListSettings{
    [ self SavePrefrence:@“MyList" PrefrenceOption:pListSettings];
    if(pListInfo)
        [pListInfo release];
    [self LoadListProfile];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丿*梦醉红颜 2024-11-07 11:47:38

默认情况下,应用程序包只能由所有者写入(如果它是通过拖到应用程序文件夹中安装的,那么这就是安装它的人;如果它是由 .pkg 安装的,那么它可能应该是 root)。如果我正确遵循的话,您这样做的方式会要求用户对应用程序的 Contents/Resources 文件夹具有写入权限,这是一个非常糟糕的主意(并不是说这是闻所未闻的 - 请参阅犹他大学的 有关“糟糕的应用程序”的文档作为示例)。实际上,在应用程序内部保存首选项无论如何都是一个坏主意。这就是各种 Library/Preferences 文件夹的用途(~/Library/Preferences 用于个人设置,/Library/Preferences 用于系统范围的设置。但是,当然,/Library/Preferences 只能由管理员写入(有很好的理由)我真的不知道有什么好方法来处理这个问题,因为非管理员可以修改系统范围的设置并不完全正常。

The application bundle will, by default, only be writable by the owner (if it's installed by dragging into the Applications folder, this'll be whoever installed it; if it's installed by a .pkg, it should probably be root). The way you're doing this, if I follow it properly, requires a user to have write access to the app's Contents/Resources folder, which is a really bad idea (not that it's unheard of -- see the University of Utah's documentation about "Poorly-Made Applications" for examples). Actually, saving preferences inside the application is a bad idea anyway; that's what the various Library/Preferences folders are for (~/Library/Preferences for personal settings, /Library/Preferences for system-wide settings. But, of course, /Library/Preferences is only writable by admins (for very good reasons). I don't really know of a good way to handle this, as system-wide settings modifiable by non-admins is not exactly normal.

看海 2024-11-07 11:47:38

您可以使用要求输入管理员密码的安装程序,然后创建“/Library/Application Support/MyApp”,然后使该世界可写,或在其中创建一个世界可写的子文件夹。现在,在非管理员帐户下运行的 MyApp 仍然可以写入此共享文件夹。

如果您不想使文件夹全局可写,则在捆绑包中包含一个帮助程序应用程序来进行写入,并使用要求管理员密码的安装程序使帮助程序 setuid root...

顺便说一句:这两个选项将不符合 Mac App Store 规则。也许你可以使用“/Users/Shared”,但我不知道 MAS 是否允许,而且无论如何它都远非标准。这会让你将它存储在网络服务器上......

You could use an installer which asks for an admin password and then create "/Library/Application Support/MyApp" and then either make this world writable, or make a sub-folder inside it which is world-writeable. Now MyApp running under a non-admin account can still write to this shared folder.

If you don't want to make the folder world-writeable then include a helper app to the bundle to do the writing and make the helper setuid root by using an installer which asks for an admin password...

BTW: Both of those options will fail Mac App Store rules. Maybe you can use '/Users/Shared', but I don't know if it is allowed by MAS, and anyway it is far from standard. Which would leave you with storing it on a web server...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文