将XML文件传输到iOS应用程序

时间:2016-04-07 17:44:31

标签: ios macos

我在开发中有一个需要XML文件的应用。该应用程序在模拟器中运行良好。但是,它在实际设备上失败,因为没有文件。如何将该文件作为包的一部分?

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryPath = [directoryPaths objectAtIndex:0];
NSString *path = [NSString stringWithFormat:@"%@/games.xml", directoryPath];
NSLog(@"%@", path);
if ([fm fileExistsAtPath:path]) {
    self.games = [[NSArray alloc] initWithContentsOfFile:path];
    NSLog(@"Found %lu games on device.", (unsigned long)self.games.count);
} else {
    NSData *data = [[NSData alloc] initWithContentsOfFile:@"/Users/philHuffman/dev/Games.xml"];
    [fm createFileAtPath:path contents:data attributes:nil];
    self.games = [[NSArray alloc] initWithContentsOfFile:path];
    NSLog(@"Found %lu games in file on the mac.", (unsigned long)self.games.count);
}

1 个答案:

答案 0 :(得分:0)

您可以在项目资源管理器中拖动xml文件,当您运行/安装它时它将成为程序包的一部分