Henry Short

Henry Short

1574475144

How to Create iOS Widget (Today Extension)

People have so many apps on their phone and this inevitably makes it very difficult for apps to be remembered on a daily basis. The today screen is something most iPhone users view every day, and if you have a place on this screen, your app won’t be forgotten by the user.

One of my favorite today widgets is Coinbase’s cryptocurrency price widget. If you are interested in cryptocurrencies, this widget makes it very easy to track prices.

Let’s talk about where to start if you want to build something like this for your app.

Add Today Extension Target Into Your Project

View your project and click on + to add a new target. Search for Today and select Today Extension to add to your project.

This is image title

This will create some files under afolder in your project. TodayViewController is where you will implement everything you need.

You can treat this view controller as a standard view controller. The only difference will be updating the views and the memory limit you get with this extension.

Here’s what Apple says about today widgets:

“A Today widget should:

Ensure that content always looks up to date.

Respond appropriately to user interactions.

Perform well (in particular, iOS widgets must use memory wisely or the system may terminate them).”

The memory limit for the widget is 16 MB, so keep it simple and make sure to test out your widget for memory leaks.

Share Data Between Your App and Extension

The only way to share data between your app and extension is by using _App Group_s and UserDefaults.

First, you will need to add App Group capability to your app target. Click on + Capability button and select App Groups. Click on _+_ and type a name for your group.

This is image title

Do the same thing for your today widget target.

Now, create a file called GroupDataManager.swift to implement the required functions to save, remove, and retrieve data.

import Foundation

class GroupDataManager {
    
    static let shared = GroupPersistenceManager()
    let sharedDefaults = UserDefaults(suiteName: "group.todaymedium")
  
    func saveData(value: Any, key: String) {
        sharedDefaults?.setValue(value, forKey: key)
    }
    
    func retrieveData(key: String) -> Any? {
        return sharedDefaults?.value(forKey: key)
    }
    
    func removeData(key: String) {
        sharedDefaults?.removeObject(forKey: key)
    }
}

GroupDataManager.swift

Make sure this file is available for both of your targets.

This is image title

GroupDataManager.swift — File Inspector

Now, go to the view controller in your app and save data to retrieve from your widget.

GroupDataManager.shared.saveData(value: “Hello Medium!”, key: “dataKey”)

In your TodayViewController.swift of your today widget, retrieve this data by using the retrieve function.

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {
        
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.label.text = GroupDataManager.shared.retrieveData(key: "dataKey") as? String
    }
        
    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        completionHandler(NCUpdateResult.newData)
    }
    
}

TodayViewController.swift

You can simply display this string in the label to see if everything is working as expected.

This is image title

Now, run your app and add the widget to your today screen to see how it looks.

#ios #Widget

What is GEEK

Buddha Community

How to Create iOS Widget (Today Extension)
Henry Short

Henry Short

1574475144

How to Create iOS Widget (Today Extension)

People have so many apps on their phone and this inevitably makes it very difficult for apps to be remembered on a daily basis. The today screen is something most iPhone users view every day, and if you have a place on this screen, your app won’t be forgotten by the user.

One of my favorite today widgets is Coinbase’s cryptocurrency price widget. If you are interested in cryptocurrencies, this widget makes it very easy to track prices.

Let’s talk about where to start if you want to build something like this for your app.

Add Today Extension Target Into Your Project

View your project and click on + to add a new target. Search for Today and select Today Extension to add to your project.

This is image title

This will create some files under afolder in your project. TodayViewController is where you will implement everything you need.

You can treat this view controller as a standard view controller. The only difference will be updating the views and the memory limit you get with this extension.

Here’s what Apple says about today widgets:

“A Today widget should:

Ensure that content always looks up to date.

Respond appropriately to user interactions.

Perform well (in particular, iOS widgets must use memory wisely or the system may terminate them).”

The memory limit for the widget is 16 MB, so keep it simple and make sure to test out your widget for memory leaks.

Share Data Between Your App and Extension

The only way to share data between your app and extension is by using _App Group_s and UserDefaults.

First, you will need to add App Group capability to your app target. Click on + Capability button and select App Groups. Click on _+_ and type a name for your group.

This is image title

Do the same thing for your today widget target.

Now, create a file called GroupDataManager.swift to implement the required functions to save, remove, and retrieve data.

import Foundation

class GroupDataManager {
    
    static let shared = GroupPersistenceManager()
    let sharedDefaults = UserDefaults(suiteName: "group.todaymedium")
  
    func saveData(value: Any, key: String) {
        sharedDefaults?.setValue(value, forKey: key)
    }
    
    func retrieveData(key: String) -> Any? {
        return sharedDefaults?.value(forKey: key)
    }
    
    func removeData(key: String) {
        sharedDefaults?.removeObject(forKey: key)
    }
}

GroupDataManager.swift

Make sure this file is available for both of your targets.

This is image title

GroupDataManager.swift — File Inspector

Now, go to the view controller in your app and save data to retrieve from your widget.

GroupDataManager.shared.saveData(value: “Hello Medium!”, key: “dataKey”)

In your TodayViewController.swift of your today widget, retrieve this data by using the retrieve function.

import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {
        
    @IBOutlet weak var label: UILabel!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.label.text = GroupDataManager.shared.retrieveData(key: "dataKey") as? String
    }
        
    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        completionHandler(NCUpdateResult.newData)
    }
    
}

TodayViewController.swift

You can simply display this string in the label to see if everything is working as expected.

This is image title

Now, run your app and add the widget to your today screen to see how it looks.

#ios #Widget

iOS App Development Company in Singapore

iOS app development in Singapore

iOS has become the first priority for most smartphone users because of the security it offers compares to the Android operating system. Due to this reason, it is suggested to launch an app in iOS before other platforms.

Want to develop an iOS app in Singapore?

WebClues Infotech with its worldwide reach has already offered its iOS app development services to customers in Singapore. With a highly-skilled development team of 120+ members, WebClues Infotech has got the required resources an agency needs to fulfil client requirements around the world.

Want to know more about our iOS app development services in Singapore?

Visit: https://www.webcluesinfotech.com/iphone-app-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#ios app development in singapore #ios app development company #ios app development #ios #ios app #hire ios developer

iOS App Development in the United Arab Emirates

iOS App Development in the United Arab Emirates

Developed and Developing nations have seen a rapid rise in the demand for iOS app development and the United Arab Emirates is no exception. The use of on-demand apps has increased drastically in the last decade and business is leveraging this demand with launching iOS mobile apps.

Want to develop the iOS app in the United Arab Emirates?

WebClues Infotech after serving multiple clients in UAE has become well aware of the people’s needs in the region. With a highly experienced development team that has completed more than 950+ projects, we are prepared to serve you with your iOS app development needs.

Want to know more about our iOS App Development Services in UAE?

Visit: https://www.webcluesinfotech.com/iphone-app-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#ios app development in the united arab emirates #ios app development #ios app #ios #ios app development company #hire ios developer

iOS App Development Company in India

iOS app development company in India

India is considered the IT hub of the world because of n number of IT infrastructure development services offering companies. In this whole market iOS app development is the leading service offered by agencies across India

Want to develop the iOS app in India

WebClues Infotech with its head office in India has created a huge presence across the world over time and has served clients in all of the major countries around the world. WebClues Infotech with a highly skilled development team of 120+ members can help you deliver a better result at a reasonable cost.

Want to know more about our iOS app development services in India?

Visit: https://www.webcluesinfotech.com/iphone-app-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#ios app development company in india #ios app development company #ios app development #ios app #ios #hire ios app developer

iOS App Development Agency in the USA

iOS App Development Agency in the USA

Whenever a start-up in the USA launch an MVP version of the app they prefer to launch it only for iPhone user because the US has a large market of iPhone users in comparison to Android. The recent phenomenon of Clubhouse is the biggest example.

Want to develop an iOS app in the USA?

With 2 office locations across the USA and 6 worldwide, WebClues Infotech has the experience of serving a huge client base of 600+. After such a satisfied client base, WebClues Infotech is prepared to serve you with and iOS App Development Services in the USA.

Want to know more about our iOS App Development Services in the USA?

Visit: https://www.webcluesinfotech.com/iphone-app-development/

Share your requirements https://www.webcluesinfotech.com/contact-us/

View Portfolio https://www.webcluesinfotech.com/portfolio/

#ios app development agency in the usa #ios app development agency #ios app development #ios app #ios #hire ios app developer