Search code examples
c#iosxamarin.iosnsurl

NSUrl request does not work in a Xamarin Today Widget


hey Guys I am working on a Today Widget in Xamarin using C# that open's a certain app that is installed on the iPhone. I'm using NSUrl request to do this however when I run the widget and click on a button it says unable to load

here's my code:

 using Foundation;
using System;
using System.CodeDom.Compiler;
using UIKit;
using NotificationCenter;

namespace ToolBelt
{
    partial class ToolBeltView : UIViewController
    {
        public ToolBeltView (IntPtr handle) : base (handle)
        {
        }
        public override void ViewWillAppear (bool animated)
        {
            base.ViewWillAppear (animated);

            var controller = NCWidgetController.GetWidgetController ();
            controller.SetHasContent(true, "com.d4a.toolbelt.Widget");
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            webbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://www.google.com");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

            emailbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("mailto:");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

            musicbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("music://");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };


            itunesbutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://itunes.apple.com/genre/music/id36?mt=8");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };


            appstorebutton.TouchUpInside += delegate {
                NSUrl request = new NSUrl ("http://itunes.apple.com/genre/mobile-software-applications/id36?mt=8");

                try {
                    UIApplication.SharedApplication.OpenUrl (request);
                } catch (Exception ex) {
                    Console.WriteLine ("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView ("Error", ex.Message, null, "OK", null);

                    alertView.Show ();
                }
            };

        }

    }

}

i'm new to Xamarin Development so any help would be amazing!


Solution

  • Extensions have their own way of opening URLs (since the device could be locked when opening a URL on an extension), so you need to use the ExtensionContext

    ExtensionContext.OpenUrl(new NSUrl("yoururl://"), null);