Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Today Extensions Tutorial

Introduction to Today Extensions

Today Extensions, also known as widgets, are a type of app extension that allow you to display some quick, useful information in the Today view of the Notification Center. These can be very useful for providing users with quick access to app functionalities and updates without needing to open the app itself.

Setting Up Your Project

To get started with creating a Today Extension, follow these steps:

  1. Open your existing Xcode project or create a new one.
  2. Go to File > New > Target.
  3. In the template chooser, select Today Extension under iOS > Application Extension.
  4. Click Next and provide a name for your extension.
  5. Select your development team and ensure the bundle identifier is correct.
  6. Click Finish to add the target to your project.

Understanding the Files

After adding the Today Extension target, you will see some new files in your project:

  • TodayViewController.swift: This is where you will write the code for your widget's functionality.
  • MainInterface.storyboard: This is where you will design the layout of your widget.
  • Info.plist: This plist file contains the configuration for your extension.

Designing the Widget Interface

Open MainInterface.storyboard and design your widget's interface using Interface Builder. You can add labels, buttons, images, etc., just like you would in a regular iOS app. Make sure to set the appropriate constraints to ensure your layout works well in different sizes.

Implementing the Widget Logic

In TodayViewController.swift, you will implement the logic for your widget. Here's a simple example that displays a greeting message:

import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {

    @IBOutlet weak var greetingLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        updateGreeting()
    }

    func updateGreeting() {
        let hour = Calendar.current.component(.hour, from: Date())
        if hour < 12 {
            greetingLabel.text = "Good Morning!"
        } else if hour < 18 {
            greetingLabel.text = "Good Afternoon!"
        } else {
            greetingLabel.text = "Good Evening!"
        }
    }

    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        updateGreeting()
        completionHandler(NCUpdateResult.newData)
    }
}
                

Running Your Today Extension

To run your Today Extension, select the Today Extension scheme from the scheme menu in Xcode and click the Run button. Your widget should appear in the Today view of the Notification Center. If you don't see it, make sure to add it by scrolling to the bottom of the Today view, tapping Edit, and adding your widget.

Advanced Features

Today Extensions can do much more than just displaying static information. Here are some advanced features you can implement:

  • Interactive Widgets: Add buttons and other interactive elements to your widget.
  • Fetching Data: Fetch data from a network or a local database to display dynamic content.
  • Customizing Size: Adjust the preferred content size of your widget for a more flexible layout.

Conclusion

Today Extensions are a powerful way to provide quick access to your app's features and information. With the ability to create interactive and dynamic widgets, you can greatly enhance the user experience. We hope this tutorial has given you a good starting point for creating your own Today Extension.