使用NSApplicationSupportDirectory和NSDocumentDirectory

时间:2013-08-05 13:31:43

标签: pdf nsuserdefaults nsfilemanager ios6.1 nsdocumentdirectory

这是交易...我正在创建一个应用程序(来自我的另一个应用程序),但我正在改为仅使用NSDocumentDirectory,这显然允许用户查看所有用户文件,仅查看一些文件...即用户创建的PDF。 我有它的工作......但是,iTunes的FileSharing / Documents窗口中没有显示任何内容。

首先是在持久性模型中调用NSApplicationSupportDirectory的方法......

+(NSString *)getDocumentpath
{
    //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    return [paths objectAtIndex:0];
}

+(NSString *) documentsDirectoryPath
{
    //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    return documentsDirectory;
}

+(void) copyResourceFileToDocumentsDirectory: (NSString *) fileName
{
    //NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:fileName];

    NSFileManager * fileManager = [NSFileManager defaultManager];
    BOOL succeeded = [fileManager fileExistsAtPath:writablePath];
    NSError *error;

    //If file is not in the documents directory then only write
    if (!succeeded)
    {
        NSString *newPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
        succeeded = [fileManager copyItemAtPath:newPath toPath:writablePath error:&error];

        if (succeeded == FALSE) {
            NSLog(@"%@ : copy failed", fileName);
        } else {
            NSLog(@"%@ : copy success", fileName);
        }
    } else {
            NSLog(@"%@ : already exists", fileName);
    }
}

这是将PDF保存到NSDocumentDirectory的方法,该方法尚未从其他应用程序更改...

- (NSString*)saveJournalToPDF:(UIView*)journal andName:(NSString*)name
{
    NSString* fileName = [NSString stringWithFormat:@"%@.pdf",name];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:fileName];

    ... // the rest of the data strings for creating the PDF

}

我的问题:如何在不暴露其他数据文件的情况下让用户看到PDF?现在,它似乎全是或全无!

1 个答案:

答案 0 :(得分:0)

我忽略了一个小细节......将应用程序设置为实际共享文件!该应用程序的info.plist没有“应用程序支持iTunes文件共享”设置为YES。 (UIFileSharingEnabled)。这么简单的修复!!!