Handoff is a powerful new feature in iOS 8 and OS X 10.10 that allows a user to begin an activity on one device and continue it on another device signed into the same iCloud account.
SPLUserActivity
is a collection of objects that make it easy to adopt Handoff for different types of activities.
SPLUserActivity
powers Handoff for the web browser in my app MUDRammer - A Modern MUD Client for iPhone and iPad.
SPLWebActivity
powers a Handoff activity for web browsing. It allows a user to continue browsing from the same web page on another device.
Check out Example
for a simple app that displays a WKWebView
and broadcasts a Handoff activity as the user browses between web pages. Note that Handoff won't function in the simulator - you'll need to run it on a device.
If your app uses a WKWebView
, initialize a SPLWebActivity
by passing your webview:
WKWebView *myWebView = ...
SPLWebActivity *webActivity = [SPLWebActivity activityWithWKWebView:myWebView];
That's it -- you're done! SPLWebActivity
will start broadcasting a Handoff event right away. Thanks to the magic of KVO, SPLWebActivity
will observe your WKWebView
and will automatically update its activity URL and page title as the user navigates between web pages.
If your app uses a UIWebView
, initialize a SPLWebActivity
by passing your webview:
UIWebView *myWebView = ...
self.webActivity = [SPLWebActivity activityWithUIWebView:myWebView];
As with WKWebView
, SPLWebActivity
will begin broadcasting a Handoff event right away. However, UIWebView
does not conform to KVO, so you'll need to tell SPLWebActivity
to update itself as the user navigates between web pages:
#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webActivity setNeedsUpdate];
}
After receiving a setNeedsUpdate
message, SPLWebActivity
will query your UIWebView
for the user's current URL and update its Handoff activity.
SPLWebActivity
also allows your user to browse any URL on another device -- no webview required.
SPLWebActivity *myActivity = [SPLWebActivity activityWithURL:[NSURL URLWithString:@"http://splinesoft.net"]];
You may optionally specify a block that will be called when the user continues an activity on another device.
myActivity.activityContinuedBlock = ^{
NSLog(@"I've just continued an activity!");
};
When your user activity is no longer relevant -- perhaps when your webview is popped off the navigation stack, or when the activity is no longer available -- make sure to invalidate the activity.
[myActivity invalidate];
An invalidated activity can no longer becomeCurrent
and cannot be reused. If you'd like to broadcast a Handoff event again, create a new instance of SPLWebActivity
.
Install with CocoaPods. Add to your Podfile
:
pod 'SPLUserActivity'
SPLUserActivity
is a @jhersh production -- (electronic mail | @jhersh)