How do I bind to a system framework with Xamarin.Mac?

472 views Asked by At

I'm in the proces of making an app for interacting with smartcards. For that I'd like to use the CryptoTokenKit Framework which is standard on a Mac (located at /System/Library/Frameworks/CryptoTokenKit.framework).

This link says that it's possible to bind frameworks in a Mac project: https://developer.xamarin.com/guides/cross-platform/macios/native-references/

I've created an ApiDefinition.cs file and a StrucsAndEnums.cs file using the following sharpie command: sharpie bind -framework ./CryptoTokenKit.framework -sdk macosx10.13 -o ~/CryptoTokenKitBinding

I can't find any info on the internet how to implement the above mentioned files and start using the framework.

1

There are 1 answers

4
SushiHangover On

Create a Xamarin.Mac binding project within a solution.

Add a NativeReference to:

/System/Library/Frameworks/CryptoTokenKit.framework

Bind it using sharpie:

sharpie bind \
    -o CryptoTokenKitFramework \
    -namespace CryptoTokenKit \
    -sdk macosx10.13 \
    -f /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/CryptoTokenKit.framework

There is a mismatch between the binding project template and the output of sharpie, so you can either delete ApiDefinition.cs and add ApiDefinitions.cs or just overwrite the template created one:

mv CryptoTokenKitFramework/ApiDefinitions.cs CryptoTokenKitFramework/ApiDefinition.cs

There will be a number of attributes like (versions will change across them):

[Watch (4,0), TV (11,0), Mac (10,12), iOS (10,0)]

As these are private frameworks on iOS, Watch, TV, so strip those platforms out. Leave the Mac attribute and the original version:

[Mac (10,11)]

Note: PlatformAttribute is obsolete but sharpie is still using it (assuming backwards version compatibility(?)), so you can use Introduced if you really want to clean up the build output:

[Introduced (PlatformName.MacOSX, 10, 11, PlatformArchitecture.Arch64)]

There will be a few [Verify] attributes that have to be reviewed/corrected. i.e. TKSmartCardUserInteraction.Cancel and TKSmartCardSlot.MakeSmartCard should both be methods not properties.

Example / Generated:

// -(TKSmartCard * _Nullable)makeSmartCard;
[NullAllowed, Export ("makeSmartCard")]
[Verify (MethodToProperty)]
TKSmartCard MakeSmartCard { get; }

Corrected:

// -(TKSmartCard * _Nullable)makeSmartCard;
[NullAllowed, Export("makeSmartCard")]
TKSmartCard MakeSmartCard();

Fix the rest of the [Verify] attributes and compiler errors, there are a bunch of bad method signatures, pointers, return types, etc.. that are generated and need corrected.

Note: TO make your life easier, make sure that you are using the latest Sharpie version:

Version:  3.4.0
SHA1:     c12859dac8d43121b5a9ed866a0db8409f9df817
URL:      https://dl.xamarin.com/objective-sharpie/ObjectiveSharpie-3.4.0.pkg