當我使用:iPhone UNIX文件訪問
int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
在設備模擬器的所有是正確的。 但物理設備返回fd = -1和errno = 0x0d(13),權限被拒絕。
目標路徑是應用程序沙箱Documents或tmp路徑。
這是可能讀取&在iPhone上,在應用程序的專用區,通過Unix函數寫入文件沒有越獄嗎?
當我使用:iPhone UNIX文件訪問
int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
在設備模擬器的所有是正確的。 但物理設備返回fd = -1和errno = 0x0d(13),權限被拒絕。
目標路徑是應用程序沙箱Documents或tmp路徑。
這是可能讀取&在iPhone上,在應用程序的專用區,通過Unix函數寫入文件沒有越獄嗎?
* 答案*的Compleat重寫
下面的代碼工作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
NSArray *dp = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [dp objectAtIndex:0];
NSString *fileNameWithPath = [NSString stringWithFormat:@"%@/%@",path,@"fileName.txt"];
int fd = open([fileNameWithPath UTF8String], O_CREAT | O_RDWR);
NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);
return YES;
}
調試輸出:
Re-enabling shared library breakpoint 1
(gdb) n
30 NSLog(@"Open %@ for writing returned %d",fileNameWithPath,fd);
(gdb) n
2012-01-20 11:01:23.161 FOpen[6535:707] Open /var/mobile/Applications/AF7EA358-E4AA-491F-AEA1-83080F2749E5/Documents/fileName.txt for writing returned 6
32 return YES;
的關鍵是,你必須通過C函數「開放'ac風格的字符串,例如[fileNameWithPath UTF8String]
。如果你使用的是ARC,你可能需要做更多的工作才能讓編譯器感到滿意,但我不能確認。
因此,我使用相同的方式獲取路徑,但open()ora creat()的結果始終爲-1,錯誤號爲0x0d。相同的結果是tmp路徑:NSString * path = NSTemporaryDirectory();還有一件事 - 在XCode Organizer,應用程序(用於我的設備)和「數據文件在沙箱中」 - 文件存在,但是lenght = 0B。 – theWalker
設備上的路徑,其中我探測寫入文件:/ var/mobile/Applications/7FF29BC7-B6D1-41CF-8621-CC6DFEEEEF045/Documents – theWalker
「...該文件存在,但lenght = 0B ...」這意味着open()創建文件,但不返回描述符,說「拒絕訪問」。我完全不理解它。 – theWalker
它應該可以正常工作,但您必須記住iOS模擬器使用的是不同於真實設備的文件系統。而且您只能從軟件包中讀取文件而不是寫入,並且您嘗試以讀取\寫入權限打開。請檢查值fileNameWitPath
正確的方式來獲得文件目錄爲:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
您應該使用
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath: contents: attributes:]
,而不是
int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
我寫:
int fd = open(fileNameWithPath, O_CREAT | O_RDWR);
但「呼叫到‘開放’需要的時候,‘O_CREAT’設置第三個參數」。 代碼的行必須(對RW權限):
int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);
這項工作的罰款。
[編輯] 我寫:
int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 0x0666);
這是錯誤的umask必須是666,但八進制,而不是十六進制:
int fd = open(fileNameWithPath, O_CREAT | O_RDWR, 00666);
斯基的關於失蹤的第三個參數的評論是正確的問題在這裏。
沒有第三個參數,打開(...)將首次運行,並在隨後的運行中以-1和EACCES失敗,因爲該應用第一次創建該文件,但不再具有訪問該文件的權限。 (如果您在設備上重新編譯和測試,並看到錯誤,這是因爲您需要刪除刪除不正確許可文件的應用程序。)
您可以訪問應用程序沙箱中的文件(例如文檔文件夾)。你如何獲得'fileNameWitPath'?獲取你需要做的Documents目錄:'NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); NSString * docPath = [paths objectAtIndex:0];'爲臨時目錄:'NSTemporaryDirectory()'是一個NSString,其路徑爲tmp –
「你怎麼得到...」:NSArray * dp = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask ,YES); NSString * path = [dp objectAtIndex:0]; – theWalker
請重新閱讀我的答案我已將其重寫。代碼現在可用。 – Diziet