Widgets
Some times we need to create the widgets for an application so the user can get easy access to application’s functionality. it is used in many ways depending upon your requirement.
There are some rule and regulation to follow for creating the widgets.
1: open a Xcode project.
2: Click on file ->new -> target -> and then select Today Extension.
3: Now give the name like the sample.
4: Now it will automatically create a separate project for widgets.
5: Now select the “sample” project as the target to test the widgets.
6: Now open their view controller like “TodayViewController”
7: It will provide the one view controller here we can design according to requirement.
Note: it will not scroll so view is fixed
8: this method will call every update:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) { // Perform any setup necessary in order to update the view. // If an error is encountered, use NCUpdateResult.Failed // If there's no update required, use NCUpdateResult.NoData // If there's an update, use NCUpdateResult.NewData print("auto update") let defaults = UserDefaults(suiteName: kAppGroupName) let number: String = (defaults?.string(forKey: "myName"))! //labelName.text = number completionHandler(NCUpdateResult.newData) } |
here we can write that method which has to update every time.
or we can use “view will appear”, “view disappear”
9: when you run the application it will show a default message in widgets.
10: It will provide the “show more or less” in widgets so for this we have to use.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@available(iOSApplicationExtension 10.0, *) public func widgetActiveDisplayModeDidChange(_ activeDisplayMode: NCWidgetDisplayMode, withMaximumSize maxSize: CGSize){ if #available(iOSApplicationExtension 10.0, *) { if (activeDisplayMode == NCWidgetDisplayMode.compact) { self.preferredContentSize = maxSize; } else { self.preferredContentSize = CGSize(width: 0, height: 400); } } else { // Fallback on earlier versions } } |
11: Now you can design the widgets according to your need.