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?
NativeLibrary.dll
directly instead of reference the project and then build your project. – Jack Hua