How to pass data from appdelegate to a viewcontroller? - ios

Hello I am trying to implement a push notification feature in my IOS app. Currently I have code that will open a specific viewcontroller if the app was opened through the push notification. The way i'm doing this is by pushing the viewcontroller ontop.
What I need to do now is to pass some data to the viewcontroller from the appdelegate. I understand to pass data from viewcontroller to viewcontroller is to use prepareforsegue. I tried doing this and it did not work
I tried researching how to accompolish this task but a lot of the answers on stackoverflow were outdated swift code that I don't have the ability to convert to swift 3. can someone explain to me how would i send data from appdelegate to a viewcontroller?
heres the code to show the VC which is in didFinishLaunchingWithOptions
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationViewController, animated: false)

//Declare you variable somewhere within the app delegate scope
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var myVariable: String = "Hello Swift"
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
// some other app delegate’s methods....
}
//In your view controller
class ViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let myOtherVariable = appDelegate.myVariable
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myLabel.text = myOtherVariable
var anotherVariable: String = appDelegate.myVariable // etc...
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

OK - you've got most of the work already done...
When using segues, iOS creates your view controller and gives you access to it in prepare, where you probably have done something like this:
if let vc = segue.destination as? detailPinViewController {
vc.myVariable = "this is the data to pass"
}
In didFinishLaunchingWithOptions, you are doing the creation part, and the "presentation" part... so you can just add in the "pass the data" part:
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "detailPin2") as! detailPinViewController
destinationViewController.myVariable = "this is the data to pass"
let navigationController = self.window?.rootViewController as! UINavigationController
navigationController.pushViewController(destinationViewController, animated: false)
and that should do it.

Related

Clean VIP with Delegate Pattern

I am new to Clean VIP Architecture and I am struggling with its entry point.
(I am only putting some bit of code)
ViewController
protocol Delegate: class {
func execute()
}
class TitlesViewController:UIViewController {
weak var delegate: Delegate?
func viewDidLoad() {
super.viewDidLoad()
delegate.execute()
}
}
Configurator
class TitlesConfigurator {
static func configureModule(viewController: TitlesViewController) {
let interactor = Interaction()
viewController.delegate = interactor
}
}
In AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let titlesViewController = TitlesViewController()
let navigationController = UINavigationController(rootViewController: titlesViewController)
TitlesConfigurator.configureModule(viewController: titlesViewController)
window = UIWindow()
window?.rootViewController = navigationController
window?.makeKeyAndVisible()
return true
}
Now Issue that I am facing is that there is no reference of interactor outside of TilesConfigurator and delegate is weak which means its total arc is 0. It results in delegate = nil inside viewDidLoad
How can I improve or fix this issue in my architecture.
P.S: I don't think its good practice to make a strong reference of delegate inside ViewController
Delegate shouldn't be weak here
var delegate: Delegate?
As there is one part that's weak which is let interactor = Interaction() so no retain cycles will occur

Prevent iOS app from loading default Storyboard entry point

I am following this tutorial on Coordinator pattern, and so far I have it going through creating the coordinator and running the start() from app delegate. But then calling a function on that coordinator doesn't work, because suddenly the coordinator var is nil. It seems that the initial view controller that is shown is not the one coming from the coordinator.start() but rather the one from the storyboard entry point. I did disable the Main in the main interface of the projects target.
AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if let registry = DependencyResolver.shared as? DependencyRegistry {
DependencyGraph.setup(for: registry)
}
let navController = UINavigationController()
coordinator = MainCoordinator(navigationController: navController)
coordinator?.start()
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
Main coordinator:
class MainCoordinator: Coordinator {
var navigationController: UINavigationController
var childCoordinators = [Coordinator]()
init(navigationController: UINavigationController) {
self.navigationController = navigationController
}
func start() {
let vc = InitViewController.instantiate()
vc.coordinator = self. //!!I do hit this breakpoint
navigationController.pushViewController(vc, animated: false)
}
}
Init view controller (the one that's initial in the storyboard, but I'm showing it with coordinator):
class InitViewController: UIViewController, Storyboarded {
private var cameraViewModel: CameraViewModel!
weak var coordinator: MainCoordinator?
#IBAction func openCameraView(_ sender: Any) {
coordinator?.openCameraView() //!!here coordinator is nil
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Storyboarded protocol - used to get view controller out of the storyboard by id:
protocol Storyboarded {
static func instantiate() -> Self
}
extension Storyboarded where Self: UIViewController {
static func instantiate() -> Self {
let fullName = NSStringFromClass(self)
let className = fullName.components(separatedBy: ".")[1]
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
let leController = storyboard.instantiateViewController(withIdentifier: className) as! Self
return leController
}
}
The problem is merely that the tutorial you're looking at is too old for current conditions. There is no Single View App template any more, and the App Delegate no longer contains the window. If you create a project in Xcode 11 or Xcode 12, the window is owned by the scene delegate. Implement the scene delegate's willConnect method to do the work that the app delegate was doing in the tutorial, and everything will spring to life.
Also the mechanism for preventing Main.storyboard from trying to load automatically has changed; you have to remove it from Application Scene Manifest in the Info.plist (editing by hand — there is no simple interface).

Swift weak delegate

my code:
public func start() {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
guard let listVC = storyboard.instantiateViewController(withIdentifier: "ListVC") as? ListVC else { return }
let viewModel = ListViewModel(dependencies: appDependencies)
viewModel.delegate = self
listVC.listViewModel = viewModel
navigationController?.pushViewController(listVC, animated: true)
}
protocol ListViewModelDelegate: class {
func needChangeScreen(cellViewModel: UserCellViewModel)
}
final class ListViewModel {
weak var delegate: ListViewModelDelegate?
func userPressed(at index: IndexPath) {
delegate?.needChangeScreen(cellViewModel: cellViewModels[index.row])
}
}
User pressed is called from UIViewController , then i want to send callback to Coordinator to start another coordinator but delegate? is always nil. I know that delegates should be weak but in this case is not working for me. Any ideas?
Okey i have fix but i do not know is it good.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let navigationController = UINavigationController()
navigationController.setNavigationBarHidden(true, animated: false)
window?.rootViewController = navigationController
let appDependencies = configureAppDependencies()
let coordinator = AppCoordinator(navigationController: navigationController, appDependencies: appDependencies)
coordinator.start()
window?.makeKeyAndVisible()
return true
}
That was my app delegate function(In AppCoordinator start is created and pushed ListCoordinator) but when i changed let coordinator for instance var :
var coordinator: AppCoordinator?
weak delegate is not nil and everything works.

Add Disclaimer Screen [duplicate]

This question already has answers here:
set initial viewcontroller in appdelegate - swift
(24 answers)
Closed 5 years ago.
I am trying to add a disclaimerViewController to my application as the initial viewController, which, if accepted will lead the user tot he entryViewController, and if not, will not allow the user to use the application. However, I want to to be that if the user accepts the disclaimer, any time after he opens the application he won't be presented with the disclaimerViewControleler but with the entryViewController.
I know that it has something do to with editing the AppDelegate.swift file, but am unsure of were to start.
You need to save user choice in UserDefaults
The code below is using Swift 3
If you don't want to load entryViewController then In the AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}else{
//show entryViewController
}
return true
}
Or you can load entryViewController and present disclaimer instantly, then in your entryViewController:
override func viewDidLoad() {
super.viewDidLoad()
//retrieve values from UserDefaults
//for the first time it will be false, because it was not set earlier
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
//present your disclaimer here
}
}
In the DisclaimerVC:
#IBAction func accept(_ sender: UIButton){
//it can be another action in your controller
//but anyway, you should save user choice after that
UserDefaults.standard.set(true, forKey: "isAccepted")
//add code here to dismiss disclaimer
}
The way I would go about doing this is by setting your disclaimerViewController to the initial view controller in your storyboard file.
In the swift file check to see if the user has previously accepted the disclaimer in the viewDidLoad method, if they haven't, allow the code to function normally and display the screen, if they have push the user on to your entryViewController
If you want to set your initial view controller in the AppDelgate and not in the storyboard, here is a useful link: set initial viewcontroller in appdelegate - swift
To transition to a new viewcontroller in swift, here is a useful link: Switching view controllers in swift
The smoothest way to implement this is to switch the views programmatically.
Make sure to set the restoration ids for your ViewControllers in the StoryBoard.
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var mainViewController: ViewController?
var acceptViewController: AcceptViewController?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
mainViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainView") as? ViewController
acceptViewController = mainStoryboard.instantiateViewController(withIdentifier: "AcceptView") as? AcceptViewController
if mainViewController == nil {
print("mainViewController is nil")
}
if acceptViewController == nil {
print("acceptViewController is nil")
}
let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted")
if isAccepted == false {
switchToAcceptViewController()
} else {
switchToMainViewController()
}
return true
}
func switchToMainViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = mainViewController
self.window?.makeKeyAndVisible()
}
func switchToAcceptViewController() {
//mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view
self.window?.rootViewController = acceptViewController
self.window?.makeKeyAndVisible()
}
}
AcceptViewController
class AcceptViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func acceptAction(_ sender: Any) {
UserDefaults.standard.set(true, forKey: "isAccepted")
let appDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.switchToMainViewController()
}
}
You can get the full project here: https://github.com/ryantxr/legendary-fiesta

Dismissing initial ViewController

I am developing an app using swift 3 . I launch a tutorial like view controller on the first launch of my app and close it using a button .But the inital view controller does not get dismissed even when i click the close button. I know the question sounds familiar but i am unable to get a correct solution for it .
Here's the code of the the AppDelegate
import UserNotifications
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application:
UIApplication,didFinishLaunchingWithOptions launchOptions: [
UIApplicationLaunchOptionsKey: Any]?) -> Bool
{
let launchedBefore = UserDefaults.standard.bool(forKey: "launchedBefore")
if launchedBefore {
print("This is not the first Launch")
} else
{
print("First Launch, setting UserDefaults")
UserDefaults.standard.set(true, forKey: "launchedBefore")
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let initiaView = storyBoard.instantiateViewController(withIdentifier: "FirstLaunchViewController")
self.window?.rootViewController = initiaView
self.window?.makeKeyAndVisible()
}
return true
}
Here's my code from the initialViewController
class FirstLaunchViewController:UIViewController
{
override func viewDidLoad() {
}
#IBAction func doneButtonTapped(_ sender: Any)
{
_ = navigationController?.popViewController(animated:true)
}
}
Try this
let rootViewController = self.window?.rootViewController // this is your initial viewcontroller set initial viewcontroller in storyboard
let initiaView = storyBoard.instantiateViewController(withIdentifier: "FirstLaunchViewController")
rootViewController.pushViewController(initiaView, animated: true)
Try setting the view controller as root view controller for window which you want to show after dismissing the initial view controller. Present the initial view controller from window root view controller

Resources