Xamarin.Forms: How to put a native iOS class which implements DependencyService interface to an separate iOS library

I have a native iOS class which implements the Xamarin.Forms DependencyService, since I want to reuse this class in different projects, so I would like to extract this class from my current Xamarin.iOS app to a separate Xamarin.iOS library.

Here is my current project structure:

BDNC (Xamarin.Forms)

---IHTBluetoothManager (DependencyService Interface)

BDNC.iOS (Xamarin.iOS)

---HTBluetoothManager : IHTBluetoothManager (DependencyService Implementation)

I want to achieve something like:

BDNC (Xamarin.Forms)

---IHTBluetoothManager (DependencyService Interface)

BDNC.iOS (Xamarin.iOS App)

--- App codes

HTBluetoothKit (Xamarin.iOS Library)

---HTBluetoothManager : IHTBluetoothManager (DependencyService Implementation)

Please help.

2 answers

  • answered 2018-05-16 11:00 baskren

    I may be answering the wrong question so please don't hesitate to redirect me. The key here is in how you setup the references in each project. If BDNC is a Xamarin.Forms cross platform project and BDNC.iOS is your iOS platform implementation project you can think of BDNC.iOS as where everything comes together.

    In other words, BDNC.iOS needs to reference BDNC in order to get the Xamarin.Forms Models and ViewModels for the iOS app. Likewise, in order for BDNC.iOS to implement the IHTBluetoothManager for iOS, it needs to call methods in HTBluetoothManager. So, BDNC.iOS needs to reference HTBluetoothManager.

    When you create a cross-platform application using VisualStudio 2017 (Mac or Windows) it creates the reference to BDNC in BCNC.iOS for you. So the only thing you'll have to do is create the reference to HTBlueToothManager in BCNC.iOS: - In Mac, in the solution explorer pad, expand the BDNC.iOS project and right click on the References folder and select Edit References...* from the popup menu. The **Edit References dialog will appear. Click on the Projects tab, select the HTBluetoothManager project, and click [OK] to finish. - On PC, in the solution explorer pad, expand the BDNC.iOS project and right click on the References folder and select Add Reference...* from the popup menu. The **Reference Manager dialog will appear. Click on the Projects section (left bar), select the HTBluetoothManager project, and click [OK] to finish.

  • answered 2018-05-16 11:26 Ali123

    I think what you need to do is create a simple project and add your dependencies and code there for each platform.

    Xamarin has access to native libraries and you can add whatever you need and work with the API of any library.

    After that, you can create a nuget package which than you can use in all your other projects and share it with others if you want