WWDC 2014 Session 205: Creating Extensions for iOS and OS X, Part 1
- Extensions have a separate code signature, entitlements, and container from their parent app.
- They are not a form of app to app IPC and are accessed solely by Apple frameworks (not even Apple’s apps access them directly)
Notification Center Widgets
- Widgets are view controllers — all of the same lifecycle and containment concepts apply.
- Widget should be ready to display after
-viewWillAppear:
.
- Widget should be ready to display after
- Notification Center sets the view frame.
- Height can be set using Autolayout constraints or by setting
UIViewController
’spreferredContentSize
. - Content should animate alongside the height change.
- On iOS, implement
-viewWillTransitionToSize:withTransitionCoordinator:
and call-animateAlongsideTransition:completion:
from theUIViewControllerTransitionCoordinator
protocol. - On OS X, implement
-viewWillTransitionToSize:
.
- On iOS, implement
- Height can be set using Autolayout constraints or by setting
- Implement
-widgetPerformUpdateWithCompletionHandler:
from theNCWidgetProviding
protocol to be notified when to update the widget. - New “Today Extension” Xcode template on both iOS and OS X for adding a widget target.
- Use
UIViewController
’sextensionContext
(seeNSExtensionContext
) to call back to the main app from the widget.
Share Extensions
- Set
CFBundleName
in the extension bundle’s Info.plist to set the display name. - Set activation rules (ie. the rules that determine when your share extension shows up in the activity picker) by setting a predicate string for the
NSExtension
->NSExtensionAttributes
->NSExtensionActivationRule
key in Info.plist.- Simpler rules can be expressed using one of the condensed rules instead of a predicate.
- Extensions with custom UI can inherit from
UIViewController
orNSViewController
. - Extensions that want the standard share sheet UI can inherit from
SLComposeServiceViewController
.- Override
-presentationAnimationDidFinish
to start heavy lifting after presentation is complete. - Override
-isContentValid
to set the enabled status of the Post button.- Call
-validateContent
to manually trigger an update of the Post button status.
- Call
- Set
charactersRemaining
property to update UI to reflect how many characters are left. - Override
-didSelectPost
to implement upload logic when user taps Post button. - Override
-configurationItems
to provide an array ofSLComposeSheetConfigurationItem
objects for displaying custom service-specific settings.
- Override
- Get data to upload using
NSExtensionContext
’sinputItems
property. - Must use
NSURLSession
with a background session configuration (see+[NSURLSessionConfiguration backgroundSessionConfiguration:]
) for uploading data. - Call
-[NSExtensionContext completeRequestReturningItems:completionHandler:]
after starting the upload to close the share sheet, or-[NSExtensionContext cancelRequestWithError:]
if the user cancelled.
Complete documentation can be found in the App Extension Programming Guide.