4

I'm trying to use an iOS native framework inside Unity, but I don't know how.

Inside my C# script, I call a native function like this :

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

    void Start () {
        writeHello();
    }

I have drag the iOS framework inside my project like this :

Link to my image

But when I deploy my app on iPad, xCode showing this error :

Undefined symbols for architecture armv7: "_writeHello", referenced from: RegisterMonoModules() in RegisterMonoModules.o ld: symbol(s) not found for architecture armv7 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I don't understand why, and I don't know if what I'm trying to do is possible. Help! :)

Thanks, Sennin

Benjamin Gimet
  • 707
  • 1
  • 9
  • 16

4 Answers4

3

The iOS framework must be put in the xCode generated project, not in unity.

And the framework's functions must be wrapped in an extern "C" block (as shown here).

Then you will be able to use it in C# with dllimport.

Sam Bauwens
  • 1,317
  • 11
  • 18
  • HI, and thanks for your response. The problem is that my writeHello function is written in objective-c native language, so my writeHello file is a .m, can I use the symbol "extern"? – Benjamin Gimet Nov 07 '12 at 10:31
1

The doc says that all files with extensions .a,.m,.mm,.c,.cpp located in the Assets/Plugins/iOS folder will be merged into the generated Xcode project automatically. The docs also says that subfolders are currently not supported, so, in your example, the "framework-helloUnity.framework" folder will be ignored.

It is important to note that .m files default to C-style bindings, while .mm files default to C++-style bindings. In other word, unless you use the extern "C" keyword, as Heilo suggested, your functions won't be found if you put them in .mm or .cpp files.

In other words, if you put the following:

void writeHello() {}

in a file named Assets/Plugins/iOS/myTestFile.m, your code should compile.

Eric Laberge
  • 101
  • 1
  • 7
0

Found the solution thanks to this post : Calling Objective-C method from C++ method?

By doing like that :

void myWriteHello( void *test) {
    [(id)test writeHello];
}

- (void) writeHello
{
    NSLog(@"Hello World!");
}

and calling my C function from Unity

Community
  • 1
  • 1
Benjamin Gimet
  • 707
  • 1
  • 9
  • 16
0

You have write the following function in your Xcode project any class extern "C" { -(void) writeHello {

} }

Ganesh
  • 41
  • 1
  • 10