2. Handoff
Also known as Continuity
Handoff is a new capability in iOS 8 and OS X v10.10 that is
designed to facilitate easy transfer of user activities among
multiple devices associated with the same user.
In other words
Handoff provides seamless user activity across device/apps
signed with identical identifier
4. Requirements
Bluetooth LE (v4.0)
Signed into identical iCloud account
Devices need to be in close physical proximity
Internet connectivity (assumption)
iOS 8 and/or OSX 10.10.x+
5. Compatibility
Will work with –
iPhone 5 or later
iPad (4th gen)
iPad Air
iPad mini
iPad mini - Retina display
iPod touch (5th gen).
9. Document based app
For basic need, have to set:
CFBundleDocumentTypes (of type Array) under Document
types key
NSUbiquitousDocumentUserActivityType key with value - string
used for the NSUserActivity object’s activity type - reverse-DNS
app designator with the name of the activity (append if more)
10. Document based app
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key> <!– name of document type -->
<string>NSRTFDPboardType</string> <key>LSItemContentTypes</key> <!–
supported file types -->
<array>
<string>com.myCompany.rtfd</string>
</array>
<key>NSUbiquitousDocumentUserActivityType</key> <!– activity name in
reverse DNS notation --> <string>com.myCompany.myEditor.editing</string>
</dict>
</array>
12. Process Activity (1/2)
Application level handling
AppDelegate (NS/UIApplicationDelegate) – the
entry/resume point
- (BOOL) application:(UIApplication *)application willContinueUserActivityWithType:(NSString
*)userActivityType
{
return YES; // when continued successfully; return NO, otherwise
}
- (void) application:(UIApplication *)application didFailToContinueUserActivityWithType:(NSString
*)userActivityType error:(NSError *)error
{
// If there’s success, there’s failure
}
- (void)application:(UIApplication *)application didUpdateUserActivity:(NSUserActivity *)userActivity
{
// When a user activity managed by UIKit has been updated. Use this as a last chance to add
additional data to the userActivity.
}
13. Process Activity (2/2)
Application level handling
When the application is aware of possible activity (or /ties) – like document base:
- (BOOL)application:(UIApplication *)application continueUserActivity: (NSUserActivity
*)userActivity restorationHandler: (void(^)(NSArray *restorableObjects)) restorationHandler
{
NSString *activityType = userActivity.activityType;
if ([activityType isEqual:@"com.company.viewing-message"])
{
id vc = [[ViewController alloc] init];
- restorationHandler(@[vc]);
return YES;
}
return NO;
}
If above method is unimplemented or returns NO (typically in Document based
app), it is handled by:
- (void)restoreUserActivityState:(NSUserActivity *)activity
14. NSUserActivityDelegate (1/2)
- (void)userActivityWasContinued: (NSUserActivity *) userActivity
{
// The user activity will be saved (to be continued). Receiver should update its
activity object with current activity state (received from other device).
}
- (void)userActivityWillSave: (NSUserActivity *) userActivity
{
// User activity was continued on another device.
}
15. NSUserActivityDelegate (2/2)
- (void)userActivity: (NSUserActivity *)userActivity
didReceiveInputStream:(NSInputStream *)inputStream
outputStream:(NSOutputStream *)outputStream
{
// If supportsContinuationStreams is set to YES the continuing side can
request streams back to this user activity. This delegate callback will be received
with the incoming streams from the other side. The streams will be in an
unopened state. The streams should be opened immediately to start receiving
requests from the continuing side.
}
16. NSStream
NSInputStream provides read-only access to
stream data
NSOutputStream provides write-only access
Data written to the output stream on the originating
side is read from the input stream on the continuing
side, and vice versa
Streams are meant to be used in a request-and-
response fashion; that is
18. Summary (1/2)
NSUserActivity class object is in core of this framework, which holds data
about user activity.
The object instance is passively shared across apps and/or devices to provide
capability of continuing a task (or more) to the user.
The application is required and responsible to handle NSUserActivity object
and delegate methods / callbacks to provide continuous activity.
If payload (data) is heavy, use stream instead of userInfo.
19. Summary (2/2)
NSDocument or UIDocument based apps are said to
be able to handle Handoff automatically
Simplest example is browsing a webpage at
particular scroll position in iPad and then user
wants to switch to iPhone. With handoff it is
possible to show the same webpage with scroll
position on smaller screen/iPhone.
20. Tips from Apple (1/3)
Design to transfer as small a payload as possible; the more payload data
you deliver, the longer it takes the activity to resume.
When a large amount of data transfer is unavoidable, use streams, but
recognize that they have a cost in terms of network setup and overhead.
Plan for different versions of apps on different platforms to work well
with each other or fail gracefully. Remember that the complementary
app design can be asymmetrical—for example, a monolithic Mac app
can route each of its activity types to smaller, special-purpose apps on
iOS.
21. Tips from Apple (2/3)
Use reverse-DNS notation for your activity types to
avoid collisions. If the activity pertains only to a
single app, you can use the app identifier with an
extra field appended to describe the activity type.
For example, use a format such as
com.<company>.<app>.<activity type>, as in
com.myCompany.myEditor.editing. If you have a
user activity that works across more than one app,
you can drop the app field, as in
com.myCompany.editing.
22. Tips from Apple (3/3)
To update the activity object’s userInfo dictionary
efficiently, configure its delegate and set its needsSave
property to YES whenever the userInfo needs updating.
At appropriate times, Handoff invokes the delegate’s
userActivityWillSave: callback, and the delegate can
update the activity state.
Be sure the delegate of the continuing app implements
its application:willContinueUserActivityWithType: to let
the user know the activity will be continued. The user
activity object may not be available instantly.
23. Thank you!
Further scope of study:
Browser to Native App Handoff and vice versa
Document based apps