iPhone模拟器存储其数据的位置在哪里?

280

我有一个SQLite数据库,用于存储应用程序数据,我需要查看其中的内容以调试遇到的问题。但是iPhone模拟器通常将其数据存储在哪个位置呢?

21个回答

12

Xcode存储模拟器和运行时的位置

运行时

$ open ~/Library/Developer/CoreSimulator/Profiles/Runtimes

例如:iOS 13.0watchOS 6.0。这些占用了最多的空间。每个运行时可以高达约5GB。

设备

$ open ~/Library/Developer/CoreSimulator/Devices

例如:iPhone XriPhone 11 Pro Max。这些通常小于15 MB。

说明

模拟器分为运行时和设备两部分。如果运行$ xcrun simctl list,您可以看到概述,但如果您想找到这些模拟器的实际位置,请查看我展示的这些目录。

完全可以删除不支持的运行时。如果需要,稍后可以重新安装它们。


10

只需这样做:

NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSLog(@"%@", docDirPath);

然后您将会得到类似于这样的东西:

/Users/admin/Library/Developer/CoreSimulator/Devices/58B5B431-D2BB-46F1-AFF3-DFC789D189E8/data/Containers/Data/Application/6F3B985F-351E-468F-9CFD-BCBE217A25FB/Documents

前往该路径,您将无论使用哪个版本的XCode都能看到您的应用程序的文档文件夹。(在Finder中使用“前往文件夹...”命令并指定路径“~/library”)。

获取字符串路径的Swift版本:

let docDirPath =
NSSearchPathForDirectoriesInDomains(.documentDirectory,
                                    .userDomainMask, true).first
print(docDirPath)

和文件夹 URL:

let docDirUrl =
    FileManager.default.urls(for: .documentDirectory,
                             in: .userDomainMask).first
print(docDirUrl)

刚想发布几乎相同的答案。这种方法的优点是始终可以在任何版本的iOS模拟器上给出正确的路径。顺便说一下,如果您不想记录它,您还可以在调试器中复制url变量的值;) - Moose

7
在iOS 5中:
/Users/[用户名]/Library/Application Support/iPhone Simulator/5.0/Applications/[应用程序GUID]/

6

Xcode 4.6中存储路径如下:

/Users/[currentuser]/Library/Application Support/iPhone Simulator/6.1/Applications/

可使用以下代码来编程了解它:

  NSLog(@"path:%@",[[NSBundle mainBundle]bundlePath]);

2
Swift: print("路径:\(NSBundle.mainBundle().bundlePath)") - SwiftArchitect

4

iOS 8版本

要定位文档文件夹,您可以将文件写入文档文件夹中:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:@"Words.txt"];
NSString *content = @"Apple";
[content writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];

didFinishLaunchingWithOptions方法中可以这样做。

然后你可以打开终端并找到文件夹:

$ find ~/Library -name Words.txt

3

对于 macOS Catalina,我发现我的数据库在:

~/Library/Developer/CoreSimulator/Devices/{deviceId}/data/Containers/Data/Application/{applicationId}/Documents/my.db

为了获得applicationId,我只是按修改日期对文件夹进行排序,不过我相信有更好的方法来做到这一点。

2
我不和这个程序有任何关联,但是如果你想要在finder中打开其中的任何一个文件夹,SimPholders可以让这变得非常容易。 "最初的回答"

2

对于不经常使用Xcode的react-native用户,可以使用find。打开终端并通过数据库名称搜索。

$ find ~/Library/Developer -name 'myname.db'

如果您不知道确切的名称,可以使用通配符:

$ find ~/Library/Developer -name 'myname.*'

最初的回答。


2

如果在Lion操作系统中,任何人仍然遇到这个问题,可以阅读一篇非常好的文章,其中包含19种不同的提示,用于查看您的~/Library目录。文章由Dan Frakes撰写,您可以在此链接找到它。

请记住,模拟器的目录如下:

~/Library/Application Support/iPhone Simulator/User/


0

您可以尝试使用以下代码

NSString *fileName = @"Demo.pdf";
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
    NSLog(@"File path%@",pdfFileName);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接