Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to App Extensions

What are App Extensions?

App Extensions allow you to extend custom functionalities beyond your app and make them available to users while they're interacting with other apps or the system. They are a powerful way to make your app's capabilities accessible in new contexts, such as sharing content, providing custom widgets, or offering services in other apps.

Types of App Extensions

There are various types of app extensions, each serving a specific purpose:

  • Today Extension: Also known as widgets, they provide quick updates or brief information in the Today view of the Notification Center.
  • Share Extension: Allows users to share content from other apps to your app.
  • Action Extension: Let users perform actions on content from other apps.
  • Photo Editing Extension: Enables photo editing within the Photos app.
  • Document Provider Extension: Provides access to documents managed by your app.
  • Custom Keyboard Extension: Allows you to create a custom keyboard for system-wide use.

Creating a Simple Today Extension

Let's walk through creating a simple Today Extension that displays a message.

Step 1: Create a New Project

Open Xcode and create a new project. Choose the "Single View App" template.

Example:

File > New > Project > Single View App

Step 2: Add a Today Extension Target

Next, add a new target to your project for the Today Extension.

Example:

File > New > Target > Today Extension

Step 3: Configure the Today Extension

Once the target is added, configure the Today Extension by modifying the TodayViewController.swift file.

Example:

TodayViewController.swift
import UIKit
import NotificationCenter

class TodayViewController: UIViewController, NCWidgetProviding {

    @IBOutlet weak var messageLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        messageLabel.text = "Hello, World!"
    }

    func widgetPerformUpdate(completionHandler: (@escaping (NCUpdateResult) -> Void)) {
        // Perform any setup necessary to update the view.
        completionHandler(NCUpdateResult.newData)
    }
}

Step 4: Run the Extension

Select the Today Extension scheme and run it. You should see your message in the Today view.

Example:

Select the Today Extension target > Run

Conclusion

App Extensions are a powerful way to enhance your app's functionality and provide users with more ways to interact with your app. By following the steps above, you can get started with creating your own Today Extension and explore other types of extensions to further enrich the user experience.