Application Shortcut
Application shortcut is the way where we can perform some task fast without open the main App & go to particular section . App Shortcut will directly to specified location , but there is some limitation to use this Shortcut it will support to iPhone 6s to above phone .
There is Some Steps to Follow.
1: Open Your App Delegate File .
a: Here I have taken one shortcut that will go to your Cart Controller in App.
declare their unique key : it is unique key to define the App Shortcut that will help to update this name or other value later .
1 2 3 |
enum AppShortCutKey:String{ case cartKey = "cartselection" } |
b: Go to this Function in App Delegate file and write this:
1 2 3 4 |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.setupAppShortCut(application, launchOptions: launchOptions) } |
c: Now we have to define this method:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
func setupAppShortCut(_ application: UIApplication,launchOptions: [UIApplicationLaunchOptionsKey: Any]?){ // If a shortcut was launched, display its information and take the appropriate action. if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem { launchedShortcutItem = shortcutItem } if let shortcutItems = application.shortcutItems, shortcutItems.isEmpty { let icon1 = UIApplicationShortcutIcon(templateImageName:"ic_cart") let item1 = UIApplicationShortcutItem(type: AppShortCutKey.cartKey.rawValue, localizedTitle: "cart".localized, localizedSubtitle: "startshopping".localized, icon: icon1, userInfo: nil) UIApplication.shared.shortcutItems = [item2] } } |
Note: Here we are checking the App Shortcut is Created before or not so if is empty than it will create other wise return the previous one.
d: Declare Shortcut Item in Appdelegate.swift class:
1 2 3 4 5 |
class AppDelegate: UIResponder, UIApplicationDelegate { var launchedShortcutItem: UIApplicationShortcutItem? } |
2: Handle the Click Event Of App Shortcut.
a: Override this method in App Delegate file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
extension AppDelegate{ func handleShortCutItem(_ shortcutItem: UIApplicationShortcutItem) -> Bool { var handled = false guard let shortCutType = shortcutItem.type as String? else { return false } switch shortCutType { case AppShortCutKey.searchKey.rawValue: handled = true NotificationCenter.default.post(name: NSNotification.Name(rawValue: "shortcutsearchtap"), object: nil, userInfo: nil) break default: break } return handled } func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: @escaping (Bool) -> Void) { let handledShortCutItem = handleShortCutItem(shortcutItem) completionHandler(handledShortCutItem) } } |
Note: If you click on Any Shortcut item , this method will automatically call so you need to return true for successfully execution.
Here i have taken Local Notification to handle the request.
You have to define this “shortcutsearchtap” in your start viewcontroller in ViewDidload Method.
In Our Case i have start viewcontroller is ViewController.swift class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
class ViewController: UIViewController,UISearchBarDelegate,CategoryViewControllerHandlerDelegate,BrandViewControllerHandlerDelegate,productViewControllerHandlerDelegate,bannerViewControllerHandlerDelegate,UITabBarControllerDelegate, RecentProductViewControllerHandlerDelegate{ override func viewDidLoad() { super.viewDidLoad() NotificationCenter.default.addObserver(self, selector: #selector(self.goToCart), name: NSNotification.Name(rawValue: "shortcutcarttap"), object: nil) } func goToCart(_ note: Notification) { self.tabBarController?.selectedIndex = 3 // handle this request } } |
That’s All
For any queries, feel free to ask.