How to redirect a user to a login page if they are not logged in? Swift 3 - swift

I'm fairly new to the whole Swift thing, but I'm trying to make a simple login screen where if the user is not logged in, it redirects them to a login page. I seem to have something not write in the code as when I click on the tab to take me to the login page as I'm not logged in nothing comes Up? When I type in the code, Xcode says everything is fine but I've named the root loginView and everything seems to be alright, but when I load it nothing happens it just come up with the page as If you were logged in.
Any ideas?
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.
}
override func viewDidAppear(_ animated: Bool)
{
self.performSegue(withIdentifier: "loginView", sender: self);
}
}

As you do not get any errors I believe you've correctly named your segue right? if that's the case. Please make sure you're doing as following:
Let's assume you have 2 View Controllers one is the main page that logged in users can go (we name it LandingVC) and the other one is the login view controller (we call it LoginVC).
First thing to check: make sure you're writing the code in LandingVC ?
Second: Have you assigned the class to VCs in StoryBoard? (see picture for reference)

Related

UIButton disappears after succesfull build

I am facing problem with my new app. I have developed many apps, but this problem i have for the first time. For testing my app I am using Apple iPad Mini 4.
I have a new, empty project. I have added only one UIButton. But when I build this project, succesfull build, and the app is running, after 2 seconds the UIButton disappears. When I use iPad or iPhone simulator, the button is there forever. No error appears, so i really do not know, what is going on. Anybody with similar problem?
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func example(_ sender: UIButton) {
print("Hello")
}
}
I have tried the same thing but working fine....no issues.
What you are saying is not possible It's not possible that way.
It's only possible if you put your button somewhere in LaunchScreen.
If you put you button on launch screen you can still add action to ViewController but you can't add outlet.
Verify this thing and let me know.

xcode / swift receiving user data through a form

I've been designing an app where the user needs to be able to send me the value of certain variables within the app. I originally planned to use a simple mail setup where they fill out a form and the game data is automatically filled out in the email.
Is there any way a user can click a button, and send me the values of variables within the app without the user specifically seeing or being able to modify the variables?
The user consents and realizes what they are doing to. I've been looking at firebase and seeing if there's anything there which can help me. Any ideas would be greatly appreciated.
Is there any way a user can click a button, and send me the values of
variables within the app without the user specifically seeing or being
able to modify the variables?
Yes. Add a UIButton to your storyboard and create a IBAction for it. Whenever the button gets tapped the IBAction gets called. In this case the IBAction is named didTapbutton. See the code below:
import UIKit
class ViewController: UIViewController {
var secretName = "Bond"
var secretNumber = 007
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func didTapButton(_ sender: Any) {
mail(to: secretName, secretNumber: secretNumber)
}
func mail(to secretName: String, secretNumber: Int) {
print("hello \(secretName + secretNumber)")
}
}
didTapButton uses the local variables secretName and secretNumber "within the app" without the user seeing the values.

SwiftSpinner not working on UIViewController.

Ok, so I am using this project: https://github.com/icanzilb/SwiftSpinner
I downloaded the project and took the Swiftspinner.Swift file from the directory and added it to my project.
Now, When I add in a test like so:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
SwiftSpinner.show("Test")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Nothings happens when I run it. no errors it just says build succeeded and then opens in the simulator.
However, If I change the class delegation at the top to this:
class ViewController: UINavigationController {
I works, Can anyone fathom this out?
Move the call to viewDidAppear(_:).
The problem is that ViewController is more than likely the initial view that is created when you created the project. viewDidLoad() is called when .view is called on your controller. Since this is the initial view controller it has not yet been added to the key window. As a result the spinner adds itself to the key window then ViewController is added to key window covering up the spinner. If you used the view hierarchy debugger you will be able to see this happen. Once the root view is set this would no longer be an issue but it is still best practice to modify the UI after the view has appeared.

Start the animation of something when app starts

I am trying to learn Swift. I currently have a button that plays an animation but I want to have the animation start immediately after the app loads. How would I go about doing that?
You need to put your animation on the viewDidApear method, while will be called after your viewDidLoad method.
For example
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
UIView.animateWithDuration(2, animations: {
//do what ever here
})
}
Your question is not very specific so I'm not sure if this answers your question.
When your iOS application starts, there is usually a View Controller that gets rendered on the screen as your main entry point. This can be changed in in your AppDelegate.swift
Now, let's assume that in your application, a view controller named WelcomeViewController is the first page and you have your button along with the rest of your logic in it. Every view controller has certain functions that you can override and use. For example:
viewDidLoad
viewDidAppear
viewWillAppear
viewWillDisappear
the names are self-explanatory so I'm sure you can guess what they do. In your case, just override viewDidAppear and start your animation in there.
override func viewDidAppear(animated: Bool) {
...
}

Segue following a change in criteria from a local notification in Swift

I have created a local notification in Swift which gives the option to end a current game without having to go back in to the app. That's all fine and works as it should. The issue I'm having is that if the user does this, I don't want them to go back to the Game view controller if that happens to be the last view that was open when the app entered the background. I would like them to go back to the app's Home view controller instead.
I expected to be able to add a perform segue to my Game view controller in the following way, should the criteria match. I tried adding it to viewDidAppear(), but it didn't work:
override func viewDidAppear(animated: Bool) {
if isThereACurrentGame() == false {
performSegueWithIdentifier("unwindToHomeScreen", sender: self)
}
}
Is this something to do with viewDidAppear() not being called when the app comes back to the foreground? If so, what might an alternative be?
P.S. My isThereACurrentGame() function works as it should, as does the performSegueWithIdentifier elsewhere in the view controller, so these aren't the cause of the problem.