Understanding Today Extensions

A Today Extension isn't much more than a view controller that is embedded inside the notification center's Today View. This means that when your Today Extension is initialized, there is no AppDelegate involved at all. The extension does not have any application life cycle methods, such as application(_:didFinishLaunchingWithOptions:).

The Today View displays your widget by showing the view controller that you set up for it. This is very similar to how a Notification Content Extension works since that extension also has a single view controller to define its functionality.

The Today View is in control of positioning and rendering all of its widgets. This means that you can't set the frame for the widget's view yourself. When you implemented your Notification Content Extension in the previous chapter, you could force your notification to be shown at a certain height through AutoLayout. This isn't possible when you implement a Today Extension.

Before iOS 10, a widget would resize itself based on its contents or the size that the widget would set as its preferredContentSize. Since iOS 10, this behavior has changed, and widgets have a user-defined compact mode and an expanded mode. The compact mode should typically be about 110 points in height, but in reality, this height varies based on the user's text-size preferences.

Because there are no real guarantees regarding the height of your widget can use, it's essential that you only show the most critical information in your widget. Because widgets tend to resize based on the user's text size preferences, it's a good idea to adopt dynamic type in your widget. This makes your widget more accessible by scaling the text inside your widget based on the user's preferences. You'll learn more about this when you implement the widget.

If your widget supports it, the user can switch to the expanded state for your widget. In the expanded state, you have a bit more freedom to set the size of the widget yourself. You will also explore this more when you implement your own widget.

One more key principle of the Today Extension is that the widget never communicates with its host app directly. If you have a widget that the user can tap on to open the app, the widget doesn't directly instruct the app to do this. Instead, URLs are used. You can use a custom URL scheme or a Universal Link to achieve this. In several snippets of example code, Apple uses custom URL schemes to redirect users from a widget to an app. The Daily Quote app you will extend in this chapter will work similarly.

Now that you're up to speed with Today Extensions, it's time to see how you can add your own Today Extension to The Daily Quote.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.119.17