xcode UnauthorizedAccessException:访问路径 [iOS/Unity3D]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37830969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UnauthorizedAccessException: Access to the path [ iOS / Unity3D ]
提问by Solid_Metal
i need your help, so yesterday i want to port my game to iOS, this is not my first time porting it, the previous version always port sucessfully, but not now
我需要你的帮助,所以昨天我想把我的游戏移植到 iOS,这不是我第一次移植它,以前的版本总是成功移植,但不是现在
i'm using Unity 5.3.4 and 5.3.5 with Xcode 7.3 on El Capitan, its sucessfully compile from the XCode, but all the object that i initilize and spawn at the start of the scene wont show up on the game, i dont know whats wrong with it since theres no error on Unity or XCode itself
我在 El Capitan 上将 Unity 5.3.4 和 5.3.5 与 Xcode 7.3 一起使用,它从 XCode 成功编译,但我在场景开始时初始化和生成的所有对象都不会出现在游戏中,我不知道它有什么问题,因为 Unity 或 XCode 本身没有错误
but from the logs, i suspect its because of this
但从日志中,我怀疑是因为这个
UnauthorizedAccessException: Access to the path"/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk" is denied. at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path, Boolean append) [0x00000] in :0 at System.IO.File.CreateText (System.String path) [0x00000] in :0
UnauthorizedAccessException:访问路径“/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk”被拒绝。在 System.IO.FileStream..ctor(System.String 路径、FileMode 模式、FileAccess 访问、FileShare 共享、Int32 缓冲区大小、布尔匿名、FileOptions 选项)[0x00000] 在 :0 在 System.IO.StreamWriter..ctor(系统.String path, Boolean append) [0x00000] in :0 at System.IO.File.CreateText (System.String path) [0x00000] in :0
its i assume because, when launch, the game will access texture/sprite from secure website using WWW method, with HTTP not HTTPS, and try to download and save it as sprite at Application.PersistentDataPath and then apply it into the game
我认为这是因为,在启动时,游戏将使用 WWW 方法从安全网站访问纹理/精灵,使用 HTTP 而非 HTTPS,并尝试在 Application.PersistentDataPath 下载并保存为精灵,然后将其应用到游戏中
its working fine on Android but somehow iOS wont allow to read/write the file, hench the error
它在 Android 上运行良好,但不知何故 iOS 不允许读/写文件,解决错误
heres snippet of the code i use :
这是我使用的代码片段:
IEnumerator getBillboard()
{
WWW getBillboardlistURL = new WWW(billboardlistURL);
yield return getBillboardlistURL;
if (getBillboardlistURL.error != null)
{
}
else
{
WWW imageLink = new WWW(pictLink[Mathf.FloorToInt(i / 3)]);
yield return imageLink;
Texture2D imageTemp = imageLink.texture;
obj.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = Sprite.Create(imageTemp, new Rect(0, 0, imageTemp.width, imageTemp.height), new Vector2(0.5f, 0.5f));
obj.name = namaProduk[Mathf.FloorToInt(i / 3)];
print("BILLBOARD ONLINE");
/// ======= WRITE BILLBOARD NAME TO FILE AND SAVE IT =======
/// ======================= END ============================
var bytes = imageLink.texture.EncodeToPNG();
//print(Application.dataPath);
//cek file
if(redownloadImage)
{
File.WriteAllBytes(Application.persistentDataPath + "/vendorBillboard/" + namaProduk[Mathf.FloorToInt(i / 3)] + ".png", bytes);
print("REDOWNLOAD SEMUA TEXTURE!");
}
obj.SetActive(false);
billboardPool.Add(obj);
}
}
}
}
is there workaround or fix for this?
有解决方法或修复吗?
thank you in advance
先感谢您
采纳答案by Solid_Metal
alright...
好吧...
today i have the chance to test a fix that i come up with, thanks to Needleski.
今天我有机会测试我想出的修复程序,感谢 Needleski。
and yes, it work, for some reason, iOS dont allow me to save a file (in this case any file) to Application.persistentDataPath
directly,
instead, what i do is, i create a Folder, then create/save the file inside the folder, and it work correctly now
是的,它有效,出于某种原因,iOS 不允许我直接保存文件(在这种情况下为任何文件)Application.persistentDataPath
,相反,我所做的是,我创建一个文件夹,然后在文件夹中创建/保存文件,现在它可以正常工作
回答by Needleski
Looking at the exception and comparing it to your code, I don't think the code your provided is where the error is occurring... for 2 reasons:
查看异常并将其与您的代码进行比较,我认为您提供的代码不是发生错误的地方...有两个原因:
The exception says "UnauthorizedAccessException: Access to the path"/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk" is denied". However, in your code you have
/vendorBillboard/
in your path. Notice/vendorBillboard/
is not in the path in the exception.At the end of the exception it states the problem is in
System.IO.File.CreateText (System.String path)
, however, you have usedSystem.IO.File.WriteAllBytes()
in your code.
异常显示“UnauthorizedAccessException:访问路径”/var/mobile/Containers/Data/Application/280DC265-ABD8-41A3-8B87-74D09910FB24/Documentstemp.gjk“被拒绝”。但是,在您的代码中,您有
/vendorBillboard/
自己的路径。注意/vendorBillboard/
不在路径中的异常。在异常结束时,它指出问题出在
System.IO.File.CreateText (System.String path)
,但是,您已System.IO.File.WriteAllBytes()
在代码中使用。
I'm guessing you are creating a text file "somewhere else" and you are using Application.dataPath
there instead of Application.persistentDataPath
.
我猜您正在“其他地方”创建一个文本文件,而您正在使用Application.dataPath
那里而不是Application.persistentDataPath
.