Darius Darius - 2 months ago 30
iOS Question

iOS Widget Extension initial UIViewController

I am adding a "Today" extension widget to my iOS app that I made with Xamarin. I am following this walkthrough:


The widget appears in my Notification section in the simulator, but I can't get any contents to appear in it. It won't even create the UIViewController class that I made and set as the initial controller to start with (I know because it never hits my breakpoint in the constructor). I set it as the principal class with this key as explained in the walkthrough:

enter image description here

Any idea why? I also get this message when I first launch the app after adding the extension:

appname may slow down your phone the developer of this app needs to update it to improve its compatibility

I made a sample project, with Xamarin, and the widget does appear in this project when deployed on the simulator, just not with the contents that I'm trying to add in the CodeViewController class:



To save you 2 days I spent on it here is the solution.

  1. Don't run it on simulator. It doesn't work (at least on mine).
  2. Don't try to hit breakpoint in VS. When you testing your extension your app is in background mode. VS will not give you to stop in debugger. To prove run any of your apps, press home and try to set breakpoint in VS. VS will hang till you bring your app to foreground.
  3. Do not use View.Frame in DidLoad. The size of the frame there is the whole screen size, so when you put your label to center you will not see it. Use WillAppear like this

    public override void ViewWillAppear(bool animated)
        if (TodayMessage == null)
            // Add label to view
            TodayMessage = new UILabel(new CGRect(0, 0, View.Frame.Width, View.Frame.Height))
                TextAlignment = UITextAlignment.Center,
                BackgroundColor = UIColor.LightGray,
                TextColor = UIColor.Black
            // Calculate the values
            var dayOfYear = DateTime.Now.DayOfYear;
            var leapYearExtra = DateTime.IsLeapYear(DateTime.Now.Year) ? 1 : 0;
            var daysRemaining = 365 + leapYearExtra - dayOfYear;
            // Display the message
            if (daysRemaining == 1)
                TodayMessage.Text = String.Format("Today is day {0}. There is one day remaining in the year.", dayOfYear);
                TodayMessage.Text = String.Format("Today is day {0}. There are {1} days remaining in the year.", dayOfYear, daysRemaining);

enter image description here