Monday 7 October 2019

Xamarin iOS 13 App Crash Resolution

Coming back to wanting to build the iOS version of an application, I ran into an issue where the application crashed on startup. Before this the following occurred :-

  • Visual Studio 2019 on the Mac was upgraded
  • Visual Studio 2017 on the PC no longer built the application
    • Wanting to downgrade what was on the Mac in terms of Xamarin.iOS version
  • Visual Studio 2019 on the PC was installed
  • iPhone 7 was upgraded to iOS 13
From Debugging, it seemed to be related to prompting for permission of the Location services, which sent me down a certain path, which then lead me to the following thread :-


A post on this thread by sschmidTU pointed me in the direction of viewing the console from the iOS device via XCode :-


"Also, to get console output for your crash on a release version on device:
connect your device to your Mac
open XCode
go to Window -> Devices and Simulators
click Open Console for your device
in the Console, type your app name in the search window to filter the output."

Having filtered the console output using the application name, I found that the application was crashing because the Bluetooth Permission was being prompted for however I didn't have the NSBluetoothAlwaysUsageDescription key in my Info.plist.

Looking at the documentation for this key, it is new in iOS 13 :-


Once the NSBluetoothAlwaysUsageDescription key was added, the application no longer crashes on startup.

Thursday 1 March 2018

How do I automatically version Xamarin.Android Library projects?

I had run into this issue when trying to build NuGet packages for Android projects before but had forgotten the solution.

The following thread details the resolution :-

https://forums.xamarin.com/discussion/48765/how-do-i-automatically-version-xamarin-android-library-projects

In summary, ensure the appropriate Mono.Android.dll version is copied to the \bin\Debug folder for your project and then the various details will be picked up from the AssemblyInfo.cs, including the Version number.

Thursday 7 September 2017

iOS P12 Creation

When exporting a P12 from Key Chain Access, you should right click on the certificate and click Export and not export against the Private Key.

Tuesday 9 May 2017

iOS App Package Creation

We had a requirement to create .app packages of our Xamarin Forms application for submission to a third party. The requirement was to create this for a iPhone 6 iOS 10.2 Simulator.

Initially I thought rebuilding the solution in Visual Studio 2015 would be enough as this would create what looked like the .app package however when using the following command line :-

xcrun simctl install booted /users/[username]/desktop/certificates/[username]/[packagename].app 

This resulted in a "Failed to chmod : No such file or directory" error.

 To resolve this issue I found that along with rebuilding the solution in Visual Studio, you need to deploy the application to the simulator, which inflates the .app package file by ~50MB, and on inspection of the before and after .app packages, the DLLs and EXE are included which allows the .app package file to be deployed to the simulator successfully, using the command line above.

Friday 10 April 2015

StringToColorConverter for Xamarin.Forms

You may have a requirement to bind a string property to a Color property, such as TextColor. If this is the case then you will need to create a converter to perform the conversion from string to Color. The following is a sample of such a converter :-

Firstly, the converter :-

using System;
using System.Globalization;
using Xamarin.Forms;
namespace LabelTextColorSample
{
    public class StringToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string valueAsString = value.ToString();
            switch (valueAsString)
            {
                case (""):
                    {
                        return Color.Default;
                    }
                case ("Accent"):
                    {
                        return Color.Accent;
                    }
                default:
                    {
                        return Color.FromHex(value.ToString());
                    }
            }
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

Then the XAML which consumes it :-


    
        
            
        
    
    
Then the setting of the BindingContext :-

using Xamarin.Forms;
namespace LabelTextColorSample
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            this.BindingContext = MyViewModel.Instance;
        }
    }
}

And finally the ViewModel :-

namespace LabelTextColorSample
{
    public class MyViewModel
    {
        private static MyViewModel _instance;
        public static MyViewModel Instance
        {
            get { return _instance ?? (_instance = new MyViewModel()); }
        }
        public string MyTextColor
        {
            get { return "#00FF00"; }
        }
    }
}

Hope this is helpful.

Wednesday 8 April 2015

Range Slider Renderer for Xamarin.Forms

Based on the Range Slider component in the Xamarin Components store, I have created a renderer so that the Range Slider can be used in Xamarin.Forms.

Firstly, I created a RangeSlider control in the Xamarin.Forms PCL, as follows :-
using Xamarin.Forms;

namespace RangeSliderSample
{
 public class RangeSlider : View
 {
  public static readonly BindableProperty LeftValueProperty =
   BindableProperty.Create(p => p.LeftValue, 0f);

  public float LeftValue
  {
   get { return (float) GetValue(LeftValueProperty); }
   set { SetValue(LeftValueProperty, value); }
  }

  public static readonly BindableProperty RightValueProperty =
   BindableProperty.Create(p => p.RightValue, 0f);

  public float RightValue
  {
   get { return (float) GetValue(RightValueProperty); }
   set { SetValue(RightValueProperty, value); }
  }

  public static readonly BindableProperty MaxValueProperty =
   BindableProperty.Create(p => p.MaxValue, 1f);

  public float MaxValue
  {
   get { return (float) GetValue(MaxValueProperty); }
   set { SetValue(MaxValueProperty, value); }
  }

  public static readonly BindableProperty MinValueProperty =
   BindableProperty.Create(p => p.MinValue, 0f);

  public float MinValue
  {
   get { return (float) GetValue(MinValueProperty); }
   set { SetValue(MinValueProperty, value); }
  }

  public static readonly BindableProperty StepProperty =
   BindableProperty.Create(p => p.Step, 0f);

  public float Step
  {
   get { return (float) GetValue(StepProperty); }
   set { SetValue(StepProperty, value); }
  }
 }
}

I then placed this control on a XAML page :-

 
  
  
  
 



I then created a Custom Renderer in the Android project :-
using RangeSlider;
using RangeSliderSample.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(RangeSliderSample.RangeSlider), typeof(RangeSliderRenderer))]
namespace RangeSliderSample.Droid
{
 public class RangeSliderRenderer : ViewRenderer
 {
  private RangeSliderView _slider;
  
  protected override void OnElementChanged(ElementChangedEventArgs e)
  {
   base.OnElementChanged(e);

   var rangeSlider = e.NewElement as RangeSlider;

   if (rangeSlider != null)
   {
    _slider = new RangeSliderView(Context, rangeSlider.MinValue, rangeSlider.MaxValue, rangeSlider.Step);

    _slider.LeftValueChanged += value =>
    {
     rangeSlider.LeftValue = _slider.LeftValue;
    };

    _slider.RightValueChanged += value =>
    {
     rangeSlider.RightValue = _slider.RightValue;
    };

    SetNativeControl(_slider);
   }
  }
 }
}

The complete solution is here :-
http://www.smartmobiledevice.co.uk/Samples/Xamarin/RangeSliderSample.zip

Xamarin Android Player and McAfee

I normally use a device for all my debugging in Xamarin however I thought I would try the Xamarin Android Player.

After installing and downloading an emulator image I tried starting the emulator however I was present with the following error :-

 OpenGL server is unreachable. Please check that Xamarin Android Player is allowed through your firewall on public networks.

 As I use McAfee, my firewall settings are handled by McAfee rather than the Windows Firewall. I viewed the Firewall settings for the Xamarin Android Player (AndroidPlayer) under "View firewall and anti-spam settings > Firewall > Internet Connections for Programs", and it was set to use "Designated Ports". Editing this entry and setting Incoming and Outgoing to "Open ports to Work and Home networks" now allows me to use the Xamarin Android Player.