Getting familiar with today extension

This post was written by our iOS Developer, Hakkı Yiğit Yener.

Of all the new features introduced in iOS 8, Today Extension is the one that is more distinctive than all the other features offered in the previous versions of the operating system. Apple gets its hands on the feature that Android and Windows Phone users are already familiar by means of ‘Widget’ and ‘Live Tile’. Even though the extensions have its own executable files, the main software inholds them in its structure. Extensions require a host application in order to perform, so that they are not publishable or downloadable on the App Store. You can consider Today Extension as a mini application that runs in notification center constantly. Some limitations such as issues related to system resources stand out due to performance and security matters. For instance, the system immediately attempts to terminate extension unless the data keep flowing each second, otherwise any one-second obstacle in data transaction causes termination. In addition to that, the system breaks the extensions off in case of more than 30 MB memory usage (depending on the type and approximate values given in extension.), or high CPU usage. Also, the extensions are the initial features that will be terminated in case of urgent need of memory or CPU by the operating system. Due to these reasons mentioned before, extensions should be designed rather simple. Any need of GPU by the extensions is not recommended to developers, in order to provide proper experience to user because GPU is a shared resource by the system. Apart from these, developers should avoid putting scroll view inside a widget to ensure that users do not experience any difficulty in the widget without scrolling the extension view. Vertical and horizontal gestures have their own obstacles. First, horizontal scroll would be crossed by the notification center to switch between the extension and notification view. Secondly, vertical scrolling is also problematic because making the view tall enough to require scrolling is not very possible because of the height limitation. The maximum height for a today extension view is the screen size minus the notification center’s own UI components. As I have mentioned before, extensions are packed with a host application, but systematically they are separate applications. Since, they are separate direct communication between these two is not viable. In this case, we can make use of ‘NSUserDefaults’ as the most basic method to transfer data between the host application and the extension. Just to give you a heads up about the NSUserDefaults I’d mention that the host application and the extension must be in the same application group.

Here is a simple way to use user default for data sharing.

To store data: NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@”com.example.domain.MyShareExtension”];[mySharedDefaults setObject:theAccountData forKey:@“AccountData”];

To read data: NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@”com.example.domain.MyShareExtension”];NSData *accountData = [mySharedDefaults objectForKey:@”AccountData”]; There is one last thing I would like to mention before I conclude my post about how to gather the host application and the extension up in the same application group. First, you need to go to ‘App Capabilities’ of the host application. Then, enable ‘Group’ feature and press ‘+’ button to enter the group name. Usually, group names begin with ‘group’ prefix. After that, the same steps should be repeated one more time for the attachment. As the last step don’t forget to choose the new group we created earlier. Now, we have just finalized gathering them up in the same application group and we are ready to share data between two of them via ‘NSUserDefaults’. This post covers the framework of the new feature by its basic means of access, package, and process. This feature is considered as one of the most significant changes made since iOS 4, so as an enterprising developer, you might want to take a look at Apple’s Today Extension.

Sources