Create a file in iOS

Hi, i’m trying to create a file in iOS, but i don’t find a valid path to save it.
I already test on Editor and Android and both work, but for iOS no, dunno why.

IEnumerator Download()
{
string myPath;
if(Application.platform == RuntimePlatform.Android)
myPath = “/storage/emulated/0/Download/30.wtc”; //ANDROID (work)
else if(Application.platform == RuntimePlatform.IPhonePlayer)
{
myPath = Application.dataPath + “30.wtc”; //iOS
myPath = myPath.Replace (“/game.app/Data”, “/tmp/”); //this path dont work
File.Create ( myPath ); //tryied with and without
}
else
myPath = Application.streamingAssetsPath + “/30.wtc”; //Editor (work)

WWW download = new WWW (urlPath);
yield return download;
File.WriteAllBytes ( myPath, download.bytes );
}

I tryied also with “/Documents/” repalced with “/tmp/” in the script, and the error in XCode is the same:
path dont found

If i try to save on the data path XCode give this error:
UnauthorizedAccessException: Access to the path “/var/containers/Bundle/Application/A5ACC159-D58B-496E-BA3V-668546FC378C/game.app/Data/Resources/30.wtc” is denied.

I tryed also:
myPath = Application.absoluteURL + “/Documents/30.wtc”;
myPath = Application.dataPath + “/Resources/30.wtc”;
myPath = Application.persistentDataPath + “/30.wtc”;
but are all wrong methods.

Can anyone tell me where is my issue? I have to use File.Create or not? I was wrong with the path?
Thanks a lot

You should be able to use Application.persistentDataPath for all platforms. File.WriteAllBytes will create the file for you as well. (shouldn’t have to use File.Create)

myPath = Application.persistentDataPath + “/30.wtc”;

This should work for all platforms.

Also as a friendly note. Don’t use IF checks for platform checks. Use the platform specific tags, so it only includes the code that is needed for the platform it’s on.

1 Like

Ok, i remove the File.Create, but i tried without and dont work as well.
I don’t know why, but on Andorid and iOS"PersistentDataPath" don’t work, so i have use that link in Andorid for let it work,
maybe is the format of the file i’m using?
Now i’ll try again to compile in iOS with persistent data path, but i alredy now that doesn’t work.
There is my new code:

string myPath;
#if UNITY_ANDROID
myPath = “/storage/emulated/0/Download/30.wtc”; //ANDROID
#elif UNITY_IOS
myPath = Application.persistentDataPath +/30.wtc"; //iOS
#else
myPath = Application.streamingAssetsPath + “/30.wtc”; //PC
#endif

WWW download = new WWW (urlWTC);
yield return download;
File.WriteAllBytes ( myPath, download.bytes );

Are you using something like log viewer to view errors/debugs?

I would see what errors are being shown, if any, on the device. You can also use File.Exist to check for the file.

Honestly, I can’t tell you why persistentDataPath isn’t working for you on all platforms. We use it in several of our apps and it works perfectly fine. I’m not aware of any special settings needed on iOS. Android I believe does require a manifest permission if I recall, but usually that’s not a big deal to include that. I’m not sure why you are having issues.

I caution against using a path directly as it may not exist. The purpose of persistentDataPath is to point to a path that will exist and it is setup for the devices.

Oook, PersistentDataPath is working!
Maybe i issue something in the build the first time!
Thanks a lot Brathnann! :smile:

Glad it’s working now! :slight_smile:

How did you fixed it, because I have the same issue!

you have any issue related to application.persistantdatatype than let me know

Do you know how to get the path to the Downloads folder?

So you can see the saved file from the Files app.

Thank you