See view in tab bar when login success - swift

I have 4 tabs in Tabbar View Controller in storyboard. When open app tab 1 appear and when tap tab 2-4 I want to show modal view for login. When login success, app will show view in tab 2-4. How can I do this ?

you did not provide information on how you want to make a check if the user is logged in. but anyway, here is what i did with Firebase
in tab 2-4, check if user is logged in in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
FIRAuth.auth()?.addAuthStateDidChangeListener({ (auth, user) in
if let user = user {
} else {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let signInViewController = storyboard.instantiateViewControllerWithIdentifier("SignIn")
self.presentViewController(signInViewController, animated: true, completion: nil)
}
})
}
once the view is loaded, it will check if the user is logged in, if not present the viewcontroller SignIn
once logged in is completed, you just have to dismiss the viewcontroller and it would return to which tab that the user clicked on previously
dismissViewControllerAnimated(true, completion: nil)

Related

Can't present view controller

I have a function that checks if the user is logged in. If the user is not logged in I want to present a login view controller. But as I mentioned in the title, the view controller is not shown. In addition, the initial view controller is not the login view controller (which is set in a storyboard and is a tab bar view controller). In fact, that view controller is in a different storyboard. Is that the problem?
private func validateAuth(){
if FirebaseAuth.Auth.auth().currentUser == nil {
let storyboard = UIStoryboard(name: "LoginAndRegisterStoryboard", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "LoginViewController")
self.present(vc, animated: true, completion: nil)
}
}
I tried to set the breakpoint to the function. The debugger says that the function is executed but the view controller is not shown. Any solutions?
And yes, the function is called.

Dismiss to previous view controller when user tapped in Instagram login button

Dismiss to previous view controller when user tapped in Instagram login button.
if UserDefaults.standard.bool(forKey: "InstaLogin") != true {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "WebViewViewController") as? WebViewViewController
vc?.textUrl = "https://www.instagram.com"
vc?.socialMedia = kConstant.plateformName.instagram
self.navigationController?.pushViewController(vc!, animated: true)
UserDefaults.standard.set(true, forKey: "InstaLogin")
}
}
This will open Instagram login page in web view controller. Like this-:
I want to dismiss this controller when user tap on login button of Instagram login page. how to get acknowledgement when user tapped on login button

Making a new view controller appear after interstitial ad is dismissed

I am building an iOS app in swift with Xcode that has a button that, when pressed, will (among other things) show an interstitial Admob ad, and then go to a different view controller. Right now, when I press the button, an interstitial ad will appear. However, when I close the ad, the view is still on the original page. If I click the button again, then I am taken to the second view controller. Sometimes, however, I have to dismiss three or four ads before I get to the second view controller. How can I make it so that, after I dismiss the interstitial ad, I go straight to the second view controller?
Here is the current code:
#IBAction func postPressed(_ sender: Any) {
if contact.text?.isEmpty == true {
print("No contact")
return
}
if avail.text?.isEmpty == true {
print("No availability")
return
}
uploadSpot()
}
func uploadSpot() {
let key = Int.random(in: 20001..<30000)
let object: [String: Any] = [
"Contact": contact.text!,
"Availability": avail.text!,
]
geoFireRef.child("\(key)").setValue(object)
let location = manager.location!
geoFire.setLocation(location, forKey: "\(key)")
if interstitialAd?.isReady == true {
interstitialAd?.present(fromRootViewController: self)
}
let storyboard = UIStoryboard(name: "SpotLoaded", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "SpotLoaded")
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true)
}
The import part is in the function "uploadSpot()," where
if interstitialAd?.isReady == true {
interstitialAd?.present(fromRootViewController: self)
}
presents the ad and
let storyboard = UIStoryboard(name: "SpotLoaded", bundle: nil)
let vc = storyboard.instantiateViewController(identifier: "SpotLoaded")
vc.modalPresentationStyle = .overFullScreen
present(vc, animated: true)
switches the viewcontroller. I have tried reversing the order if these two (i.e. putting let storyboard, etc., before if insterstitialAd), but that just takes me to the new view controller and the ad never appears. I have also tried putting the let storyboard chunk inside of the brackets following the if interstitialAd. This does not help either. If I put it after interstitialAd?.present(fromRootViewController: self), I have the same problem where the ad shows but the view controller does not change. If I put it after, the viewcontroller changes but the ad never shows.
As the main purpose of interstitial ads is to appear in between views of an app, I do not think what I am trying to do should be so difficult. I think there must just be something I am missing or do not understand. Please help in whatever way you can.

Firebase Sign Out Not Changing Views Working

I have some firebase code with a listener set up in AppDelegate and a log out set elsewhere. It is not taking me to the login view controller when I click sign out.
Logout Code:
#IBAction func logOutTapped(_ sender: Any) {
try! Auth.auth().signOut()
}
App Delegate Code:
let authListener = Auth.auth().addStateDidChangeListener { auth, user in
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
self.window = UIWindow(frame: UIScreen.main.bounds)
if user != nil
{
...//Some code
}
else
{
print("Headed to login view controller.")
let loginViewController = storyBoard.instantiateViewController(withIdentifier: "loginViewController") as! LoginViewController
self.window?.rootViewController = loginViewController
self.window?.makeKeyAndVisible()
}
I know the sign out code is calling the "else" in app delegate, because my logs are printing "Headed to login view controller" when I press logout. However, the login view controller is never displayed. It stays on the screen where the user clicks log out. When the app is stopped and started again, the login screen is displayed. The storyboard identifier is correctly set as loginViewController.
Any ideas?

Swift viewWillAppear redirect

I'm using "viewWillAppear" to check UserDefaults.standard if i have data on the defaults i want redirect to Dashboard View and pass out of Login.
When i Log In correctly the app redirect normally but not in viewWillAppear in case of have data.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let defaults = UserDefaults.standard
if defaults.string(forKey: "username") != nil {
print("Exist")
print(defaults.string(forKey: "username")!)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "Dashboard")
self.present(vc, animated: true, completion: nil)
}
}
And i get this error con debug.
2016-11-04 17:44:32.130024 RHiOS[1155:278240] Warning: Attempt to present <RHiOS.MenuViewController: 0x100f0d480> on <RHiOS.ViewController: 0x100e0b880> whose view is not in the window hierarchy!
How can i redirect to Dash in case of have data? Thanks.
You cannot present a ViewController in viewWillAppear. Simply put this code in viewDidAppear and it should fix your problem.
When the view is not visible, it's not in the window hierarchy as the error mentioned. You could also create a segue in your storyboard and call performSegue(withIdentifier:sender:) instead.
You won't be able to present anything when the view hasn't appeared yet, so you either have to move your code into viewDidAppear or prevent this view from showing in the first place and redirect directly to the dashboard from a parent view controller that presents either the dashboard or login screen depending on whether or not you have data.