15

I am trying out the new TodayExtensions in iOS 8 and I followed the steps to create a Today Extension as described in the WWDC video Creating Extensions for iOS and OS X, Part 1. I added a colored UIView to the ViewController in the provided storyboard. I get a title in my "Today" Notification center, but I get no body with my colored view. It looks like this (I made two):

enter image description here

Is anyone else getting this? I set breakpoints in all of my ViewControllers methods and nothing gets called. I changed my Info.plist to just go directly to my VC class, instead of the storyboard and I get nothing still. I can change the title of the today extension in the info.plist.

Andrew
  • 15,357
  • 6
  • 66
  • 101
SirRupertIII
  • 12,324
  • 20
  • 72
  • 121
  • I found that sometimes the debugger fails to attach to the widget on the device. One thing you can do (albeit tedious) is set `NSLog` statements throughout your code and check in the device's log. You can get to it in Xcode by going to Window > Organizer > Devices then near the bottom of the window you'll see a little expand icon. Note that you'll likely see various outputs from the system and/or other apps so you may need to manually sift through those to see your widget's log statements. – Yazid Jun 10 '14 at 12:02
  • As for the body, try adding an explicit Auto Layout height constraint to your colored `UIView`. – Yazid Jun 10 '14 at 12:04
  • 2
    possible duplicate of [iOS 8 Beta Today extension widget not showing in a Swift app?](http://stackoverflow.com/questions/24074691/ios-8-beta-today-extension-widget-not-showing-in-a-swift-app) – Andrew Jun 10 '14 at 16:11
  • 1
    Check your crash logs. It is actually _crashing_. – Andrew Jun 10 '14 at 16:12
  • 1
    I've found that the only thing that lets me see the body of my Today Extension after making a change is to hit the iOS Simulator "Rest Content and Settings" option. This doesn't work if you set the _preferredContentSize_ setting though. I think I will be waiting for Beta 3 to see if things improve. – Anthony C Jul 07 '14 at 01:30
  • 1
    (Beta 3 released today, things are much better) – Anthony C Jul 07 '14 at 20:26

5 Answers5

9

First, to test that anything is happening, add awakeFromNib to your view controller and set preferred content size (all code in Obj C):

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setPreferredContentSize:CGSizeMake(self.view.bounds.size.width, 50)];
}

As milesper said above, comment out the default init method and create an empty initWithCoder: to get around some bug in Beta 2:

-(id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        // init
    }
    return self;
}

//- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
//    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
//    if (self) {
//        // Custom initialization
//    }
//    return self;
//}

Now Clean and then run again. At this point you should see it resize (make sure you add a label with text or something to test).

Make sure you test with a plain UIViewController class, not a subclass. Once you see your widget size respond, then try a subclass. I spent an hour today just to find out that using UICollectionViewController simply doesn't work in Beta 2 (will file a RADAR).

Alex the Ukrainian
  • 4,528
  • 3
  • 24
  • 25
  • Worked for me, great answer! This bug is really annoying. – Viper Jul 05 '14 at 18:33
  • After setting the `preferredContentSize`, the height of the view of the extension's ViewController will shrink the next time opening it, what's wrong? – liushuaikobe Jun 07 '15 at 14:54
7

If you are running the app scheme and not the widget scheme, first thing to check will be the Device log or the Simulator log. As the Today's view is part of the system and not part of the app that you are debugging in Xcode, you won't see errors on the widget view controller on the Xcode's console. You can check the simulator's console on the System Log:

Simulator's log

If there was a crash on the widget view controller it will show something like this:

xxxxx.local Widget[43414]: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[TodayViewController 0x7fd893d7ca60> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key label.'

If you select the widget target instead, xcode will attach the debugger to the today's view widget and if you have the 'All exceptions' breakpoint' enabled, you'll be able to see if there is an exception and where is being rised.

Widget target

pablobart
  • 2,641
  • 1
  • 24
  • 22
2

Comment out the original init method:

//    init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) {
//        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
//        // Custom initialization
//    }

Add this init method:

init(coder aDecoder: NSCoder!) {
   super.init(coder: aDecoder)
   // Custom initialization here
}

Clean and Build your project. Make sure your widget content has a height constraint.

mginn
  • 16,036
  • 4
  • 26
  • 54
1

I struggled a bit with this.

Then I figured out that it was trying to infer the height of the View using the Constraints for all subviews within the MainInterface.storyboard.

If you add all the necessary constraints to your view's subviews then the height of the Today Extension can be inferred and your view will appear as you intend.

Seoras
  • 1,286
  • 13
  • 21
  • This was what made the content appear for me as well. I had to have vertical constraints between elements, height constraints on the elements, and constraints to the top of container for first element and to bottom of container for last element. It's like it couldn't figure out the height without everything in autolayout totally explicit. – JeremyWeir May 17 '15 at 04:12
  • I tried this but it didn't work for me. Set explicit height and width on every element, as well as explicit spacing between them and the borders of the container. – morphatic Jul 08 '15 at 06:25
0

I ran into the same problems like you did with different problems.

1) If you often restart your widget from Xcode it gets killed on the phone and it seems the system punishes you for this, it's a good idea to always close the today view before you kill your widget in Xcode so it gets "nicely" ended instead of killed by Xcode

Solution: Sometimes the only fix is to remove the widget from the today view and read it (and even sometimes to remove the app from the phone and install it again)

2) I was using a logging framework and every time I tried to upload data to the server the widget froze and wasn't reacting anymore. When I then closed the notification center and reopened it I had the same problem like you with either an empty today widget or a 0 height today widget.

Solution: Not a good one though, not uploading data from the widget code... the really strange thing there is, in debug builds everything works fine but not on a release build.

hashier
  • 4,670
  • 1
  • 28
  • 41