ios - 将文件传输到物理 iOS 设备

标签 ios xcode ipad

我已经设置了一个配置文件来在连接的物理设备上调试我的应用程序(通过 Xcode)。

问题是这个应用程序需要某些支持文件。对于 Mac 上的模拟器,我只需导航到模拟器目录下应用程序的 Documents 目录并将文件放在那里即可。

有没有办法将这些相同的文件传输到物理设备上?

最佳答案

将文件放置在项目文件结构中。 因此,它们会被复制到您的 App Bundle 中,并且可以通过文档目录访问。

要将文件正确添加到您的 iOS 项目:

  1. 右键单击左侧文件列表顶部的项目图标。
  2. 选择Add files to <YourProjectName>
  3. 选择要包含的文件夹/文件,然后单击“添加”。
    • 不要忘记选择正确的target从给定列表中选择它。请参阅屏幕截图。
  4. 如果您的资源不是单个文件而是目录结构,并且您希望复制所有目录树,请记住选择添加的文件夹:创建组

添加弹出窗口的文件在 XCode 6.x 中如下所示: enter image description here

构建目标后,打开 bundle ,您的目录结构将完整地存在于其中。不仅如此,这些文件还可以通过 iOS SDK 访问,如下所示。

因此,您可能需要将它们复制到应用程序内的文档/库目录,因为您可能想在应用程序中访问它们。

使用以下代码来复制它们。

// Check if the file has already been saved to the users phone, if not then copy it over
BOOL success;
NSString *fileName = @"test.jpg";
NSString *LIBRARY_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSString *filePath = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];
NSLog(@"%@",filePath);

// Create a FileManager object, we will use this to check the status
// of the file and to copy it over if required
NSFileManager *fileManager = [NSFileManager defaultManager];

// Check if the file has already been created in the users filesystem
success = [fileManager fileExistsAtPath:filePath];

// If the file already exists then return without doing anything
if(success) return;

// Else,
NSLog(@"FILE WASN'T THERE! SO GONNA COPY IT!");

// then proceed to copy the file from the application to the users filesystem
// Get the path to the files in the application package
NSString *filePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];

// Copy the file from the package to the users filesystem
[fileManager copyItemAtPath:filePathFromApp toPath:filePath error:nil];

希望您能够清楚上述代码示例。

因此,每当您想在应用程序中访问该文件时,您都可以通过获取该文件的路径来引用该文件,如下所示:

    NSString *sqliteDB = [LIBRARY_DIR_PATH stringByAppendingPathComponent:fileName];

注意:在任何情况下,如果您需要将文件复制到 Documents用户应用程序安装位置中的目录,替换 LIBRARY_DIR_PATH具有以下内容:

NSString *DOCUMENTS_DIR_PATH = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

希望这个回答对您有帮助!

干杯!

关于ios - 将文件传输到物理 iOS 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32448213/

相关文章:

ios - 更新 Xcode 后,我的 Swift 3 项目运行不正确但构建成功

ios - 我是否必须注册新的 Bundle ID 才能将新应用程序上传到 iTune Connect?

ios - swift 3 : Defining var labels: [UILabel] fails

css - 远程调试时的 Safari 开发工具问题

ios - "Cannot convert value of type '(字符串)-> bool 值 ' to expected argument "

c# - iOS 模拟器以空蓝屏启动

iphone - 如何通过我的应用程序为所有不同的 UITableView 提供相同的外观?

ios - SceneKit – 获取节点的高度

ios - 如何选择不同的细节 View Controller ?

ipad - 在为 iPad 编译时是否设置了特定的 Xcode 编译器标志?