Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

c# - How to bind an Objective-C static library to Xamarin.iOS?

I have read the documentation from Xamarin.

And this is my test class in Objective-C:

#import "XamarinBundleLib.h"

@implementation XamarinBundleLib

+(NSString *)testBinding{
    return @"Hello Binding";
}
@end

It's very easy, just one method.

And this is my C# class:

namespace ResloveName
{
    [BaseType (typeof (NSObject))]
    public partial interface IXamarinBundleLib {
        [Static,Export ("testBinding")]
        NSString TestBinding {get;}
    }
}

Then this is my AppDelegate code:

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            // Override point for customization after application launch.
            // If not required for your application you can safely delete this method

            string testStr = ResloveName.IXamarinBundleLib.TestBinding.ToString ();
            System.Console.WriteLine ("testStr="+testStr);

            return true;
        }

When I run the application, I get this exception: enter image description here

The TestBinding attributes is null. I must be wrong somewhere, so how can I fix it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I wrote a very detailed blog post about creating a static library from ObjC code last year that works on Xamarin.iOS binding projects and you can find it here (just in case :wink::wink:).

That being said if you already have a fat static library in your hands and it is already added into your Xamarin.iOS Binding Project as shown here:

binding image

The issue could be that your libxyz.linkwith.cs is missing some information, if it looks like this:

using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", SmartLink = true, ForceLoad = true)]

it is definitely missing some important information about the architectures supported by your fat library (it is missing the second argument target), you can use the following command to retrieve what architectures your current static library supports

xcrun -sdk iphoneos lipo -info path/to/your/libFoo.a

and you should get something like this as output

Architectures in the fat file: Foo/libFoo.a are: i386 armv7 x86_64 arm64

So we know this static library supports i386 armv7 x86_64 arm64 and we should provide our LinkWith attribute the supported archs by providing the second argument target as follows:

using ObjCRuntime;
[assembly: LinkWith ("libFoo.a", LinkTarget.ArmV7 | LinkTarget.Arm64 | LinkTarget.Simulator | LinkTarget.Simulator64, SmartLink = true, ForceLoad = true)]

Also make sure that the first parameter of the LinkWith attribute matches your static library file name ("libFoo.a" in my case).


The other thing I would suggest double checking is that the Build Action of your static library (libFoo.a in my case) is correctly set to ObjcBindingNativeLibrary as show here:

binding image

Hope this helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...