I got stuck specific view controller is not move when I tap on push notification alert when application is not open stage totally.
Here is my code:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: #escaping (UIBackgroundFetchResult) -> Void) {
/*
fetch and add push notification data
*/
goAnotherVC()
}
func goAnotherVC() {
if (application.applicationState == UIApplicationState.active) {
/* active stage is working */
} else if (application.applicationState == UIApplicationState.inactive || application.applicationState == UIApplicationState.background) {
if (type == "1" || type == "2") {
let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
let navigationController = UINavigationController.init(rootViewController: apptVC)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
} else if (type == "3") {
let storyboard: UIStoryboard = UIStoryboard(name: "MyAppointments", bundle: nil)
let apptVC = storyboard.instantiateViewController(withIdentifier: "NotificationDetailViewController") as! NotificationDetailViewController
let navigationController = UINavigationController.init(rootViewController: apptVC)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
} else if (type == "4") {
let storyboard: UIStoryboard = UIStoryboard(name: "Enquiry", bundle: nil)
let enqVC = storyboard.instantiateViewController(withIdentifier: "EnquiryDetailViewController") as! EnquiryDetailViewController
let navigationController = UINavigationController.init(rootViewController: enqVC)
self.window?.rootViewController = navigationController
self.window?.makeKeyAndVisible()
}
}
}
I can get notification and tap to move specific VC when application is active. Please help me what I am missing.
Swift 5
Simply, implement the following function which will be called when the user clicked on the notification.
In AppDelegate:
// This method is called when user clicked on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
{
// Do whatever you want when the user tapped on a notification
// If you are waiting for specific data from the notification
// (e.g., key: "target" and associated with "value"),
// you can capture it as follows then do the navigation:
// You may print `userInfo` dictionary, to see all data received with the notification.
let userInfo = response.notification.request.content.userInfo
if let targetValue = userInfo["target"] as? String, targetValue == "value"
{
coordinateToSomeVC()
}
completionHandler()
}
private func coordinateToSomeVC()
{
guard let window = UIApplication.shared.keyWindow else { return }
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let yourVC = storyboard.instantiateViewController(identifier: "yourVCIdentifier")
let navController = UINavigationController(rootViewController: yourVC)
navController.modalPresentationStyle = .fullScreen
// you can assign your vc directly or push it in navigation stack as follows:
window.rootViewController = navController
window.makeKeyAndVisible()
}
Note:
If you navigate to a specific controller based on the notification, you should care about how you will navigate back from this controller because there are no controllers in your stack right now. You must instantiate the controller you will back to. In my case, when the user clicked back, I instantiate the home controller and make it the app root again as the app will normally start.
When you app is in closed state you should check for launch option in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { }
and call your API.
Example:
if let option = launchOptions {
let info = option[UIApplicationLaunchOptionsKey.remoteNotification]
if (info != nil) {
goAnotherVC()
}
}
Swift 5, iOS 13 -
Since iOS 13 "window" is available in SceneDelegate. But the didReceiveNotification method is still present in AppDelegate.
So you have to first access the window from SceneDelegate
let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window
Now You can set the rootViewController property of the window
window.rootViewController = viewControllerObject
window.makeKeyAndVisible()
Related
I looked on SO but I wasn't able to find any question that discusses when you receive a push notification how can you then open a specific view controller.
For example if you are creating an app like WhatsApp and you receive two different push notifications ie messages from two different users how would you direct from the app delegate to the respective viewController?
As far as I know in the userinfo dictionary that the appDelegate gives you you can give an ID to a specific viewController but I don't know how to give any a tribute to a specific view controller so that then you could again direct to that viewController.
Kindly include a code snippet with your answer
**** Swift or Objective-C answers are both acceptable ****
You can detect if the app opened from the notification with this code in app delegate. You will need to set the initial view controller when the application state is UIApplicationStateInactive before the app has become active. You can perform any logic there to decide which view controller should be opened and what content should be shown in that view controller.
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
if(application.applicationState == UIApplicationStateActive) {
//app is currently active, can update badges count here
} else if(application.applicationState == UIApplicationStateBackground){
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
} else if(application.applicationState == UIApplicationStateInactive){
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
}
Here is the Swift 3 Version with switch/case instead of if/else
open func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
switch application.applicationState {
case .active:
print("do stuff in case App is active")
case .background:
print("do stuff in case App is in background")
case .inactive:
print("do stuff in case App is inactive")
}
}
//This method is called when user tap on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
print("user clicked on the notification")
let userInfo = response.notification.request.content.userInfo
print(userInfo)
//check your response and navigate to specific view controller
moveToNextViewController()
}
func moveToNextViewController() {
//Add code for present or push view controller
let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier:"ViewController") as! ViewController
self.navigationController?.pushViewController(vc, animated: true)
}
This method is called when user tap on the notification. implement in appdelegate
var navigationC: UINavigationController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if USERDEFAULT.value(forKey: "isLogin") != nil {
let tabbarvc = MainStoryboard.instantiateViewController(withIdentifier: "TabBarVC") as! TabBarVC
self.navigationC = UINavigationController(rootViewController: tabbarvc)
}else {
let loginvc = MainStoryboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
self.navigationC = UINavigationController(rootViewController: loginvc)
}
self.navigationC?.setNavigationBarHidden(true, animated: false)
self.window?.clipsToBounds = true
self.window?.rootViewController = navigationC
self.window?.makeKeyAndVisible()
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print("-------------\nUser info in notification -> \(userInfo)\n-------------")
let dict = userInfo["aps"] as? NSDictionary ?? [:]
if USERDEFAULT.value(forKey:"isLogin") != nil{
let type = dict["type"] as? Int ?? 0
switch type {
case 0:
break
case 1:
for vc in self.navigationC!.viewControllers {
if vc is TabBarVc {
let exam = dict["exam"] as? String ?? ""
if exam == ""{
let TestVC = MainStoryboard.instantiateViewController(withIdentifier: "TestVC") as! TestVC
TestVC.chapterId = dict["chapter_id"] as? String ?? ""
TestVC.strSubTitle = dict["chapter"] as? String ?? ""
self.navigationC?.isNavigationBarHidden = true
self.navigationC?.pushViewController(TestVC, animated: true)
}else if exam != ""{
let ExamTestVC = MainStoryboard.instantiateViewController(withIdentifier: "ExamTestVC") as! ExamTestVC
ExamTestVC.examId = dict["exam_id"] as? String ?? ""
ExamTestVC.strSubTitle = dict["exam"] as? String ?? ""
self.navigationC?.isNavigationBarHidden = true
self.navigationC?.pushViewController(ExamTestVC, animated: true)
}
return;
}
}
break
case 2:
for vc in self.navigationC!.viewControllers {
if vc is TabBarVc {
let VideoListVC = MainStoryboard.instantiateViewController(withIdentifier: "VideoListVC") as! VideoListVC
VideoListVC.chapterId = dict["chapter_id"] as? String ?? ""
VideoListVC.video_id = dict["video_id"] as? String ?? ""
VideoListVC.strSubTitle = dict["chapter"] as? String ?? ""
VideoListVC.questionsCount = dict["question_count"] as? Int ?? 0
VideoListVC.testCount = dict["test_count"] as? Int ?? 0
self.navigationC?.isNavigationBarHidden = true
self.navigationC?.pushViewController(VideoListVC, animated: true)
return;
}
}
break
case 3:
break
default: break
}
}else{
let loginVC = SignupiPadStoryboard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
SignupStoryboard = SignupiPadStoryboard
self.navigationC = UINavigationController(rootViewController: loginVC)
}
}
OR you can direct open view controller like whatsapp chat..etc.
#available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: #escaping (UNNotificationPresentationOptions) -> Void)
{
let userInfo = notification.request.content.userInfo
let dictAps = userInfo["aps"] as! NSDictionary
print(dictAps)
let notification_type = dictAps["notification_type"] as? Int ?? 0
if notification_type == 6{
if isChatActive == false{
completionHandler([.alert, .badge, .sound])
}else{
if USERDEFAULT.value(forKey:"isLogin") != nil{
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "ReloadChatMessages"), object: nil, userInfo: (dictAps as! [AnyHashable : Any]))
}else{
completionHandler([.alert, .badge, .sound])
}
}
}else{
completionHandler([.alert, .badge, .sound])
}
}
I use firebase to send a message to ios device, I debug, I have received data payload in Appdelegate in func
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) {
if let messageID = userInfo[gcmMessageIDKey] {
print("Message ID: \(messageID)")
}
I want to do how to open different view controllers based on that data, which means that when I click on the message, I will go to the corresponding view controllers.
I used the code below in Appdelegate but failed
let sb = UIStoryboard(name: "Main", bundle: nil)
let otherVC = sb.instantiateViewController(withIdentifier: "UserNotLoginViewController") as! UserNotLoginViewController
self.window?.rootViewController = otherVC;
when you recive notification in didReceiveRemoteNotification delegate then call the function to pushview to nextviewcontroller.
func application(_ application: UIApplication, didReceiveRemoteNotification
data: [AnyHashable : Any]) {
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .background {
// background
pushToPage(data: data)
}
}
func pushToPage(data:[AnyHashable : Any]){
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let window = appDelegate.window {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController =
storyBoard.instantiateViewController(withIdentifier: "NextViewController") as! NextViewController
window.rootViewController = nextViewController
}
}
Declare this function in your appDelegate and then use it to change rootViewController.
public func makeRootVC(vcName : String) {
let vc = UIStoryboard(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: vcName)
let navigation = UINavigationController(rootViewController: vc)
navigation.navigationBar.isHidden = true
self.window?.rootViewController = navigation
}
usage:
self.makeRootVC("YourViewControllerStoryboardID")
Here it is
func handlePushNotification(userInfo: [String: Any]) {
guard let notificationType = userInfo["nt"] as? String else {
return
}
if notificationType.toInt() == 1 {
self.navigateToViewController1()
} else if notificationType.toInt() == 2 {
self.navigateToViewController2()
}
}
And for navigation, you can use this below function
fileprivate func navigateToViewController1() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let _ = rootViewController.topViewController as? VC1 {
let vc = AppStoryboard.Main.viewController(viewControllerClass: VC3.self)
rootViewController.pushViewController(vc, animated: true)
}
}
}
fileprivate func navigateToViewController2() {
if let rootViewController = self.window?.rootViewController as? UINavigationController {
if let homeVC = rootViewController.topViewController as? VC2 {
}
}
}
Still, you face any issue so please let me know.
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
in my app i implemented push notification. when my app is in running state and if push notification come i am handle that with this code.
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
myid = (userInfo["id"] as? String)!
print(myid)
if let notification = userInfo["aps"] as? NSDictionary,
let alert = notification["alert"] as? String {
var alertCtrl = UIAlertController(title: "Notification", message: alert as String, preferredStyle: UIAlertControllerStyle.Alert)
alertCtrl.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
// Find the presented VC...
var presentedVC = self.window?.rootViewController
while (presentedVC!.presentedViewController != nil) {
presentedVC = presentedVC!.presentedViewController
}
presentedVC!.presentViewController(alertCtrl, animated: true, completion: nil)
}
What i want :: when my app is not running and if push notification come i want to open perticular post based on that notification post id. so how can i do this?
Here is my code
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SVProgressHUD.setDefaultMaskType(SVProgressHUDMaskType.Black)
UIApplication.sharedApplication().applicationIconBadgeNumber = 0
let dicTemp = launchOptions?["UIApplicationLaunchOptionsRemoteNotificationKey"]
if dicTemp != nil{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
myid = (dicTemp["id"] as? String)!
let controller:pushnotificationpostViewController = self.storyboard.instantiateViewControllerWithIdentifier("pushnotificationpostViewController") as! pushnotificationpostViewController
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
}
else
{
window = UIWindow(frame: UIScreen.mainScreen().bounds)
storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller:MainViewController = self.storyboard.instantiateViewControllerWithIdentifier("MainViewController") as! MainViewController
navigation = UINavigationController(rootViewController: controller)
window?.rootViewController = navigation
window?.makeKeyAndVisible()
}
//print(NSUserDefaults.standardUserDefaults().valueForKey("pushnotify")as! Bool)
let notificationTypes: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let pushNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerUserNotificationSettings(pushNotificationSettings)
if (NSUserDefaults.standardUserDefaults().valueForKey("pushnotify")) != nil
{
pushnotification = NSUserDefaults.standardUserDefaults().valueForKey("pushnotify") as! Bool
// let notificationcheck = NSUserDefaults.standardUserDefaults().valueForKey("pushnotify") as! Bool
if (pushnotification == true)
{
application.registerForRemoteNotifications()
}
else
{
application.unregisterForRemoteNotifications()
}
}
else
{
application.registerForRemoteNotifications()
}
return true
}
but by this code i am not getting id means myid is getting nil. so how can i do this?
i think when you quit your application the
didReceiveRemoteNotification method not gets called when you tap on
the notification, instead the method didFinishLaunchingWithOptions
gets called there you have to check weather application is launched by
notification
Put below code in your didFinishLaunchingWithOptions :
var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] as! UILocalNotification)
if notification != nil {
// handle your notification
}
above code is for handling the push notifications if your using local notification then try:
// if launched by the local notification
var notification: UILocalNotification = (launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification)
if notification != nil {
}
So i am trying to add 3D Touch on my application icon using shortCutItems. Here is my AppDelegate.swift:
import UIKit
import Parse
//MARK: - Handle QuickActions For ShorCut Items -> AppDelegate Extension
#available(iOS 9.0, *)
typealias HandleForShorCutItem = AppDelegate
#available(iOS 9.0, *)
extension HandleForShorCutItem {
/// Define quick actions type
enum QuickActionsType: String {
case JegHarAldri = "JEGHARALDRI"
case Pling = "PLING"
case FlasketutenPekerPå = "FLASKETUTENPEKERPÅ"
case KortetTaler = "KORTETTALER"
}
}
#available(iOS 9.0, *)
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
Parse.setApplicationId("XX",
clientKey: "XX")
// Register for Push Notitications
if application.applicationState != UIApplicationState.Background {
// Track an app open here if we launch with a push, unless
// "content_available" was used to trigger a background push (introduced in iOS 7).
// In that case, we skip tracking here to avoid double counting the app-open.
let preBackgroundPush = !application.respondsToSelector("backgroundRefreshStatus")
let oldPushHandlerOnly = !self.respondsToSelector("application:didReceiveRemoteNotification:fetchCompletionHandler:")
var pushPayload = false
if let options = launchOptions {
pushPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] != nil
}
if (preBackgroundPush || oldPushHandlerOnly || pushPayload) {
PFAnalytics.trackAppOpenedWithLaunchOptions(launchOptions)
}
}
if application.respondsToSelector("registerUserNotificationSettings:") {
if #available(iOS 8.0, *) {
let types:UIUserNotificationType = ([.Alert, .Sound, .Badge])
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
}
else {
// Register for Push Notifications before iOS 8
application.registerForRemoteNotificationTypes([.Alert, .Sound, .Badge])
}
//// - Add lines before the return true
let currentInstallation: PFInstallation = PFInstallation.currentInstallation()
currentInstallation.badge = 0
currentInstallation.saveEventually()
//UIApplication.sharedApplication().applicationIconBadgeNumber = 11
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let installation = PFInstallation.currentInstallation()
installation.setDeviceTokenFromData(deviceToken)
installation.saveInBackground()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
if error.code == 3010 {
print("Push notifications are not supported in the iOS Simulator.")
} else {
print("application:didFailToRegisterForRemoteNotificationsWithError: %#", error)
}
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
PFPush.handlePush(userInfo)
if application.applicationState == UIApplicationState.Inactive {
PFAnalytics.trackAppOpenedWithRemoteNotificationPayload(userInfo)
}
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSUserDefaults.standardUserDefaults().removeObjectForKey("KortetTalerKey")
NSUserDefaults.standardUserDefaults().removeObjectForKey("TerningspilletKey")
NSUserDefaults.standardUserDefaults().removeObjectForKey("segmentKey")
NSLog("Keys Deleted")
}
//MARK: - Handle QuickActions For ShorCut Items -> AppDelegate Extension
typealias HandleForShorCutItem = AppDelegate
func handleShortcut( shortcutItem:UIApplicationShortcutItem ) -> Bool {
// 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 true
}
/// Shortcut Item, also called a Home screen dynamic quick action, specifies a user-initiated action for app.
func QuickActionsForItem(shortcutItem: UIApplicationShortcutItem) -> Bool {
// set handled boolean
var isHandled = false
// Get the string type from shorcut item
if let shorchutItemType = QuickActionsType.init(rawValue: shortcutItem.type) {
// Get root navigation controller + root tab bar controller
let rootNavigationController = window!.rootViewController as? UINavigationController
let tabbarController = window!.rootViewController as? UITabBarController
// if needed pop to root view controller
rootNavigationController?.popToRootViewControllerAnimated(false)
// return tabbarcontroller selected
switch shorchutItemType {
case .JegHarAldri:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("JEGHARALDRI") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
isHandled = true
return true
case .Pling:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("PLING") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
isHandled = true
return true
case .FlasketutenPekerPå:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("FLASKETUTENPEKERPÅ") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
isHandled = true
return true
case .KortetTaler:
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("KORTETTALER") as UIViewController
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
isHandled = true
return true
}
}
return isHandled
}
/// Calls - user selects a Home screen quick action for app
func application(application: UIApplication, performActionForShortcutItem shortcutItem: UIApplicationShortcutItem, completionHandler: (Bool) -> Void) {
// perform action for shortcut item selected
let handledShortCutItem = QuickActionsForItem(shortcutItem)
completionHandler(handledShortCutItem)
}
}
I have some issues. FYI! I do not have a tabBarController in my project.
When i open my application using the 3D Touch items, it freezes for 4-5 seconds before it opens the application. When the application is opened, the back buttons does not work.
So is there any possible ways to open the app on the initial controller, and perform segue to the correct view controller?
private func showViewController(viewControllerKey: ViewControllerKeys) -> Bool
{
// Init the storyboard
let storyboard = UIStoryboard(name: "Main", bundle: nil)
// Init the root navigationController
guard let navigationController = storyboard.instantiateInitialViewController() as? UINavigationController else { return false }
// Set the root navigationController as rootViewController of the application
UIApplication.sharedApplication().keyWindow?.rootViewController = navigationController
// Init the wanted viewController
guard let viewController = storyboard.instantiateViewControllerWithIdentifier(viewControllerKey.rawValue) else { return false }
// Push this viewController
navigationController.pushViewController(viewController, animated: false)
return true
}
Please find more impletation details of how to handle quick actions in my QuickActionsManager, or checkout the entire 3DTouch sample of code