locked
Xamarin does NOT support Binding iOS SWIFT FRAMEWORK to CSharp? RRS feed

  • Question

  • User221964 posted

    I'm trying to binding this iOS native to Xamarin.iOS library.

    this is what I've done so far.

    1. I created embedded framework from this (**SWIFT LIBRARY) : https://github.com/NordicSemiconductor/IOS-DFU-Library

    2. I created Xamarin library with that framework and bind it to CSharp.(Wrapping)

    3. I success to build Xamarin library.

    4. getting runtime Error :

    Could not create an native instance of the type 'nRF5xDFULibraryXamariniOS.DFUServiceInitiator': the native class hasn't been loaded. It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.

    Now, I ran into this thread : https://forums.xamarin.com/discussion/63657/binding-swift-code

    Our existing binding tools do not work with Swift. If you have a way to bring this into Obj-c headers, then tooling like Objective Sharpie can consume it and create a binding definition. However, we don't currently support using a Swift library, so your mileage may vary.

    I'm getting error because it's a framework made by Swift? If so, I'm really screwed.

    Can you give me any advice?

    Sunday, August 14, 2016 11:05 PM

Answers

  • User37696 posted

    @BBright,

    That's correct. Our current tooling does not directly support binding Swift. Is there a way to expose the Swift framework via a static Obj-c library? If so, then you should be able to bind that Obj-c library. I am not familiar enough with native development to know if that's possible.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 15, 2016 1:23 PM

All replies

  • User37696 posted

    @BBright,

    That's correct. Our current tooling does not directly support binding Swift. Is there a way to expose the Swift framework via a static Obj-c library? If so, then you should be able to bind that Obj-c library. I am not familiar enough with native development to know if that's possible.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Monday, August 15, 2016 1:23 PM
  • User12211 posted

    If the library isn't compiled for whatever target platform you're running on, you'll get that error. For example, if you're trying to run on the simulator, the binary needs to have an x86 and/or an x8664 slice. See https://developer.xamarin.com/guides/ios/advancedtopics/bindingobjective-c/walkthrough/#CreatingaFatBinary for reference. Not sure how that works with Swift, but it should give you a starting point.

    Monday, August 15, 2016 3:14 PM
  • User221964 posted

    OK Thanks for answering my question @JohnMiller . Could I know any information about progress that you guys're working on that(supporting binding Swift)? So that I can decide weather I can stop here or not.

    Monday, August 15, 2016 3:15 PM
  • User221964 posted

    Oh, @DaveHunt . I believed I did make Fat framework. I built that framework by this way : http://stackoverflow.com/questions/26906600/how-to-create-universal-framework-in-xcode6

    Universal framework mentioned by that link, is not same with Fat binary? If so, I will have to do it again with following document you gave.

    Thanks.

    But as John says, Xamarin is not supporting binding Swift Library at this moment.

    Monday, August 15, 2016 3:21 PM
  • User12211 posted

    John says their current tooling doesn't support generating bindings for Swift. That's not the same as not being able to do it.

    You may have created a fat binary, but you may not have included all the necessary architectures for wherever you're trying to run it.

    Monday, August 15, 2016 4:55 PM
  • User221964 posted

    @DaveHunt Thanks again for answering my question. I don't quiet understand what you said but sounds like a good news. If you don't mind, could you check my binded library? https://github.com/myallb/nRF51DFULibraryXamarin_iOS

    Thanks.

    weird thing is, I'm sure all source code is written by Swift. https://github.com/NordicSemiconductor/IOS-DFU-Library

    But built framework's header file is Objc. What's the deal? is it natural?

    Monday, August 15, 2016 4:59 PM
  • User12211 posted

    When the creator of the framework includes @objc prefixes on externally accessible objects/methods/etc, an Obj-C header isautomatically created to provide an interface to those methods. Without that, you would not be able to call them from Obj-C or create bindings for Xamarin.

    I can't tell from your project whether you created a fat binary with all the necessary architectures. The link I provided you shows how to do that using a Makefile and Xcode.

    Monday, August 15, 2016 5:08 PM
  • User221964 posted

    My framework has arm, arm64, x86_64 achitectures. It's working fine on simulator and device when I tested with importing this framework to my original iOS Native app with XCode. So I thought it's fat framework.

    The link you gave me is dealing with .a library to combine those achitectures. I'm sure it's same with I've done to my framework. You can see my framework filefolder which I created. here : https://github.com/myallb/nRF51DFULibraryXamarin_iOS.

    in github, nRF51DFULibraryXamariniOS/nRF5xDFULibraryXamarin_iOS/DFULibrary.framework/

    Thanks again. @DaveHunt

    Monday, August 15, 2016 5:18 PM
  • User12211 posted

    Unfortunately, I'm at work where we don't have Macs, so there's not much I can look at.

    Monday, August 15, 2016 6:27 PM
  • User221964 posted

    @DaveHunt Thanks though!

    Monday, August 15, 2016 9:56 PM
  • User145901 posted

    Hey @BBright ,

    Hope you already succeeded to make static obj-c library that bind a swift framework and expose it with an obj-c header, So Objective Sharpie could do the wrapping stuffs, have you?

    Thursday, November 17, 2016 10:15 AM
  • User221964 posted

    @ArashDavidi

    Hi Arash. I'm sorry. I stopped digging more at that point. I don't use obj-c static library nor swift library currently.

    Thursday, November 17, 2016 3:12 PM
  • User232631 posted

    Anyone have Idea about IOS DFU with Xamarin. or any reference code

    Saturday, June 17, 2017 12:34 PM
  • User221964 posted

    @Abhi.7519

    Hey Abhi, if you're using nordic chip for ble communication, please check this thread. https://forums.xamarin.com/discussion/64859/porting-nordic-ble-dfu-fota-to-xamarin

    Tuesday, June 20, 2017 12:42 PM
  • User373674 posted

    @JohnMiller or anyone else who may know - Is it still the case that binding Swift libraries is not supported?

    I haven't found any Xamarin/Microsoft documentation on it, but wanted to make sure. Thanks!

    Friday, November 9, 2018 2:52 PM
  • User313403 posted

    R you still looking for Swift library integration? I found this while I am looking for Swift library integration https://medium.com/@Flash3001/binding-swift-libraries-xamarin-ios-ff32adbc7c76 and it worked pretty good for me.

    _I had to integrate ObjectiveC Code (which had some swift libraries in it) with my Xamarin iOS Project. _

    Monday, November 12, 2018 2:24 AM