Passing a value from Unity to Objective C

Hi, from the title I’m trying to pass a value in this case a file from using Application.CaptureScreenshot which is a string type. The capture screen function is in a js script so I pass the value to cs code. Here’s what I have so far:

    //saveToAlbum.cs

    private CaptureScreen captureScreen;
	
	[DllImport("__Import")]
	extern static public string savePicture(string picture);
	private string temp;
	
	void Awake()  {  
        //Get the JavaScript component  
        captureScreen = this.GetComponent<CaptureScreen>(); 
    }  
	
	void Update()	{
		if(captureScreen.press == true)	{
			//print(captureScreen.picName);
			temp = savePicture(captureScreen.picName);
		}
	}

It builds fine but now the tricky part for me in calling it in Objective C. So I wrote this:

// MyApplication.h

extern "C"  {
    NSString *savePicture(NSString *picture);
}

and

// MyApplication.mm 

NSString *savePicture(NSString *picture)  {
    NSLog(@ "@", picture);
    
    return picture;
}

At the moment, this is just to test printing out the names of the pictures. Whenever i run this, xcode crashes and has this in the output console “Unable to resolve pinvoke method”

What am I doing wrong or is there a better way of doing this? If this works I’m hoping to use UIImageWriteToSavedPhotosAlbum.

Appreciate the help, thanks very much.

Hi Hakimo,

Remember me?

Anyway the problem is, you call a objective c function by passing a string, but I can see you don’t change the received string to NSString.

Use this function to change your string to NSString

NSString* CreateNSString (const char* string)
{
  if (string)
	return [NSString stringWithUTF8String: string];
  else
        return [NSString stringWithUTF8String: ""];
}

and modify your function as:
extern "C"  {
 void savePicture(const char* picture)
 {
    NSLog(@"Hi i am in objective c---",CreateNSString(picture));
 }
}

Note : Save this file with .mm extention