I am trying to add 3D Touch Shortcuts to an application, I have managed to have the shortcuts appear when using 3DTouch on the app icon from the homescreen; however when using the shortcut the application crashes on load and I am unsure why.
I have managed to get the application to load for the bookmarks shortcut but it does not initiate the BookmarksViewController, it just loads the InitialViewController.
The application is embedded within a UITabBarController and a UINavigationController for each Tab. Both View Controllers I am trying to load are in different tabs but the first view in the navigation controller stack.
Does anyone know where I am going wrong ?
info.plist file
App Delegate
enum ShortcutItemType: String {
case Bookmarks
case Favourites
init?(shortcutItem: UIApplicationShortcutItem) {
guard let last = shortcutItem.type.componentsSeparatedByString(".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return NSBundle.mainBundle().bundleIdentifier! + ".\(self.rawValue)"
}
}
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
handleShortcutItem(shortcutItem)
}
return true
}
private func handleShortcutItem(shortcutItem: UIApplicationShortcutItem) {
if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let sb = UIStoryboard(name: "main", bundle: nil)
let favouritesVC = sb.instantiateViewControllerWithIdentifier("FavouritesVC") as! FavouritesTableViewController
let bookmarksVC = sb.instantiateViewControllerWithIdentifier("BookmarksVC") as! BookmarksNotesViewController
switch shortcutItemType {
case .Bookmarks:
rootViewController.presentViewController(bookmarksVC, animated: true, completion: nil)
break
case .Favourites:
rootViewController.presentViewController(favouritesVC, animated: true, completion: nil)
break
}
}
}
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
handleShortcutItem(shortcutItem)
}
}
Storyboard ID
These are the StoryboardID for the View Controllers.
I think you forgot the 's' in com.appName.Bookmark
Or you need to remove the 's' from Bookmarks here:
enum ShortcutItemType: String {
case Bookmarks
case Favourites
Instead, try using a shortcut like this:
if shortcutItem.type == "com.appName.Bookmark"
#available(iOS 9.0, *)
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
if shortcutItem.type == "com.appName.Bookmark" {
}
}
Related
would appreciate any help. We have implemented handling of universal links in our app and I am struggling with the following issues:
Universal Links opens when the app is running in the background (working fine)
When running on the device with iOS13 installed, opening a universal link only works properly if the app is running in the background. If it has been terminated, after tapping the
link the app is getting launched but this method not called
application(continue userActivity:.., restorationHandler:..)
Any ideas? Appreciate!
enter code here
var window: UIWindow?
var tabBarController1: UITabBarController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool
{
presentAppLaunchVC()
return true
}
func presentVC(navController : UINavigationController)
{
if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}
topController.present(navController, animated: false, completion: nil)
}
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if userActivity.activityType == NSUserActivityTypeBrowsingWeb
{
guard let url = userActivity.webpageURL else {
return false
}
if !isValidDeepLink(web_url: url)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else
{
scrapDeepLinkingUrl(url : url)
}
}
return true
}
func isValidDeepLink(web_url :URL) -> Bool
{
guard let components = URLComponents(url : web_url,resolvingAgainstBaseURL : true) else {
return false
}
guard let host = components.host else {
return false
}
switch host {
case "www.domain.com":
return true
default:
return false
}
}
func scrapDeepLinkingUrl(url : URL)
{
}
else
{
presentAppLaunchVC()
}
}
func presentAppLaunchVC()
{
let storyBoard = UIStoryboard(name: storyboard_name, bundle: nil)
let screen = storyBoard.instantiateViewController(withIdentifier: identifier)
if identifier == "dashboardVC" {
tabBarController1 = screen as? UITabBarController
}
self.window?.rootViewController = screen
}
You need to check the URL in didFinishLaunchingWithOptions method as well.
It can be an URL:
launchOptions[UIApplicationLaunchOptionsURLKey]
or it can be an Universal link:
launchOptions[UIApplicationLaunchOptionsUserActivityDictionaryKey]
What I would do is add conditional scene delegate support. That way, you would get the message in scene(_:willConnectTo:). Okay, this is going to be more work, but you need to get in sync with the native scene support in iOS 13 and later, and this seems to be the moment to do so.
I'm trying to implement a 3D touch command where if the user presses "New scan", then the view controller ProcessedImageViewController is called. I have already set up the Info.plist to create the quick option, but I am having trouble actually calling the ProcessedImageViewController when "New scan" is pressed.
Here is my code from the AppDelegate.swift:
var launchedShortcutItem: UIApplicationShortcutItem?
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if let shortcutItem = launchOptions?[UIApplication.LaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem{
launchedShortcutItem = shortcutItem
}
return true
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
completionHandler(handleShortcutItem(item: shortcutItem))
}
func applicationDidBecomeActive(_ application: UIApplication) {
guard let shortcutItem = launchedShortcutItem else { return }
//If there is any shortcutItem,that will be handled upon the app becomes active
_ = handleShortcutItem(item: shortcutItem)
//We make it nil after perfom/handle method call for that shortcutItem action
launchedShortcutItem = nil
}
func handleShortcutItem(item: UIApplicationShortcutItem) -> Bool {
var handled = false
// Verify that the provided shortcutItem's type is one handled by the application.
let mainStoryboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
var reqVC: UIViewController!
reqVC = mainStoryboard.instantiateViewController(withIdentifier: "ProcessedImageViewController") as! ProcessedImageViewController
handled = true
if let homeVC = self.window?.rootViewController as? UINavigationController {
homeVC.pushViewController(reqVC, animated: true)
} else {
return false
}
return handled
}
When I try to click on "New scan" in the Quick Actions menu, I only get taken to the Root View controller (not ProcessedImageViewController).
I am trying to get the 3d touch quick action to work, I have this code below.
I do get a print saying it was pressed but it doesn't go to the view controller I want.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "LiveFeed" {
print("Tony: We got to live view")
guard let navVC = window?.rootViewController as? UINavigationController else { return }
let storyBoard: UIStoryboard = UIStoryboard(name: "LiveFeed", bundle: nil)
if #available(iOS 11.0, *) {
let composeViewController = storyBoard.instantiateViewController(withIdentifier: "LiveFeedMain") as! LiveFeedMain
navVC.pushViewController(composeViewController, animated: false)
} else {
// Fallback on earlier versions
}
}
}
I have this in the appDelegate
3D Touch is working fine when I press it on the home screen, but not showing the navigation and tab bar, How can I present Navigation and Tab bar using 3D Touch? Don't know how to Override point for customization after application launch.
Grazie Mille
class AppDelegate: UIResponder, UIApplicationDelegate {
enum ShortcutIdentifier: String {
case First
case Second
case Third
case Fourth
//Initializers
init?(fullType: String) {
guard let last = fullType.components(separatedBy: ".").last else { return nil }
self.init(rawValue: last)
}
//Properties
var type: String {
return Bundle.main.bundleIdentifier! + ".\(self.rawValue)"
}
}
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 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 }
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var vcc = UIViewController()
switch (shortCutType) {
case ShortcutIdentifier.First.type:
vcc = storyboard.instantiateViewController(withIdentifier: "VC1")
handled = true
break
case ShortcutIdentifier.Second.type:
vcc = storyboard.instantiateViewController(withIdentifier: "VC2")
handled = true
break
case ShortcutIdentifier.Third.type:
vcc = storyboard.instantiateViewController(withIdentifier: "VC3")
handled = true
break
case ShortcutIdentifier.Fourth.type:
vcc = storyboard.instantiateViewController(withIdentifier: "VC4")
handled = true
break
default:
break
}
// Display the selected view controller
self.window?.rootViewController?.present(vcc, animated: true, completion: nil)
return handled
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
let handledShortCutItem = handleShortCutItem(shortcutItem)
completionHandler(handledShortCutItem)
}
func applicationDidBecomeActive(_ application: UIApplication) {
guard launchedShortcutItem != nil else { return }
//handleShortCutItem(shortcut)
launchedShortcutItem = nil
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// If a shortcut was launched, display its information and take the appropriate action
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
launchedShortcutItem = shortcutItem
}
return true
}
How can I present Navigation and Tab bar using 3D Touch
The same way you do it during the normal run of your app. You should not be doing some special thing in response to the user pressing the shortcut item (i.e. your self.window?.rootViewController?.present, which in effect merely puts up a temporary facade); you should be navigating to the actual area of your real app that the shortcut item corresponds to.
I am trying to migrate my code to Swift 3, and came across an error regarding trying to handle 3D Touch shortcuts..
I get the following error
Performing segue using 3D Touch Shortcut - Ambiguous Reference To
Member 'Subscript'
For the following line
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: Any]?) -> Bool {
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsShortcutItemKey] as? UIApplicationShortcutItem {
handleShortcutItem(shortcutItem)
}
}
I am not sure why I am getting this, does anyone else know ?
Here's how I am handling the shortcuts
enum ShortcutItemType: String {
case First
case Second
case Third
init?(shortcutItem: UIApplicationShortcutItem) {
guard let last = shortcutItem.type.components(separatedBy: ".").last else { return nil }
self.init(rawValue: last)
}
var type: String {
return Bundle.main.bundleIdentifier! + ".\(self.rawValue)"
}
}
fileprivate func handleShortcutItem(_ shortcutItem: UIApplicationShortcutItem) {
if let rootViewController = window?.rootViewController, let shortcutItemType = ShortcutItemType(shortcutItem: shortcutItem) {
let rootNavController = rootViewController.childViewControllers.first as! UINavigationController
let viewController = rootNavController.childViewControllers[1]
switch shortcutItemType {
case .First:
viewController.performSegue(withIdentifier: "firstSegue", sender: self)
break
case .Second:
viewController.performSegue(withIdentifier: "secondSegue", sender: self)
break
case .Third:
//Segue to view controller from first then perform another segue to a modal view.
viewController.performSegue(withIdentifier: "thirdSegue", sender: self)
break
}
}
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
handleShortcutItem(shortcutItem)
}
The method header of application(_:didFinishLaunchingWithOptions:) has changed to:
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)
The symbol UIApplicationLaunchOptionsShortcutItemKey has been replaced with UIApplicationLaunchOptionsKey.shortcutItem.
And this may be another issue, but application(_:performActionFor:completionHandler:) needs to have this header:
func application(_ application: UIApplication,
performActionFor shortcutItem: UIApplicationShortcutItem,
completionHandler: #escaping (Bool) -> Void)
Try fixing all of them.