无法写入AplicationSupport目录中的文件 [英] unable to write to a file in AplicationSupport directory

查看:69
本文介绍了无法写入AplicationSupport目录中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些值存储在我的i​​OS应用程序的应用程序支持目录中的plist中.但是写操作总是失败.我使用了来自iOS文档的样板代码.

I am trying to store some values in a plist in the Application support directory of my iOS app. But the write always fails. I used boiler plate code from iOS documentation.

在下面的代码中,我总是在执行writeToURL:atomically时遇到错误. I.E我总是在日志中看到无法写入plist",并且从未创建该文件.

In the code below I always hit an error while doing writeToURL:atomically. I.E I always get a "Failed to write to plist" in the log and the file is never created.

URL似乎已正确创建.这是我在下面的URL打印中看到的内容. 用于存储plist的URL为file:///var/mobile/Containers/Data/Application/9E26C447-7562-438E-A38A-E8F04C6DAFFE/Library/Application%20Support/com.apm.smartiothome.chatime/bcastSeqNum.plist

The URL appears to be created correctly. This is what I see in the URL print below. URL to store plist is file:///var/mobile/Containers/Data/Application/9E26C447-7562-438E-A38A-E8F04C6DAFFE/Library/Application%20Support/com.apm.smartiothome.chatime/bcastSeqNum.plist

我希望我能指出我做错了什么.

I would appreciate any pointers on what Im doing wrong.

 NSFileManager* sharedFM = [NSFileManager defaultManager];
NSArray* possibleURLs = [sharedFM URLsForDirectory:NSApplicationSupportDirectory
                                         inDomains:NSUserDomainMask];
NSURL* appSupportDir = nil;
NSURL* appDirectory = nil;

if ([possibleURLs count] >= 1) {
    // Use the first directory (if multiple are returned)
    appSupportDir = [possibleURLs objectAtIndex:0];
}

// If a valid app support directory exists, add the
// app's bundle ID to it to specify the final directory.
if (appSupportDir)
{
    NSString* appBundleID = [[NSBundle mainBundle] bundleIdentifier];
    appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
}
else
{
    DDLogError(@"Could not get pointer to app support directory") ;
}

self.bcastSeqNumFilePtr = [appSupportRootPtr URLByAppendingPathComponent:@"bcastSeqNum.plist" isDirectory:NO];

NSMutableArray *arrToReturn ;

arrToReturn = [NSMutableArray arrayWithContentsOfURL:self.bcastSeqNumFilePtr] ;

if(!arrToReturn)
{
    /*File does not exist...create one*/
    DDLogVerbose(@"Bcast seq num file does not exist.  Creating bcast plist file") ;
    NSMutableDictionary *dictToStore = [[NSMutableDictionary alloc] init] ;
    NSMutableArray *arrToReturn = [[NSMutableArray alloc] init] ;
    [arrToReturn addObject:[NSNumber numberWithInt:-1]] ;
    [dictToStore setObject:arrToReturn forKey:@"Bcast Seq Numbers"] ;
    if(![dictToStore writeToURL:self.bcastSeqNumFilePtr atomically:YES])
    {
        DDLogError(@"Failed to write to plist.") ;
    }
}
else
{
    DDLogVerbose(@"Bcase seq num file exists. returning seq number list from it %@",arrToReturn) ;
}

推荐答案

问题是writeToURL不会为您在路径中创建任何缺少的目录.如果您致电:

The problem is that writeToURL will not create any missing directories in the path for you. If you call:

NSError *error;
[sharedFM createDirectoryAtURL:appDirectory withIntermediateDirectories:NO attributes:nil error:&error];

这将创建目录.

一个问题,为什么要在路径中添加bundleIdentifier?您获得的应用程序支持目录已经是应用程序沙箱的私有目录.如果不添加此目录,则无需创建目录.

One question, why are you adding the bundleIdentifier into the path? The app support directory you get is already private to your app's sandbox. If you don't add this then you don't need to create the directory.

这篇关于无法写入AplicationSupport目录中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆