Parse for Xamarin Forms - No PCL, my solution with conditional builds

About Parse

In my previous article (or next, we will see ;) I talk about Parse BaaS. This is a 'Back end As Services' that offers you simple but really convenient back end capabilities for storage but also managing users... I approach why I choose this technology for my mobile application, looking for advantages and drawbacks.
Parse, after being purchased by Facebook, was closed last year, but fortunately, the source code is open source and Parse still exists. So you can install your own server or use the new Parse on Buddy platform for instance. You can take a look here: https://buddy.com/parse/



The problem with Parse for Xamarin Forms

So, actually the problem is that we can find .NET Xamarin SDK but specifically for iOS and Android frameworks. There is no SDK for Xamarin Forms.

The ideal solution could be the following:



Many 'pseudo' solutions can be found on internet but I will give you mine, using conditional builds in Visual Studio.
I have to mention that some solutions found on internet will work on Visual Studio for PC but not with the new edition of Visual Studio for MAC... So don't be surprised


My 'pseudo' solution, conditional builds

Its an easy solution and just need few configuration steps described bellow. I just use build configurations and conditional building based on these configurations.

1- Download latest Parse's assemblies

The first is to download the latest official Parse's assemblies for android and iOS (in my case 1.7.0.0). I don't use Nuget packages for several reasons. The first is that there is multiple Nuget packages and we don't really know which one to choose (I tried and some don't work with Xamarin)...

Multiple Parse's nuget packages
You can download correct DLLs from here:
https://github.com/parse-community/Parse-SDK-dotNET/releases/tag/1.7.0


2- Copy Parse DLL alongside the nuget packages

Next, I placed these DLLs in a specific folder in my solution to embed it into my source control. I don't want them managed as Nuget packages because it's not.



3- Make new build configurations

Under my solution in Visual Studio I then next generate new build configurations based on the existing ones, as on the schema bellow. In my case I created a new build configuration designed specifically for iOS platform.


These new configurations are just copies of existing ones. Then in Visual Studio:


For more information on how to create new build definitions, please refer to the documentation:
https://msdn.microsoft.com/en-us/library/kkz9kefa.aspx


4- Conditional build depending on current build configuration

Why to create copy of a build configuration ?
In fact we will select the 'correct' Parse DLL depending on the current build configuration (depending on the platform we want to generate the application).

If 'Debug' --> Get Parse.Android to build the solution
if 'Debug iOS' --> Get Parse.iOS to build the solution

This is done by manually modifying the *.csproj (the project) that use Parse. In our example, we would modify 'BusinessLayer.csproj'. We have to add the conditional reference to the correct Parse DLL:

  <Choose>
    <When Condition="'$(Configuration)' != 'DebugiOS'">
      <ItemGroup>
        <Reference Include="Parse.Android, Version=1.7.0.0, Culture=neutral, processorArchitecture=MSIL">
          <HintPath>..\..\packagesExt\Parse.Android\Parse.Android.dll</HintPath>
        </Reference>
      </ItemGroup>
    </When>
    <When Condition="'$(Configuration)' == 'DebugiOS'">
      <ItemGroup>
        <Reference Include="Parse.iOS, Version=1.7.0.0, Culture=neutral, processorArchitecture=MSIL">
          <HintPath>..\..\packagesExt\Parse.iOS\Parse.iOS.dll</HintPath>
        </Reference>
      </ItemGroup>
    </When>
  </Choose>

*.csproj file should look something like that:

5- Select build configuration to build expected platform's app

Now, you just have to select the correct build configuration to build your app. It will automatically include the Parse DLL corresponding to the platform for which you are building the app.

Warning In visual Studio for MAC, in the solution explorer, the reference is not refreshed automatically. But the build process choose the right assembly.

Bellow, building for iOS reference only Parse for iOS assembly:

And voila !
If you have any question don't hesitate.

Comments

Popular posts from this blog

EMGU with Xamarin Forms guide [part 1]

[Xamarin Forms] Custom bottom bordered entry (iOS & Android)

Set your browser on Fire with P5.js