iOS 9 Today Widget not calling viewDidLoad
Asked Answered
T

2

6

The today widget is drawn correctly when it is added to the today view. But if you user comes back to it later, the viewDidLoad function is not called and it is showing stale data. Should viewDidLoad be called everytime? Is there an iOS 9 / Xcode 7 beta 6 bug?

Edit: Added that widgetPerformUpdateWithCompletionHandler not called either. I have breakpoints set and print functions

func widgetPerformUpdateWithCompletionHandler(completionHandler: ((NCUpdateResult) -> Void)) {
    print("in widgetPerformUpdateWithCompletionHandler")
    fetch()

    completionHandler(NCUpdateResult.NewData)
}
Teleplay answered 31/8, 2015 at 20:10 Comment(3)
I have never worked with widgets but normally viewDidAppear is called every time screen is presented, viewDidLoad is only called when view initially loaded.Bumblebee
Are you not using widgetPerformUpdateWithCompletionHandler: see hereFlat
I've the same issue. widgetPerformUpdateWithCompletionHandler used to be called each time the widget was displayed. iOS9 calls this methods far less. But I didn't find any documentation about this change.Plexus
H
5

When you scroll a widget off and back on screen, the same controller instance will be reused for a short amount of time (appears to be ~30 seconds in my testing), and viewDidLoad and widgetPerformUpdateWithCompletionHandler: will not be called.

However, viewWillAppear and viewDidAppear will be called every time your widget is displayed.

Homogenous answered 1/9, 2015 at 15:5 Comment(2)
This fixed the issue for me as wellFlyleaf
Thanks yeah, I switched to viewDidAppear and now everything works, thanks :)Bobbe
T
0

Posting my own answer, but would like discussion on this code - should it be there or how to properly do it?. We had in this method, and by removing it the widget began to work correctly

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator)
{

    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    if let safeCoordinator = coordinator as UIViewControllerTransitionCoordinator?
    {
        print("coordinator != nil")
        safeCoordinator.animateAlongsideTransition({ context in
            self.tableView.frame = CGRectMake(0, 0, size.width, size.height)
            }, completion: nil)

    }
    else
    {
        print("coordinator == nil")
    }
}
Teleplay answered 31/8, 2015 at 20:57 Comment(3)
This code looks like you're trying to resize your table view in response to something else changing the height of your view. What is that something else? If you're using auto layout, your table view should resize to fit the number of cells it displays. Otherwise you can set preferredContentSize to specify exactly the height you want.Homogenous
@ChristopherPickslay Doesn't Apple discourage the use of preferredContentSize ?Bobbe
@Bobbe not that I'm aware of: If you don’t use Auto Layout, you can use the UIViewController property preferredContentSize to request a height for the widget. developer.apple.com/library/ios/documentation/General/…Homogenous

© 2022 - 2024 — McMap. All rights reserved.