0

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?

armnotstrong
  • 8,605
  • 16
  • 65
  • 130

2 Answers2

1

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.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • thanks for the advice, I will try this, the only concern I have is if there will be some more pits I have to struggle with if passing a bulk of data to the native plugin function. – armnotstrong Sep 06 '17 at 07:06
  • I know. It's annoying but you will get used to it. I use C/C++ to do it then call Object-C function from the C++ function. That's extremely easy then trying to do it directly from Object-C. Don't forget to pin the array in C# before sending to C/C++. See [here](https://stackoverflow.com/a/44424465/3785314) for example on sending array to C/C++ from C#. – Programmer Sep 06 '17 at 07:10
  • Thanks for the ref, and I am now thinking of making a native function that moves the file from the container it owns to the shared container might be an easy way for me to manipulate, I am a fish on this plugin stuff and in my project I have to save to the owned container anyway :D – armnotstrong Sep 06 '17 at 07:24
0

I notice that Unity's Application.persistentDataPath return a path begining with /var/mobile/Containers/Data/Application/ on iOS.

I've tried trunking the AppGroups's container path making it beginning by /var/mobile/Containers/Shared/AppGroup/ and it make the file writing/reading work from C#/Unity side.