Using an Objective-c framework directly in Unity 3d

Hi,

Here’s what I have done:
1- Define some functions within a framework
In .h :

#import <UIKit/UIKit.h>

FOUNDATION_EXPORT double TestVersionNumber;

FOUNDATION_EXPORT const unsigned char TestVersionString[];

@interface MyLib : NSObject

+ (id)sharedInstance;

- (void)openWebView:(NSString *)url;

- (BOOL)testMethod;

@end

In .m :

#import <Foundation/Foundation.h>
#import "MyLib.h"
#import "ViewController.h"

@interface MyLib()
@property (nonatomic, retain) ViewController *webViewController;
@end

@implementation MyLib

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

+ (id)sharedInstance {
    static dispatch_once_t pred = 0;
    static id instance = nil;
    
    dispatch_once(&pred, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

- (BOOL)testMethod {
    NSLog(@"testMethod was called");
    return TRUE;
}

@end

3- I then copied the framework to 'Assets/Plugins/iOS/` folder

4- I attached this to the camera in the scene:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class Ttest : MonoBehaviour {


	[DllImport ("__Internal")]
	private static extern bool testMethod();
	

	// Use this for initialization
	void Start () {
		var result = testMethod();
		if (result) {
			Camera.main.backgroundColor = Color.yellow;
		}
	}


}

4- Built the unity project and ran in the simulator.

I get an error saying testMethod() was not found.

What’s the problem?
Thanks.

Adding my comment from here.

Basically, you have to have the methods you want to call exposed directly in a header or .m file.

You would have another “outside” object store your shared instance and hold reference to your class so that will call the MyLib functions.

You won’t be able to call them directly from the C# side.

You can have the sharedInstance call whatever other functions you want to call within the static library or framework, but they don’t necessarily need to be 1 to 1 unless you need to call each function within the MyLib class separately.

UPDATE: Ok, after further research I realized that the framework itself needs to be in a specific manner to work.

To work on the device, in the framework project:

  1. Build Settings → MachO → The framework should be marked as “Static Library”
  2. Build Settings → Turn off “Build active architecture only”
  3. Build Settings → Other Linker Flags should be ‘-ObjC’

Thank you @superdev for this. Unfortunately the .m file that I have created doesn’t work properly with the framework that i created. So here’s what i’ve done but no success:

  1. Create a framework with the name MyLib.framework

  2. Copy the framework and the .h/.m wrapper files to plugin/iOS/

  3. Call the wrapper function _testMethod from the c# code (added the dllimport signature too).

  4. I get errors regarding ViewController that lives in the framework. I commented the framework code out for now just to test a simple method that is in the mention .m file.

  5. I got this error:

    EntryPointNotFoundException: _testMethod
    at (wrapper managed-to-native) iosCallerTest:_testMethod ()
    at iosCallerTest.Start () [0x00000] in :0

Any suggestions?

You can follow this thread. :slight_smile: