文件未在iTunes中显示w /文件共享

时间:2014-09-17 15:03:25

标签: ios objective-c file-io nsstring

我正在尝试将NSString写入文件,然后在iTunes中查看该文件。我将Application supports iTunes file sharing密钥设置为YES,我可以在iTunes中看到文件共享。但是文件永远不会出现。

以下是我编写文件的方式:

  NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateStyle:NSDateFormatterFullStyle];

        NSString *fileName = [dateFormatter stringFromDate:[NSDate date]];
        fileName = [fileName stringByReplacingOccurrencesOfString:@" " withString:@"_"];
        fileName = [fileName stringByReplacingOccurrencesOfString:@"," withString:@""];

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
        NSString* rootPath = paths[0];
        NSString *path = [rootPath stringByAppendingPathComponent:fileName];
        path = [path stringByAppendingPathExtension:@"txt"];

        NSURL *url = [NSURL fileURLWithPath:path];

        NSString *writeString = @"Hello world";

        NSError *error;
        BOOL ok = [writeString writeToURL:url atomically:YES encoding:NSUnicodeStringEncoding error:&error];

        if (!ok) {
            NSLog(@"%@", error);
        }
        else {
            NSLog(@"File saved to: '%@'", path);
        }

这是文件位置:

'/var/mobile/Applications/066FD6CD-0E5F-4701-9344-46258ABF0E97/Library/Wednesday_September_17_2014.txt'

并且好的iTunes:

enter image description here

1 个答案:

答案 0 :(得分:1)

您希望NSDocumentDirectory不是NSLibraryDirectory

先生先睡个好觉! ;)