ibnetariq
ibnetariq

Reputation: 738

Directory.createDirectory creates files instead of directory in iOS

I have to save some data locally on iOS device for an Unity Game. Unity provide

    Application.persistentDataPath

to get public directory to save data. And printing it on console shows that this path returned for iOS is correct i.e. /Users/userName/Library/Developer/CoreSimulator/Devices/********-****-****-****-************/data/Containers/Data/Application/********-****-****-****-************/Documents/

So one function returns the path and other check if directory exists, if not it should create an directory but it creates FILE without any extension. Here is my code

    void savePose(Pose pose){

        if (!Directory.Exists(PoseManager.poseDirectoryPath())){
            Directory.CreateDirectory(PoseManager.poseDirectoryPath());
            //Above line creates a file  
            //by the name of "SavedPoses" without any extension
        }
        // rest of the code goes here
    }

    static string poseDirectoryPath() {
        return Path.Combine(Application.persistentDataPath,"SavedPoses");
    }

Upvotes: 1

Views: 2206

Answers (1)

Programmer
Programmer

Reputation: 125435

Possible sloutions:

1.Path.Combine(Application.persistentDataPath,"SavedPoses"); adds back slash before savedPoses while others are forward slashes. Maybe this causes a problem on iOS. Try raw string concatenating without the Path.Combine function.

static string poseDirectoryPath() {
    return Application.persistentDataPath + "/" + "SavedPoses";
}

2.If the Directory class does not work properly, Use the DirectoryInfo class.

private void savePose(Pose pose)
{
    DirectoryInfo posePath = new DirectoryInfo(PoseManager.poseDirectoryPath());

    if (!posePath.Exists)
    {
        posePath.Create();
        Debug.Log("Directory created!");
    }
}

static string poseDirectoryPath()
{
    return Path.Combine(Application.persistentDataPath, "SavedPoses");
}

EDIT:

Likely a permission problem on iOS.

You can should create the folder in the StreamingAssets directory. You have permission to read and write to this directory.

The general way to access this is with Application.streamingAssetsPath/.

On iOS, it can also be accessed with Application.dataPath + "/Raw".

On Android, it can also be accessed with "jar:file://" + Application.dataPath + "!/assets/"; and for Windows and Mac with Application.dataPath + "/StreamingAssets";. Just use the one that works for you.

For your question, Application.dataPath + "/Raw"+"/SavedPoses"; should do it.

Upvotes: 1

Related Questions