Search code examples
iosunity-game-engineimessage-extension

can't save file to shared container of AppGroup with unity ios


I want to save an image file to group container to use with iMessage extension as a sticker which was generated by Unity, So I wrote an ios plugin to get the containers path and save the data to the accordingly path, I have successfully get the path which was

/private/var/mobile/Containers/Shared/AppGroup/1C848C27-6215-4C6F-98A9-E42E7794826D

but can't save it due to the following exception:

    IsolatedStorageException: Could not find a part of the path "/private/var/mobile/Containers/Shared/AppGroup/1C848C27-6215-4C6F-98A9-E42E7794826D/Documents/0.jpg".
  at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0 
  at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in <filename unknown>:0 
  at TestSharedContainer.saveImageToFolder () [0x00000] in <filename unknown>:0 
  at TestSharedContainer.OnGUI () [0x00000] in <filename unknown>:0 

I save the file using following code in c#

string fs_share_path = containerPath + "/0.jpg";
Debug.Log("writing :" + fs_share_path);
FileStream fs_share = File.Create(fs_share_path);
fs_share.Write(text.EncodeToJPG(), 0, text.EncodeToJPG().Length);
fs.Close(); 

How to make this work?


Solution

  • I've never seen any one that got this working by obtaining the path and trying to write to it from C#. Also, I've tried this but failed too. I guess you can't do it from C#.

    The only way I found to work to work for this is to move the write function to the plugin. Use Object-C to write a function that writes to that directory. This Object-C function should take two parameters: the data (byte[]) and the name of the file(string). You can then call this function from C# and pass the data to save and the name of the file to Object-C plugin.