问题
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):
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.
回答1:
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).
回答2:
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:
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.
回答3:
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.
回答4:
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.
回答5:
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.
来源:https://stackoverflow.com/questions/24128024/today-extension-has-a-title-but-no-body-ios-8