Execute native ios code in unity

| | August 4, 2015

I want to execute native iOS code in unity. For this I have added two files in /plugins/ios.

  1. ViewController.h
  2. ViewController.m

Code for each file represented as under.

ViewController.h

@interface ViewController

-(void)sampleMethod;
@end

ViewController.m

#import "ViewController.h"

@implementation ViewController

-(void)sampleMethod
{
     NSLog(@"Sample Method Execute");
}
@end

For C# file I have added following code

[DllImport("__Internal")]
private static extern void sampleMethod();

and call to above method

sampleMethod();

When I am export project for iOS, xCode give me following error that I can’t able to understand.

enter image description here

I can’t able to understand how to solve this problem? Please give some suggestion here.

EDIT :

I have added extern keyword in my .m file after reading some suggestion.

#import "ViewController.h"

@implementation ViewController

extern "C"
{
   -(void)sampleMethod
   {
        NSLog(@"Sample Method Execute");
   }
}
@end

But in xCode now I am getting Expected Identifier or ‘(‘ at the line of extern keyword line. So what to do next?

2 Responses to “Execute native ios code in unity”

  1. Right now I’m not in a place to go over your code thoroughly, but at a glance I’m wondering if you need to declare your code with C linkage as explained here:

    extern "C" {
      float FooPluginFunction();
    }
    
  2. I’m not sure if this answers your question but:

    iOS of course doesn’t run Java since iOS requires all code to be compiled AOT to xcode / objective c. When you build an iOS project, Unity just creates an xcode project which then can be built using xcode. You are free to change the code that Unity has cross compiled, however you have to do this every time when you rebuild the xcode project from Unity.

    It’s probably possible to compile a native library for iOS that you can add to the Unity project itself as “plugin”. However i’m not familiar with iOS development.

    http://answers.unity3d.com/questions/816358/executing-native-ios-code-in-unity.html

Leave a Reply