AppDelegate presents views twice - ios

I'm implementing quick-actions for my app and for that I need to present viewcontrollers from the app delegate. I use code like this:
let viewController = storyboard.instantiateViewController(withIdentifier :"MyControllerID") as! MyController
let navController = UINavigationController.init(rootViewController: viewController)
if let window = self.window, let rootViewController = window.rootViewController {
var currentController = rootViewController
while let presentedController = currentController.presentedViewController {
currentController = presentedController
}
currentController.present(navController, animated: true, completion: nil)
}
If the app is in background and the quickaction is clicked everything works fine, but if the app hasn't been started yet and the quick-action gets clicked the view is presented twice.
What am I doing wrong here?
// Edit 1: This code is in a function and the function is called in applicationDidBecomeActive
In didFinishLaunchingWithOptions I check if the app was launched via Quick-Actions like this:
if let shortcutItem = launchOptions?[UIApplicationLaunchOptionsKey.shortcutItem] as? UIApplicationShortcutItem {
launchedShortcutItem = shortcutItem
}
The launchedShortcutItem is a variable:
var launchedShortcutItem: UIApplicationShortcutItem?
// Edit 2: For those who don't know what I mean by "Quick-Actions" here are the Apple Pages for them: Guideline, Documentation

Related

ios - Change tabs and open modal window dynamically

I have an app that sends local notiffications at specific times. When the user taps on the notification, the app should open on a particular tab (a library tab) and then open automatically one of the lessons (without the user having to tap on it).
I can get the app to switch to the Library tab, but the modal with the lesson never appears.
This is the code I'm using:
if let tabBarController = window.rootViewController as? UITabBarController {
// this works
tabBarController.selectedViewController!.dismiss(animated: true, completion: nil)
tabBarController.selectedViewController = tabBarController.viewControllers?[1]
// Segue to particular lesson
let vc = tabBarController.selectedViewController as? LibraryViewController
let lessonVC = LessonViewController()
let les60 = Lesson(lessonNumber: "60")
lessonVC.delegate = vc
lessonVC.lesson = les60
vc?.present(UINavigationController(rootViewController: lessonVC), animated: true)
}
This is the code I use for every item in the Library:
let lessonVC = LessonViewController()
lessonVC.delegate = self
lessonVC.lesson = lessonsArray[indexPath.row]
present(UINavigationController(rootViewController: lessonVC), animated: true)
so I tried to replicate it above but it does not work.

Opening ViewController In AppDelegate While Keeping Tabbar

In my Xcode project when a user taps on a notification I want to first send them to a certain item in my tabBar then I want to instantiate a view controller and send an object over to that view controller. I have code the that sends them to the tabBar I want, but I do not know how to instantiate them to the view controller while keeping the tabBar and navigation bar connected to the view controller. All the answers on this require you to change the root view controller and that makes me lose connection to my tabBar and navigation bar when the view controller is called.
A Real Life Example of this: User receives Instagram notification saying "John started following you" -> user taps on notification -> Instagram opens and shows notifications tab -> quickly send user to "John" profile and when the user presses the back button, it sends them back to the notification tab
Should know: The reason why I'm going to a certain tab first is to get that tab's navigation controller because the view controller I'm going to does not have one.
Here's my working code on sending the user to "notifications" tab (I added comments to act like the Instagram example for better understanding):
if let tabbarController = self.window!.rootViewController as? UITabBarController {
tabbarController.selectedViewController = tabbarController.viewControllers?[3] //goes to notifications tab
if type == "follow" { //someone started following current user
//send to user's profile and send the user's id so the app can find all the information of the user
}
}
First of all, you'll to insatiate a TabBarController:
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
And then insatiate all of the viewControllers of TabBarController. If your viewControllers is embedded in to the UINavigationController? If so, you'll to insatiate a Navigation Controller instead:
let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController
Also you should instantiate your desired ViewController too:
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
Make all of the NavigationControllers as viewControllers of TabBarController:
tabBarController.viewControllers = [first, second, third]
And check: It's about your choice.
if tabBarController.selectedViewController == first {
// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)
// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)
}
Make tabBarController as a rootViewController:
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
Finally: It's your completed code:
func openViewController() {
let storyboard = UIStoryboard.init(name: "YourStoryboardName", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourTabBarController") as! UITabBarController
let first = storyboard.instantiateViewiController(withIdentifier: "YourFirstNavigationController") as! UINavigationController
let second = storyboard.instantiateViewiController(withIdentifier: "YourSecondNavigationController") as! UINavigationController
let third = storyboard.instantiateViewiController(withIdentifier: "YourThirdNavigationController") as! UINavigationController
let desiredVC = storyboard.instantiateViewController(withIdentifier: "desiredVC") as! ExampleDesiredViewController
tabBarController.viewControllers = [first, second, third]
if tabBarController.selectedViewController == first {
// Option 1: If you want to present
first.present(desiredVC, animated: true, completion: nil)
// Option 2: If you want to push
first.pushViewController(desiredVC, animated. true)
}
self.window = UIWindow.init(frame: UIScreen.main.bounds)
self.window?.rootViewController = tabBarController
self.window?.makeKeyAndVisible()
}
If you want to present or push ViewController when the notification is tapped? Try something like that:
extension AppDelegate: UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDefaultActionIdentifier:
openViewController()
completionHandler()
default:
break;
}
}
}
I can think of two ways to do that:
1) If that view controller is a UINavigationController you can simply push the profile from wherever you are:
if let tabNavigationController = tabbarController.viewControllers?[3] as? UINavigationController {
tabbarController.selectedViewController = tabNavigationController
let profileViewController = ProfileViewController(...)
// ... set up the profile by setting the user id or whatever you need to do ...
tabNavigationController.push(profileViewController, animated: true) // animated or not, your choice ;)
}
2) Alternatively, what I like to do is control such things directly from my view controller subclass (in this case, PostListViewController). I have this helper method in a swift file that I include in all of my projects:
extension UIViewController {
var containedViewController: UIViewController {
if let navController = self as? UINavigationController, let first = navController.viewControllers.first {
return first
}
return self
}
}
Then I would do this to push the new view controller:
if let tabViewController = tabbarController.selectedViewController {
tabbarController.selectedViewController = tabViewController
if let postListViewController = tabViewController.containedViewController as? PostListViewController {
postListViewController.goToProfile(for: user) // you need to get the user reference from somewhere first
}
}
In my last live project, I'm using the same approach like yours. So even though I doubt this method is the correct or ideal for handling a push notification from the AppDelegate (I still got a lot of stuff to learn in iOS 🙂), I'm still sharing it because it worked for me and well I believe the code is still readable and quite clean.
The key is to know the levels or stacks of your screens. The what are childViewControllers, the topMost screen, the one the is in the bottom, etc...
Then if you're now ready to push to a certain screen, you would need of course the navigationController of the current screen you're in.
For instance, this code block is from my project's AppDelegate:
func handleDeeplinkedJobId(_ jobIdInt: Int) {
// Check if user is in Auth or in Jobs
if let currentRootViewController = UIApplication.shared.keyWindow!.rootViewController,
let presentedViewController = currentRootViewController.presentedViewController {
if presentedViewController is BaseTabBarController {
if let baseTabBarController = presentedViewController as? BaseTabBarController,
let tabIndex = TabIndex(rawValue: baseTabBarController.selectedIndex) {
switch tabIndex {
case .jobsTab:
....
....
if let jobsTabNavCon = baseTabBarController.viewControllers?.first,
let firstScreen = jobsTabNavCon.childViewControllers.first,
let topMostScreen = jobsTabNavCon.childViewControllers.last {
...
...
So as you can see, I know the hierarchy of the screens, and by using this knowledge as well as some patience in checking if I'm in the right screen by using breakpoints and printobject (po), I get the correct reference. Lastly, in the code above, I have the topMostScreen reference, and I can use that screen's navigationController to push to a new screen if I want to.
Hope this helps!

Changing rootViewController causing weird behavior

I am setting rootViewController like this in my app.
func setupMainView() {
let rootViewContorller = window?.rootViewController
if (rootViewContorller?.presentedViewController != nil || rootViewContorller?.presentingViewController != nil) {
rootViewContorller?.dismiss(animated: false, completion: nil)
}
let tabbarController = UITabBarController()
tabbarController.delegate = self
let homeViewController = HomeViewController()
let rewardsViewController = RewardsViewController()
let homeNVc = UINavigationController()
let rewardsNVc = UINavigationController()
homeNVc.viewControllers = [homeViewController]
rewardsNVc.viewControllers = [rewardsViewController]
tabbarController.viewControllers = []
tabbarController.viewControllers = [homeNVc, rewardsNVc]
tabbarController.selectedIndex = 0
self.window?.rootViewController = tabbarController
}
It is working fine. But I have to change rootViewController in the app like after registration etc. After that When I go to Debug View Hierarchy . I still see the registrationViewController there. And lets say If I change rootViewController 3-4 times all previous controllers are still there. So my question is How can I remove all viewControllers from memory before changing the rootViewController.
You can call this function this will clear all viewcontrollers.
self.window?.rootViewController?.dismissViewControllerAnimated(false, completion: nil)

Getting Black Screen with navigationController?.pushViewController (Swift 3.0)

I am having problems programmatically sending a user from one view controller to another. I am posting the code associated with he flow below. In addition to letting me know what the correct code is (which I would appreciate) I'd also be interested if the logic/design itself seems OK.
I am controlling my UI programmatically. Accordingly, in my app delegate didfinnishlaunchingwithoptions I have
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
FIRApp.configure()
window?.rootViewController = SigninController()
When the user opens the app, they are redirected to the SigninController.
Then, inside of SigninController, I am handling all of the social authentication stuff against Firebase. I have a listener in my code to confirm that the user is (or is not) authenticated and sends him along:
let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFacebookAuth()]
FUIAuth.defaultAuthUI()?.providers = provider
// listen for changes in the authorization state
_authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in
// check if there is a current user
if let activeUser = user {
// check if the current app user is the current FIRUser
if self.user != activeUser {
self.user = activeUser
self.signedInStatus(isSignedIn: true)
print("user session is active, redirecting...")
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
}
} else {
// user must sign in
self.signedInStatus(isSignedIn: false)
self.loginSession()
}
}
}
In the above code, if the user is confirmed as signed in, then I use the below code to send them along. This is where I am having the problem. Right now I just see a black screen but no error message.
let nextViewController = CustomTabBarController()
self.navigationController?.pushViewController(nextViewController, animated: true)
And here is the code for the CUstomTabBarController class.
class CustomTabBarController : UITabBarController
{
override func viewDidLoad()
{
super.viewDidLoad()
let home = createNavController(imageName: "gen-home", rootViewController: HomeController(collectionViewLayout: UICollectionViewFlowLayout()))
let loc = createNavController(imageName: "loc-map-route", rootViewController: LocController(collectionViewLayout: UICollectionViewFlowLayout()))
let stats = createNavController(imageName: "pre-bar-chart", rootViewController: StatsController(collectionViewLayout: UICollectionViewFlowLayout()))
let profile = createNavController(imageName: "account", rootViewController: ProfileController(collectionViewLayout: UICollectionViewFlowLayout()))
viewControllers = [home, loc, stats, profile]
}
private func createNavController(imageName: String, rootViewController: UIViewController) -> UINavigationController
{
let navController = UINavigationController(rootViewController: rootViewController)
navController.tabBarItem.image = UIImage(named: imageName)
return navController
}
}
I am sure I am overlooking something silly, but sometimes it takes another pair of eyes to point it out.
Thanks in advance.
Here
self.navigationController?.pushViewController(nextViewController, animated: true)
there is no navigationController.
In your AppDelegate you need to do this:
let rootViewController = SigninController()
let navController: UINavigationController = UINavigationController(rootViewController: rootViewController)
FIRApp.configure()
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
It's better to assign rootViewController to the window before making it visible. Otherwise there may be screen blinks.

performSegueWithIdentifier not working when being called from new instance of viewController in Swift

My mission
When app receive notification and user taps on the notification i want to redirect the user to the correct View. In my case, SingleApplicationViewController.
Current code
PushNotification.swift - A class with static functions to handle behaviors when receiving Push Notifications
The __getNavigationController returns a specific NavigationController based on a tab -and viewIndex from TabBarController.
internal static func __getNavigationController(tabIndex: Int, viewIndex: Int) -> UINavigationController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
window?.rootViewController = viewController
let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
rootViewController.selectedIndex = tabIndex
let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController
return nav
}
The applicationClicked is being called when user click on notification and that method calls on __getApplication to fetch the application from the db with the objectId received in the push notification and then instantiate a GroupTableViewController to perform segue to the SingleApplicationViewController.
(TabbarController -> Navigation Controller -> GroupTableViewController -> SingleApplicationViewController)
What is a bit strange is when I set tabIndex to 0 and viewIndex to 1. The GroupView however is on second tab (tab 1) and the view controller should be the first (0). But when I set them to the corresponding numbers, I receive nil and the application crashes.
I read that you will force the view controller to load when doing _ = groupTableViewController.view and which it actually does. When this is being called, the viewDidLoad -function is being called.
/************** APPLICATION ***************/
static func applicationClicked(objectId: String) {
__getApplication(objectId) { (application, error) in
if application != nil && error == nil {
let nav = __getNavigationController(0, viewIndex: 1)
let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController
_ = groupTableViewController.view
groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!)
} else {
// Hanlde error
}
}
}
GroupTableViewController.prepareForSegue()
Here I create a new instance of the ApplicationTableViewController, which is a middle step before getting to SingleApplicationViewController
} else if segue.identifier == "GroupTableToApplicationToDetailApplication" {
let navC = segue.destinationViewController as! UINavigationController
let controller = navC.topViewController as! ApplicationViewController
controller.performSegueWithIdentifier("ApplicationsToSingleApplicationSegue", sender: sender as! Application)
}
So, what's not working?
Well, the prepareForSegue in GroupTableViewController is not being called. I use the same code structure on my TimeLineViewController, and almost the exact same code, when getting another Push Notification and it works perfectly. In that case I use tabIndex 0 and viewIndex 0 to get the proper NavigationController.
Please, any thoughts and/or suggestions is more than welcome!
There is change in following method..
internal static func __getNavigationController(tabIndex: Int) -> UINavigationController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let window:UIWindow? = (UIApplication.sharedApplication().delegate?.window)!
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyBoard.instantiateViewControllerWithIdentifier("MainEntry")
window?.rootViewController = viewController
let rootViewController = appDelegate.window!.rootViewController as! UITabBarController
rootViewController.selectedIndex = tabIndex
let nav = rootViewController.selectedViewController as! UINavigationController //This will return navigation controller..
//No need of viewIndex..
return nav
}
you have written
let nav = rootViewController.viewControllers![viewIndex] as! UINavigationController
change to rootViewController.selectedViewController give you UINavigationController.
Here you get navigavtion controller object..In your applicationClicked method nav object might be nil so it can not execute further performsegue code.
Check following method.
/************** APPLICATION ***************/
static func applicationClicked(objectId: String) {
__getApplication(objectId) { (application, error) in
if application != nil && error == nil {
let nav = __getNavigationController(0)//0 is your tab index..if you want 1 then replace it with 1
let groupTableViewController = nav.viewControllers.first as! GroupsTableViewController //Rootview controller of Nav Controller
groupTableViewController.performSegueWithIdentifier("GroupTableToApplicationToDetailApplication", sender: application!) //Perform seque from Root VC...
} else {
// Hanlde error
}
}
}

Resources