I have a 3D Force Touch Action that I set up in my Info.plist. Now I want that when the 3D Action is running it presents a view controller that I gave a Storyboard ID and I need to do this in the AppDelegate.
I used the performActionForShortCutItem function.
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if let tabVC = self.window?.rootViewController as? UITabBarController {
if shortcutItem.type == "Hausaufgaben" {
tabVC.selectedIndex = 1
tabVC.performSegue(withIdentifier: "gotoHausaufgaben", sender: nil)
}
}
}
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "Hausaufgaben" {
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "YOUR PAGE Identifier")
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
Related
I'm trying deeplink to open particular page in app on click of shared link from other app/from safari, URL opens the app but unable to take application on particular page i,e, unable to read link (custom URL). This is my custom URL :- WOT://tradeDetail
If anyone knows where i'm going wrong, please help
here is screenshot and code
var window: UIWindow?
var scheme = "WOT"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
ApplicationDelegate.shared.application(application, didFinishLaunchingWithOptions: launchOptions)
GMSPlacesClient.provideAPIKey(WOT.googlePlaceAPIKey)
if let url = launchOptions?[.url] as? URL {
return handle(url: url)
}
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
return handle(url: url)
}
func handle(url: URL) -> Bool {
switch url.absoluteString {
case "\(scheme)://tradeDetail" : do {
let sb = UIStoryboard(name: "Main", bundle: .main)
let detailView = sb.instantiateViewController(withIdentifier: "SearchedPlaceDetailVC") as? SearchedPlaceDetailVC
window?.rootViewController = detailView
window?.makeKeyAndVisible()
}
default: return false
}
return true
}
You need to call below function to navigate particular page
func application(_ application: UIApplication, continue userActivity:
NSUserActivity, restorationHandler: #escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
let myUrl: String? = userActivity.webpageURL?.absoluteString
if myUrl?.range(of: "tradeDetail") != nil {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let yourViewController = storyboard.instantiateViewController(withIdentifier: “YourViewController”) as? YourViewController
self.window?.rootViewController = yourViewController
self.window?.makeKeyAndVisible()
}
return true
}
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
I am trying to add 3D touch quick action in my app. I have two viewContollers embedded in navigationContoller. When the user press on the 3D action, it should take them to the add events viewController. But i am getting this error
could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360).
2017-05-02 02:51:36.220324-0400 Morning Star 2[3731:895126] Could not cast value of type 'Morning_Star_2.AddEventsViewController' (0x1000a2260) to 'UINavigationController' (0x1a79cd360).
Here is my code
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.krp.addEvent" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! UINavigationController
self.window?.rootViewController?.present(addEventVC, animated: true, completion: nil)
}
}
I tried changing as! UINavigationController to as! AddEventsViewController. It works, but then it won't show the navigation bar on the top on my add viewController as it normally would if you open the app normally.
Here is my main.storyboard
You shouldn't cast AddEventsViewController to UINavigationController, because it isn't UINavigationController, is just subclass of UIViewController. But as I see, your rootViewController is. So you need to cast it to UINavigationController, and then push your AddEventsViewController, instead present, and you will see NavigationBar
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.krp.addEvent" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let addEventVC = sb.instantiateViewController(withIdentifier: "addEventVC") as! AddEventsViewController
if let navigationController = window?.rootViewController as? UINavigationController {
navigationController.pushViewController(addEventVC, animated: true)
}
}
}
I got a 3d touch function in my app delegate for the shortcutitems and it has to instantiate a viewcontroller when 3d touch item clicked from the iphone.
Here is the code:
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
let root = UIApplication.shared.keyWindow?.rootViewController
root?.present(exerciseVC, animated: false, completion: { () -> Void in
completionHandler(true)
})
}
}
This code results in this warning/error:
Warning: Attempt to present <ActiveRest.exerciseVC: 0x131d39320> on <ActiveRest.ViewController: 0x131d0e470> whose view is not in the window hierarchy!
Has this anything to do with delay?
Thanks for any help!
Kind Regards.
view is not in the window hierarchy that means your view of your viewcontroller is not loaded in memory still. so you are unable to present viewcontroller on it. you should have to present viewcontroller from viewDidAppear of your viewcontroller (I mean from your rootviewController).
func application(_ application: UIApplication, performActionFor shortcutItem: UIApplicationShortcutItem, completionHandler: #escaping (Bool) -> Void) {
if shortcutItem.type == "com.kevinvugts.addStuf" {
let sb = UIStoryboard(name: "Main", bundle: nil)
let exerciseVC = sb.instantiateViewController(withIdentifier: "exerciseVC") as! exerciseVC
window?.rootViewController?.addChildViewController(exerciseVC)
}
}
I made this change and everything works fine.
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" {
}
}