- Home /
How to Interact With native iOS framework in podspec github repository?
Another developer has created a native iOS framework for me to add to my Unity3D project, however, I do not know how to interact with it.
All I received was a cocoapod (.podspec). I have looked through tutorials and forums about this, and it seems that I require more files (.mm) but I am confused on who produces these files or how to obtain these files. This is the tutorial I am following: https://medium.com/voodoo-engineering/bringing-native-ios-frameworks-into-unity-games-by-hacking-ios-methods-2c656d18cf9b
I also have been trying to follow these instructions too: https://docs.unity3d.com/560/Documentation/Manual/PluginsForIOS.html
I am stuck because I am not sure if I have to create a C++ file to sit in the middle of the native framework and Unity3D to communicate with each other? cocoapod <-- C++ (.mm) file --> Unity3D Can this be generated or automated somehow? I could use some help with a step-by-step guide about this. I'm confused on the bridging part. Thanks in advanced!
Answer by unity_SndF7xzWLSMUWQ · Jul 07, 2021 at 04:33 PM
I actually have been looking for the answer to this same question recently. There isn't very much documentation on this. Community is basically our best shot at demystifying technical issues. Has anyone else done this before?
I think I have a general idea of how it works, but I don't want to lead you down the wrong road since I have not actually done it myself. But here is my general understanding of it.
There is no way to communicate between Unity and XCode directly. So you have to create a bridge, like you mentioned. This bridge requires coding on both ends.
From the documentation link you posted (you are on the right track)
Unity iOS supports automated plugin integration in a limited way. 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. However, merging is done by symlinking files from Assets/Plugins/iOS to the final destination, which might affect some workflows. The.h files are not included in the Xcode project tree, but they appear on the destination file system, thus allowing compilation of .m/.mm/.c/.cpp files.
So, you need to create a UnityIOSBridge.m and place that class to the path "Assets/Plugins/iOS". Unity needs the plugins to be c-named, so it is a good practice to wrap up the methods which need to be called from Unity inside extern "C". But there is no hard and fast rule, you can create .m class and write your c-named methods just outside the implementation part and you can call them from Unity. The only constraint is that you can not call these methods if you are building your app on a simulator, as Unity iOS plugins only works on devices (as far as I know).
UnityIOSBridge.m class should look like as follows:
#
import "UnityIOSBridge.h"
void messageFromUnity(char * message) {
NSString * messageFromUnity = [NSString stringWithUTF8String: message];
NSLog(@ "%@", messageFromUnity);
}
@implementation UnityIOSBridge
@end
To call the above method from Unity, we have to write a Unity script, so let’s create the file UnityIOSBridge.cs.
using UnityEngine;
using System.Collections;
using System;
//This is needed to import iOS functions
using System.Runtime.InteropServices;
public class UnityIOSBridge: MonoBehaviour {
/*
* Provide function decalaration of the functions defined in iOS
* and need to be called here.
*/
[System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void messageFromUnity(string message);
//Sends message to iOS
static void SendMessageToIOS() {
messageFromUnity("Hello iOS!");
}
}
But with this you require access to binary files. Because you received a cocoapod, there are more steps than what I outlined. I am trying to figure those out.
But here are my thoughts about the cocoapod. The files you need, such as the .h and .m files are automatically generated by the cocoapod when you import the library or framework. These are Objective-C files. What does your podfile look like in Unity after you import? You may have all that you need. Rename the .m file to .mm, and that will get you to the start of my post. Again, I have not done this myself, I am just guessing :-).
And as a reference, I have been looking at this article to learn (mostly): https://www.agnosticdev.com/blog-entry/swift/integrating-unity-and-vuforia-ios-swift-project
I also had this problem, so thanks for making this and I will read these articles to further expand my knowledge.
Thanks for taking the time to look into this. I would like to inform that what I received was a Podspec github repository containing iOS native framework. It is written using Swift Dynamic Library. I find no .m or .mm files inside this repository. I'm adding this podspec github repository as a dependency using the External Dependency Manager. Here in the iOS Resolver Usage section you can find the format to add the dependency. Also the same is stated in the Unity part of Step 2 of the Medium article that I had posted in my actual question. Now I don't know how to proceed from here onwards.
Your answer
Follow this Question
Related Questions
Custom swift framework in Unity iOS game 0 Answers
iOS Simulator build for Facebook Review 2 Answers
EXC_BAD_ACCESS on PreparePresentRenderingSurface (iOS Build) 0 Answers
How do I get the Metal device for IOS within a native plugin? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers