I have created new Cocoa Touch Static Library in XCode. I have written code in: StaticLibrary.m:
#import "StaticLibrary.h"
@implementation StaticLibrary
- (int)addX:(int)x toY:(int)y
{
int sum = x + y;
return sum;
}
@end
I have build project in Release-iphoneos and Release-iphonesimulator, then use terminal:
lipo -create Release-iphoneos/StaticLibrary.a Release-iphonesimulator/StaticLibrary.a -output StaticLibraryFat.a
Now I have fat library "StaticLibraryFat.a". Then I create new iOS Binding Library (Xamarin), click PPM -> Add Existing item -> StaticLibraryFat.a. So the file was added and the new libStaticLibraryFinal.linkwith.cs was created. Code inside:
using System;
using ObjCRuntime;
[assembly: LinkWith ("libStaticLibraryFinal.a", LinkTarget.Simulator, ForceLoad = true)]
I go to Mac, open terminal and use Objective Sharpie:
sharpie bind --output=StaticLibrary --namespace=StaticLibrary ~/Desktop/StaticLibrary/StaticLibrary/*.h --sdk=iphoneos12.1 -scope ~/Desktop/StaticLibrary
Now I copy content of ApiDefinitions.cs into iOS Binding Library (Xamarin) - to ApiDefinitions.cs in project.
ApiDefinition.cs
namespace NativeLibrary
{
[BaseType(typeof(NSObject))]
interface StaticLibrary
{
[Export("addX:toY:")]
int AddX(int x, int y);
}
}
I build iOS Binding Library (Xamarin). In folder bin -> Debug there is NativeLibrary.dll.
I create new iOS App (Xamarin). PPM -> Add Reference -> Project -> Solution -> iOS Binding Library (Xamarin). In ViewController.cs I write:
using NativeLibrary
and
NativeLibrary.AddX(1, 2);
but there is an error
"Using directive is unnecessary. The type or namespace name "Native Library" could not be found (are you missing a using directive or an assembly reference?)
What am I doing wrong? When I add reference to iOS Class library then the reference is working perfectly. Why reference to iOS Binding Library is not working?
Ok, I have solved it. There was a problem with different namespaces, so Visual Studio can not connect everything. Namespace at ApiDefinition.cs and Structs.cs must be the same as name of iOSBindingLibrary. The generated .dll file has name "NativeLibrary.dll" and I change it to namespace.dll, then at iOS application I add reference to this dll. then using directive (using "namespace"). In class I write name of XCode's library and create new object. Everything is working perfectly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With