iPhone unix 文件访问
当我使用:
int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
在设备模拟器中一切正常。 但物理设备返回fd = -1和errno = 0x0d (13),权限被拒绝。
目标路径是应用程序沙箱文档或tmp路径。
这可以阅读吗?不越狱,通过Unix功能在iPhone上的应用程序私有区域写入文件?
When I use:
int fd = open(fileNameWitPath, O_CREAT | O_RDWR);
in the device simulator all is right.
But physical device return fd = -1 and errno = 0x0d (13), permission denied.
The destination path is the application sandbox Documents or tmp path.
Is this possible to read & write files on iPhone, in the application private area, by Unix function without jailbreak?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它应该可以正常工作,但您必须记住,iOS 模拟器使用与真实设备不同的文件系统。事实上,您只能从捆绑包中读取文件,而不能写入文件,并且您尝试使用读\写权限打开文件。请检查 fileNameWitPath 的值
获取文档目录的正确方法是:
您应该使用
而不是
It should work OK but you have to remember that the iOS Simulator uses different filesystem than a real device. And the fact that you can only READ files from the bundle, not WRITE, and you try to open with read\write permission. please check the value of fileNameWitPath
The right way to get the document directory is:
You should use
instead of
Skippy 关于缺少第三个参数的评论是这里的正确问题。
如果没有第三个参数, open(...) 将第一次工作,并在后续运行中失败并返回 -1 和 EACCES,因为应用程序第一次创建了该文件,但不再具有访问该文件的权限。 (如果您在设备上重新编译和测试,并看到错误,那是因为您需要删除应用程序,该应用程序会删除权限不正确的文件。)
Skippy's comment regarding the missing third parameter is the correct issue at hand here.
Without the third parameter, open(...) will work the first time, and fail with -1 and EACCES on subsequent runs because the app created the file the first time, but no longer has permission to access the file. (If you are recompiling and testing on device, and seeing the error, it's because you need to delete the app which deletes the incorrectly-permissioned file.)
*完全重写答案*
以下代码有效:
调试输出:
关键是您必须传递 c 函数“open”ac 样式字符串,例如
[fileNameWithPath UTF8String]
。如果您使用 ARC,您可能需要做更多的事情才能让编译器满意,但我无法确认这一点。*Compleat rewrite of answer *
The following code works:
Debug output:
The key is that you must pass the c function 'open' a c style string, e.g.
[fileNameWithPath UTF8String]
. If you are using ARC you may have to do a bit more to get the compiler happy, I can't confirm that though.我写的是:
但是“设置'O_CREAT'时调用'open'需要第三个参数”。
代码行必须是(对于 RW 权限):
并且这可以正常工作。
[编辑]
我写的是:
这是错误,umask 必须是 666,但是八进制,而不是十六进制:
I was write:
But "Call to 'open' requires third argument when the 'O_CREAT' is set".
The line of the code must be (for RW permissions):
and this work fine.
[edit]
I was write:
this is error, umask must be 666 but octal, not hex: