Tuesday, December 23, 2014

Mailing using Device.OpenUri on iOS in Xamarin Forms

Short version

Device.OpenUri will not work if passing it a mailto-uri on iOS that contains spaces or escaped spaces. Check out work around at the bottom.

Long version

The "issues" are iOS specific since Android seems to swallow all that comes its way.

The starting point is these two lines of code placed somewhere in the shared code (PCL or shared) in a Xamarin Forms solution.

    var uri = "mailto:info@johankarlsson.net?subject=subject&body=body";
    Device.OpenUri(new Uri(uri));


This works fine!

Adding spaces, not working

    var uri = "mailto:info@johankarlsson.net?subject=more in subject&body=body";
    Device.OpenUri(new Uri(uri));


This gives you an "System.Exception: Could not initialize an instance of the type 'MonoTouch.Foundation.NSUrl': the native 'initWithString:' method returned nil."

Alright, so we URI encode it to

    var uri = "mailto:info@johankarlsson.net?subject=more%20in%20subject&body=body";
    Device.OpenUri(new Uri(uri));


Same issue... And that's a bit weird since Apples own documentation states that this is the way to do it. (https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MailLinks/MailLinks.html#//apple_ref/doc/uid/TP40007899-CH4-SW1)

Trying Apples own example Uri

With a copy of the example Uri from Apples documentation it should work.

    var uri = "mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20";
    Device.OpenUri(new Uri(uri));
  

It does not.

Conclusion

This seems to be a bug for Bugzilla to handle... (https://bugzilla.xamarin.com/show_bug.cgi?id=25584)

Workaround


No comments:

Post a Comment