Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Aram-Azhari · Jun 11, 2015 at 08:07 PM · iosnative pluginobjective-c

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Aram-Azhari · Jun 12, 2015 at 03:10 PM

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by superdev · Jun 12, 2015 at 09:50 AM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Cris-Uy · Jun 28, 2017 at 12:46 AM

https://medium.com/@rolir00li/integrating-native-ios-code-into-unity-e844a6131c21

You can follow this thread. :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Display a UIPopOver on iPad 0 Answers

Issue with integration into native iOS app 1 Answer

Sharing image and text using objective c native code 1 Answer

iOS ipa file size increases a LOT when using native calls 0 Answers

Magnifying Glass Unity iOS 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges