Created
October 26, 2015 21:19
-
-
Save xhacker/75695c7d9d87539a9d9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright (C) 2015 Apple Inc. All Rights Reserved. | |
See LICENSE.txt for this sample’s licensing information | |
Abstract: | |
The application delegate class used to manage this sample. | |
*/ | |
import UIKit | |
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
// MARK: Types | |
enum ShortcutIdentifier: String { | |
case First | |
case Second | |
case Third | |
case Fourth | |
// MARK: Initializers | |
init?(fullType: String) { | |
guard let last = fullType.componentsSeparatedByString(".").last else { return nil } | |
self.init(rawValue: last) | |
} | |
// MARK: Properties | |
var type: String { | |
return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)" | |
} | |
} | |
// MARK: Static Properties | |
static let applicationShortcutUserInfoIconKey = "applicationShortcutUserInfoIconKey" | |
// MARK: Properties | |
var window: UIWindow? | |
/// Saved shortcut item used as a result of an app launch, used later when app is activated. | |
var launchedShortcutItem: UIApplicationShortcutItem? | |
func handleShortCutItem(shortcutItem: UIApplicationShortcutItem) -> Bool { | |
var handled = false | |
// Verify that the provided `shortcutItem`'s `type` is one handled by the application. | |
guard ShortcutIdentifier(fullType: shortcutItem.type) != nil else { return false } | |
guard let shortCutType = shortcutItem.type as String? else { return false } | |
switch (shortCutType) { | |
case ShortcutIdentifier.First.type: | |
// Handle shortcut 1 (static). | |
handled = true | |
break | |
case ShortcutIdentifier.Second.type: | |
// Handle shortcut 2 (static). | |
handled = true | |
break | |
case ShortcutIdentifier.Third.type: | |
// Handle shortcut 3 (dynamic). | |
handled = true | |
break | |
case ShortcutIdentifier.Fourth.type: | |
// Handle shortcut 4 (dynamic). | |
handled = true | |
break | |
default: | |
break | |
} | |
// Construct an alert using the details of the shortcut used to open the application. | |
let alertController = UIAlertController(title: "Shortcut Handled", message: "\"\(shortcutItem.localizedTitle)\"", preferredStyle: .Alert) | |
let okAction = UIAlertAction(title: "OK", style: .Default, handler: nil) | |
alertController.addAction(okAction) | |
// Display an alert indicating the shortcut selected from the home screen. | |
window!.rootViewController?.presentViewController(alertController, animated: true, completion: nil) | |
return handled | |
} | |
// MARK: Application Life Cycle | |
func applicationDidBecomeActive(application: UIApplication) { | |
guard let shortcut = launchedShortcutItem else { return } | |
handleShortCutItem(shortcut) | |
launchedShortcutItem = nil | |
} | |
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { | |
// Override point for customization after application launch. | |
var shouldPerformAdditionalDelegateHandling = true | |
// If a shortcut was launched, display its information and take the appropriate action | |
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem { | |
launchedShortcutItem = shortcutItem | |
// This will block "performActionForShortcutItem:completionHandler" from being called. | |
shouldPerformAdditionalDelegateHandling = false | |
} | |
// Install initial versions of our two extra dynamic shortcuts. | |
if let shortcutItems = application.shortcutItems where shortcutItems.isEmpty { | |
// Construct the items. | |
let shortcut3 = UIMutableApplicationShortcutItem(type: ShortcutIdentifier.Third.type, localizedTitle: "Play", localizedSubtitle: "Will Play an item", icon: UIApplicationShortcutIcon(type: .Play), userInfo: [ | |
AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Play.rawValue | |
] | |
) | |
let shortcut4 = UIMutableApplicationShortcutItem(type: ShortcutIdentifier.Fourth.type, localizedTitle: "Pause", localizedSubtitle: "Will Pause an item", icon: UIApplicationShortcutIcon(type: .Pause), userInfo: [ | |
AppDelegate.applicationShortcutUserInfoIconKey: UIApplicationShortcutIconType.Pause.rawValue | |
] | |
) | |
// Update the application providing the initial 'dynamic' shortcut items. | |
application.shortcutItems = [shortcut3, shortcut4] | |
} | |
return shouldPerformAdditionalDelegateHandling | |
} | |
/* | |
Called when the user activates your application by selecting a shortcut on the home screen, except when | |
application(_:,willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions) returns `false`. | |
You should handle the shortcut in those callbacks and return `false` if possible. In that case, this | |
callback is used if your application is already launched in the background. | |
*/ | |
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: Bool -> Void) { | |
let handledShortCutItem = handleShortCutItem(shortcutItem) | |
completionHandler(handledShortCutItem) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment