From 444e0597686fdeb513e0c1f328ca3a92019db9ca Mon Sep 17 00:00:00 2001 From: Matthew Michael Date: Sun, 27 Mar 2016 11:45:36 -0600 Subject: [PATCH] Merge handoff from Graeme Harrison Thanks to GH for adding handoff functionality --- SwiftRadio/Info.plist | 4 ++++ SwiftRadio/NowPlayingViewController.swift | 26 +++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/SwiftRadio/Info.plist b/SwiftRadio/Info.plist index 13bcea89..286279f1 100755 --- a/SwiftRadio/Info.plist +++ b/SwiftRadio/Info.plist @@ -29,6 +29,10 @@ NSAllowsArbitraryLoads + NSUserActivityTypes + + NSUserActivityTypeBrowsingWeb + UIBackgroundModes audio diff --git a/SwiftRadio/NowPlayingViewController.swift b/SwiftRadio/NowPlayingViewController.swift index 322f2367..ca2486e9 100755 --- a/SwiftRadio/NowPlayingViewController.swift +++ b/SwiftRadio/NowPlayingViewController.swift @@ -55,6 +55,9 @@ class NowPlayingViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() + // Setup handoff functionality - GH + setupUserActivity() + // Set AlbumArtwork Constraints optimizeForDeviceSize() @@ -534,6 +537,7 @@ class NowPlayingViewController: UIViewController { // Update Labels self.artistLabel.text = self.track.artist self.songLabel.text = self.track.title + self.updateUserActivityState(self.userActivity!) // songLabel animation self.songLabel.animation = "zoomIn" @@ -553,4 +557,26 @@ class NowPlayingViewController: UIViewController { } } } + + //***************************************************************** + // MARK: - Handoff Functionality - GH + //***************************************************************** + + func setupUserActivity() { + let activity = NSUserActivity(activityType: NSUserActivityTypeBrowsingWeb ) //"com.graemeharrison.handoff.googlesearch" //NSUserActivityTypeBrowsingWeb + userActivity = activity + let url = "https://www.google.com/search?q=\(self.artistLabel.text!)+\(self.songLabel.text!)" + let urlStr = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) + let searchURL : NSURL = NSURL(string: urlStr!)! + activity.webpageURL = searchURL + userActivity?.becomeCurrent() + } + + override func updateUserActivityState(activity: NSUserActivity) { + let url = "https://www.google.com/search?q=\(self.artistLabel.text!)+\(self.songLabel.text!)" + let urlStr = url.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()) + let searchURL : NSURL = NSURL(string: urlStr!)! + activity.webpageURL = searchURL + super.updateUserActivityState(activity) + } }