Sharing code across Apple platforms

mhutchmhutch USXamarin Team Xamurai
edited December 2015 in Xamarin.iOS

We've just made available a new set of NuGet packages that allows you to build PCLs (Portable Class Libraries) that use Apple APIs and work on multiple Apple platforms. For example, you can build a library that uses CoreGraphics that can be used from iOS, Mac, tvOS and watchOS projects.

Each NuGet is an intersection of several Apple platform bindings (Xamarin.iOS.dll, Xamarin.Mac.dll, Xamarin.tvOS.dll, Xamarin.watchOS.dll). It contains the types and members that exist in all of the intersected bindings. Multiple intersection NuGets exist for different combination of platforms.

NOTE: THIS IS AN EARLY PREVIEW. The APIs may change between now and the final release. Please provide feedback to help us improve and move it toward a final release!

How to Use It

To use an intersection NuGet, you must reference it from your PCL and app projects.

  1. Create a PCL project and app projects, for example Xamarin.iOS and Xamarin.Mac. The PCL project should use Profile7.
  2. From the PCL project, reference an intersection NuGet that is compatible with all of your app projects, for example Xamarin.Apple.Core. Note that currently you must enable “Show pre-release packages” in the Add Packages dialog.
  3. From each of the app projects, reference the PCL project and the intersection NuGet.
  4. Write shared code in the PCL using Apple APIs.
  5. Use the shared code from your app projects!

How It Works

Each NuGet contains a contract assembly that gets used when compiling the PCL. This contains a subset of the types and members that exist in the binding assemblies. The NuGet also contains platform-specific facade assemblies that redirect the type references to the real binding assemblies when you consume the PCL from an app project.

The types and members in the contract assemblies are stubs. If you try to use the NuGet on an unsupported platform (e.g. Android) you will get a build error or NotImplementedExceptions.

Intersections

There are currently four intersections: Xamarin.Apple.Core, Xamarin.Apple.iOSTVMac, Xamarin.Apple.iOSTV and Xamarin.Apple.iOSMac. These have been chosen because they support common code sharing scenarios. If there are additional intersections that would be useful to you, or important types and members missing from the intersections, please let us know.

Intersections are compatible with their subsets, for example a library that references Xamarin.Apple.Core can be used from a library that references Xamarin.Apple.iOSTVMac.

Xamarin.Apple.Core

Intersects: Xamarin.iOS.dll, Xamarin.tvOS.dll, Xamarin.Mac.dll, Xamarin.WatchOS.dll.

APIs: CoreData, CoreFoundation, CoreGraphics, CoreLocation, Foundation, ImageIO, Security, System.NMath

Xamarin.Apple.iOSTVMac

Superset of Xamarin.Apple.Core.

Intersects: Xamarin.iOS.dll, Xamarin.tvOS.dll, Xamarin.Mac.dll.

APIs: Xamarin.Apple.Core APIs plus Accelerate, AudioToolbox, AudioUnit, AVFoundation, CloudKit, CoreAnimation, CoreBluetooth, CoreFoundation.CFNetwork, CoreImage, CoreText, CoreVideo, Foundation.NSNetService, GameController, GameKit, GameplayKit, GLKit, JavaScriptCore, MediaAccessibility, Metal, MetalKit, ModelIO, OpenTK, SceneKit, SpriteKit, StoreKit, SystemConfiguration.

Xamarin.Apple.iOSTV

Superset of Xamarin.Apple.iOSTVMac.

Intersects: Xamarin.iOS.dll, Xamarin.tvOS.dll.

APIs: Xamarin.Apple.iOSTVMac APIs plus AudioToolbox, AVFoundation, CoreBluetooth, CoreLocation, GameKit, GameplayKit, GLKit, MediaPlayer, MediaToolbox, MetalKit, MetalPerformanceShaders, OpenTK, SceneKit, SpriteKit, UIKit.

Xamarin.Apple.iOSMac

Superset of Xamarin.Apple.iOSTVMac.

Intersects: Xamarin.iOS.dll, Xamarin.Mac.dll.

APIs: Xamarin.Apple.iOSTVMac APIs plus Accounts, AVFoundation, Contacts, CoreLocation, CoreMidi, CoreServices, EventKit, Foundation, GameKit, LocalAuthentication, MapKit, NetworkExtension, Social, VideoToolbox, WebKit.

Posts

  • mhutchmhutch USXamarin Team Xamurai

    The packages have been updated to 0.2-alpha.

    Please note that because watchOS is included in the core intersection, it was necessary to generate the intersections using prerelease versions of Xamarin.iOS and Xamarin.Mac. As a consequence, some types and members may not resolve against the currently available Xamarin.iOS and Xamarin.Mac releases.

  • huangjinshehuangjinshe USMember ✭✭✭
    edited September 2016

    @mhutch Is there any progress about WCF Duplex channel for Xamarin.IOS and Xamarin.Mac?

  • BlackBladesBlackBlades USMember

    Each NuGet is an intersection of several Apple platform bindings (Xamarin.iOS.dll, Xamarin.Mac.dll, Xamarin.tvOS.dll, Xamarin.watchOS.dll). It contains the types and members that exist in all of the intersected bindings. Multiple intersection NuGets exist for different combination of platforms.

    This is nice and definitlely offers great convinenecef or the developers.

  • tofutimtofutim USUniversity ✭✭

    @mhutch Is the XamMac here xamarinmac20?

  • TedRogersTedRogers USMember ✭✭✭✭

    @mhutch Could I use this to share iOS code between main app and app extensions? If so, can I edit Xib's in the PCL which is a current problem I am wrestling with in shared projects.

  • mhutchmhutch USXamarin Team Xamurai

    @TedRogers in that case you should just be able to use an iOS library project.

Sign In or Register to comment.