簡體   English   中英

將文件傳輸到物理iOS設備

[英]Transfer file to physical iOS device

我已經設置了配置文件,以在連接的物理設備上調試應用程序(通過Xcode)。

問題是此應用需要某些支持文件。 使用Mac上的模擬器,我只需導航到該應用程序的“ Documents目錄(在模擬器目錄下)並將文件放在此處即可。

有沒有辦法將這些相同的文件放到物理設備上?

將文件放在項目文件結構中。 因此,它們將被復制到您的App Bundle中,並且可以通過documents目錄獲得。

要將文件正確添加到您的iOS項目:

  1. 右鍵單擊左側文件列表頂部的項目圖標。
  2. 選擇Add files to <YourProjectName>
  3. 選擇要包括的文件夾/文件,然后單擊添加。
    • 不要忘了從給定列表中選擇正確的target 請參考屏幕截圖。
  4. 如果您的資源不是單個文件而是目錄結構,並且您希望復制所有目錄樹,則請記住選擇“ 添加的文件夾”:創建組

在XCode 6.x中,添加彈出窗口的文件如下所示: 在此處輸入圖片說明

構建目標后,打開捆綁包,目錄結構將完全保留在其中。 不僅如此,可以通過以下iOS SDK訪問這些文件。

因此,您可能需要將它們復制到應用程序內的documents / library目錄,因為您可能想在應用程序內訪問它們。

使用以下代碼復制它們。

// 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使用以下內容替換LIBRARY_DIR_PATH

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

希望這個答案對您有幫助!

干杯!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM