How to do logout with Parse Swift iOS 8 Xcode 6.4 - ios

How do I log out with Parse Swift iOS 8 Xcode 6.4?
Here's a screenshot.
I need to add the action for my button so that I can log out with Parse.
http://i.gyazo.com/905df0a5f4bf29e7dd1a2b44e0c35865.png
Please help.
Thanks,
Jamie Mathieson

After you are log out you need to present the LoginView Controller or Sign up
PFUSer.logout()
let Login = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.presentViewController(Login, animated: true, completion: nil)

PFUser.logOutInBackground(block: { (error) in
if error == nil {
// Show whatever screen you want here
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerID") as! Wizard
self.present(loginVC, animated: true, completion: nil)
}})

Related

How do I dismiss an ORKTaskViewController and present a view controller of my choice?

I've spent 24 hours trying to find a solution for this issue. When a user hits register on my app they will have to answer a series of survey questions (which I created using an ORKorderedtask (research kit)). Once the survey is completed I'd like the home page to be presented, however when I test the app and finish the survey it goes straight back to the register page. here's my code:
1.presenting the ordered task view controller;
let registrationTaskViewController = ORKTaskViewController(task: registrationSurvey, taskRun: nil)
registrationTaskViewController.delegate = self
self.present(registrationTaskViewController, animated: true, completion: nil)
2. Dismissing the task view controller(this doesn't work);
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
self.dismiss(animated: false) {
let home = homePageViewController()
self.present(home, animated: true, completion: nil)
}
thanks in advance.
Without knowing all the ViewControllers in your stack, I'd suggest not dismissing your register page view controller. Instead present your HomePageViewController on top of your Register screen. Just change your delegate method to this:
func taskViewController(_ taskViewController: ORKTaskViewController, didFinishWith reason: ORKTaskViewControllerFinishReason, error: Error?) {
let home = homePageViewController()
self.present(home, animated: true, completion: nil)
}
Or you could even present your HomePageViewController in the completion block after you prsent your ORKTaskViewController. The benefit of this approach would be that when the user dismisses the survey, they will see the HomePageViewController immediately:
let registrationTaskViewController = ORKTaskViewController(task: registrationSurvey, taskRun: nil)
registrationTaskViewController.delegate = self
self.present(registrationTaskViewController, animated: true, completion: {
let home = homePageViewController()
self.present(home, animated: true, completion: nil)
})
A couple more points:
• Classes should begin with capital letters (i.e. HomePageViewController). It's a convention that every single experienced developer uses, and Apple even recommends.
• Ultimately, I would suggest using a Navigation Controller to handle these transitions. With Nav Controllers you can achieve a much better 'flow' using push segues. It just feels better.

How can i send different users to separate view controllers using firebase and Xcode

I am fairly new to coding and have started using firebase as a back end server for an application i am creating in Xcode using swift.
The app itself will have one login page but 3 separate types of users. The admin will have different permissions to the other 2 users.
The code I currently have is:
FIRAuth.auth()?.signIn(withEmail: username!, password: password!, completion: { (user, error) in
if error == nil {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "AdminVC")
self.present(vc!, animated: true, completion: nil)
}
The code is getting the email and password for the authentication page. But because of the 3 different types of users I don't want them all going to the 'AdminVC' view controller.
Is there a way of getting the 2 other users to go to their own view controllers using this authentication method?
If you want to store a type for a user you have to use the database. Like this
When the user logs in, get the value from the database for the path "users/<userId>/type". Then use a switch statement to redirect to the correct view controller.
Here's the full code
// Sign in to Firebase
FIRAuth.auth()?.signIn(withEmail: "ntoonio#gmail.com", password: "Password123", completion: {
(user, error) in
// If there's no errors
if error == nil {
// Get the type from the database. It's path is users/<userId>/type.
// Notice "observeSingleEvent", so we don't register for getting an update every time it changes.
FIRDatabase.database().reference().child("users/\(user!.uid)/type").observeSingleEvent(of: .value, with: {
(snapshot) in
switch snapshot.value as! String {
// If our user is admin...
case "admin":
// ...redirect to the admin page
let vc = self.storyboard?.instantiateViewController(withIdentifier: "adminVC")
self.present(vc!, animated: true, completion: nil)
// If out user is a regular user...
case "user":
// ...redirect to the user page
let vc = self.storyboard?.instantiateViewController(withIdentifier: "userVC")
self.present(vc!, animated: true, completion: nil)
// If the type wasn't found...
default:
// ...print an error
print("Error: Couldn't find type for user \(user!.uid)")
}
})
}
})
Instead of the whole switch statement you can do
let vc = self.storyboard?.instantiateViewController(withIdentifier: "\(snapshot.value)_View")
self.present(vc!, animated: true, completion: nil)
Warning! This will crash if the type isn't found. But that's fixable :)

Why am I getting a black screen when I use self.presentViewController(vc, animated: true, completion: nil) Xcode swift 2

So I'm try to set up a login func (which worked) but when I test the login, I get to the login screen and login in. Then It shows a black screen.
func login(){
FIRAuth.auth()?.signInWithEmail(Username.text!, password: Password.text!, completion: {
user, error in
if error != nil{
print("Incorrect Username/Password")
}
else{
print("Welcome")
self.presentViewController(vc, animated: true, completion: nil)
}
})
}
Can anybody tell me what I am doing wrong.
The signInWithEmail part doesn't affect the UIview.
Storyboard name I'm new to this but the following answer worked for me
let NextView = self.storyboard?.instantiateViewControllerWithIdentifier("xyzVC") as! xyzVC
self.presentViewController(NextView, animated: true, completion: nil)
used the following answer
https://stackoverflow.com/a/30248306/6679536
again, I'm new, so maybe this will create issues in the future!
make sure you call the ViewController in storyboard the exact same name, as shown in the picture... otherwise you will get a huge error.

Clean application memory in swift

I've an XCode project programmed in swift with a registration/connection process. When you're connected, even if the app restarted you don't repeat this process until you click on "log out" button.
I've noticed that if you follow the registration/connection process when you start the app, memory is about 500Mb whereas if the app started with a connected user, memory is about 140Mb.
Registration process is composed of 3 view controllers connected with custom segue. Access to logged in view is made by following code :
func switchRootViewController(rootViewController: UIViewController, animated: Bool, completion: (() -> Void)?)
{
if animated
{
UIView.transitionWithView(window!, duration: 0.5, options: .TransitionCrossDissolve, animations:
{
let oldState: Bool = UIView.areAnimationsEnabled()
UIView.setAnimationsEnabled(false)
self.window!.rootViewController = rootViewController
UIView.setAnimationsEnabled(oldState)
}, completion: { (finished: Bool) -> () in
if (completion != nil)
{
completion!()
}
})
}
else
{
window!.rootViewController = rootViewController
}
}
Where rootViewController is replace by the logged in view I want to load as you can see here :
func goToNextView()
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController = storyboard.instantiateViewControllerWithIdentifier("conversationViewController") as!ConversationViewController
homeViewController.currentUser = self.currentUser
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.switchRootViewController(home, animated: true, completion: nil)
}
However it doesn't clean the memory.
Here a screen of memory allocation :
So my question is : is it possible to "kill" some view controller in order to clean the memory ?
Here for example it can be obvious to "kill" registration process views when you're connected.
Thank you for answers.

Tab bar items image doesn't appear after presentview

I coded my application with Swift 2. When i was trying to create my application, i used this code after user login successfully
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("home") as! UITabBarController
self.presentViewController(vc, animated: true, completion: nil)
vc is a UITabBarController which has 3 UIViewController. After TabBar appears only selected TabBar's item's image appears. All the other images appears after 10 seconds or if i touch any of them it appears. If I set TabBarController as an initial view images appears successfully.
I checked the RAM status and application uses 14.5 mb on iPhone 6 plus so i don't think happens because of memory leak.
I searched everywhere but i couldn't find any problem same as mine.
SOLVED:
I was using Touch ID in my code and this presentviewcontroller code was in completion. Raywenderlich handled completion with treads like this.
authContext.evaluatePolicy(LAPolicy.DeviceOwnerAuthenticationWithBiometrics, localizedReason: "Testing Touch ID", reply: { (complete, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if complete {
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("home") as! UITabBarController
self.presentViewController(vc, animated: true, completion: nil)
}
else{
}
})
})
I change my code like this and it worked fine. I edited my answer maybe this is a Xcode bug or else.

Resources