22

I am trying to port cocoapods to xamarin.

Objective C based cocoapods can be ported by converting it into a static library(.a) and subsequently converting that as binding library in xamarin studio or visual studio

But Swift based cocoapods can not be converted into a static library and so it can't be ported to xamarin.

but swift can be converted into a dynamic framework but I couldn't find any way to port that to xamarin

Is there any other way to port swift based cocoapods or ios project into xamarin ?

Durai Amuthan.H
  • 31,670
  • 10
  • 160
  • 241
  • 1
    To my beliefs this was not possible until know, but did a quick search and found this link: https://forums.xamarin.com/discussion/76272/binding-swift-libraries . It says it's not official but it could work. Give it a try. – pinedax Mar 17 '17 at 01:27
  • 1
    The "supported" method is to use Obj-C to bridge to `Swift` and expose the API in Obj-C. Depending upon what the `Swift` API is providing and/or how it is inherited, there are ways to bind a `Swift` framework, here is one: http://stackoverflow.com/documentation/xamarin.ios/6091/binding-swift-libraries#t=201703170123590044331 – SushiHangover Mar 17 '17 at 01:31
  • 2
    Personally `Swift` converts really well to `F#` (and C#) and converting the entire Swift framework is an option that should not be overlooked. I use a couple of Constraint and Animation "frameworks" that were originally written in Swift and that I "bound", the results kind of worked (extensions of course will not work, etc..etc...) but were ~30% slower than C#, so I transposed three of the Swift frameworks and the app was faster (load and run), much smaller due to the fact that 6 Swift support dylibs were not required and that the Linker could strip unused code, build times are faster, etc... – SushiHangover Mar 17 '17 at 01:42
  • 1
    @SushiHangover - Thanks You mentioned the use of F# but is it supported in xamarin ? – Durai Amuthan.H Mar 29 '17 at 10:10
  • 2
    @DuraiAmuthan.H `F#` and `C#` are officially supported languages across the entire Xamarin framework (iOS, Android and Forms), `VB` is also *supported* but only in a PCL-based library – SushiHangover Mar 29 '17 at 16:30
  • Thanks for the info – Durai Amuthan.H Mar 29 '17 at 17:07

1 Answers1

8

Binding a Swift Library in Xamarin.iOS follows the same process for Objective-C as shown in xamarin documentation

but with some caveats.

  • A swift class must inherit from NSObject to be binded.

  • Swift compiler will translate class and protocol names into something else, so you must specify the final name in the ApiDefinition.

  • In runtime your APP must include some swift core libraries alongside your binded framework in a folder called Frameworks;

  • When the App is pushed to AppStore it must include a SwiftSupport folder alongside your Payload folder. Those are inside the IPA file.

Here you can find a simple sample binding: https://github.com/Flash3001/Xamarin.BindingSwiftLibrarySample

And a full binding sample: https://github.com/Flash3001/iOSCharts.Xamarin

Graham
  • 7,431
  • 18
  • 59
  • 84
mahesh paymal
  • 146
  • 1
  • 3