Xamarin.iOS is not seeing reference to iOS binding library
Asked Answered
S

2

7

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?

Soften answered 1/8, 2019 at 9:4 Comment(3)
Try to reference the NativeLibrary.dll directly instead of reference the project and then build your project.Lublin
I have already tried it, but it is still not working.Soften
That's great that you found a solution. Add your answer below, so we can mark the question as closed.Haddock
S
7

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.

Soften answered 5/8, 2019 at 7:17 Comment(0)
V
1

This is Crazy, but in my case, just create the ios binding project in a different solution and add the binding project DLL directly in the ios project. it will work fine.

Volley answered 28/2, 2020 at 9:20 Comment(1)
I had the same issue and @hussein Khalil solution worked for me (just create the ios binding project in a different solution and add the binding project DLL directly in the ios project. it will work fine.)Leahy

© 2022 - 2024 — McMap. All rights reserved.