[英]Transfer file to physical iOS device
我已經設置了配置文件,以在連接的物理設備上調試應用程序(通過Xcode)。
問題是此應用需要某些支持文件。 使用Mac上的模擬器,我只需導航到該應用程序的“ Documents
目錄(在模擬器目錄下)並將文件放在此處即可。
有沒有辦法將這些相同的文件放到物理設備上?
將文件放在項目文件結構中。 因此,它們將被復制到您的App Bundle中,並且可以通過documents目錄獲得。
要將文件正確添加到您的iOS項目:
Add files to <YourProjectName>
target
。 請參考屏幕截圖。 構建目標后,打開捆綁包,目錄結構將完全保留在其中。 不僅如此,可以通過以下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.