0

I have recently added a Widget to my application, or have tried to. When I run it the widget shows in the notification center but it appears collapsed. I have confirmed the viewDidLoad is not being called by using NSLog. Any ideas why its not loading? Thanks.

Edit: it seems the problem is the label won't be added I had to create everything in the interface builder.

.h file

#import <UIKit/UIKit.h>
#import <NotificationCenter/NotificationCenter.h>

@interface TodayViewController : UIViewController {
    UILabel *lblCurrentLocation;

}
#define SCREEN ([[UIScreen mainScreen] bounds])

@end

.m file

#import "TodayViewController.h"

@interface TodayViewController () <NCWidgetProviding>

@end

@implementation TodayViewController

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    lblCurrentLocation = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
    lblCurrentLocation.text = @"Current Location: Unknown";
    [self.view addSubview:lblCurrentLocation];
    NSLog(@"Set up");

}


- (void)widgetPerformUpdateWithCompletionHandler:(void (^)    (NCUpdateResult))completionHandler {
// Perform any setup necessary in order to update the view.

// If an error is encountered, use NCUpdateResultFailed
// If there's no update required, use NCUpdateResultNoData
// If there's an update, use NCUpdateResultNewData

completionHandler(NCUpdateResultNewData);
}

@end
bolencki13
  • 89
  • 1
  • 8

3 Answers3

0

Happens to me too (a lot). As far as I know there are a lots of troubleshooting while debugging the widget.

Try the following steps :

  1. Stop the widget from running.
  2. Remove the widget from Today view in notification center, by tapping on "Edit" button on the bottom of notification center.
  3. Close the notification center.
  4. Run the widget again.
Tsahi Deri
  • 571
  • 1
  • 3
  • 14
  • I got it working the problem was I couldn't programmatically add labels (didn't try anything else) I ended up using the interface builder – bolencki13 Mar 15 '15 at 23:31
0

Please check class name TodayViewController assign to your main interface builder.

Anjali jariwala
  • 410
  • 5
  • 15
-1

In case of Today extension, it is normal that viewDidLoad is not getting called everytime. But, viewWillAppear and viewDidAppear get called everytime. For loading new data, you should put the code in this method:

func widgetPerformUpdate(completionHandler: ((NCUpdateResult) -> Void))

Commonly, in case of today extension, we use above method in place of viewDidLoad for getting and setting data. This method is going to be called whenever new data required.(It commonly called everytime, but if you are switching NotificationCenter frequently, this one only going to called first time). So, depending on your need use required method.

For further details, you can refer: https://developer.apple.com/library/archive/documentation/General/Conceptual/ExtensibilityPG/Today.html

Mehul Thakkar
  • 12,440
  • 10
  • 52
  • 81