Parse set applicationId quitting - ios

I have an app that opens up right away to a login/register page. Everytime I run it, I get an error. The exception breakpoint shows that the error is in the viewDidLoadstatement. Here it is:
override func viewDidLoad() {
super.viewDidLoad()
Parse.setApplicationId("EVH469wqjtGjbWVATySdePvjaETquQDImcYaqUjc", clientKey: "4H9sMitCWq1AaaSvFSJ3EluUOwcI3OteYwMjrlxV")
var currentUser = PFUser.currentUser()
if currentUser != nil
{
self.performSegueWithIdentifier("homepage", sender: AnyObject?())
}
else
{
}
}
The breakpoint highlights the Parse.setApplicationId line. I cannot figure this out for the life of me. Any help is greatly appreciated!
UPDATE: It only crashes on my phone, not in the simulator... which is even weirder

Related

Firebase uid returning nil after authentication (Swift)

In my app, as soon as it opens I check to see if the user is already authenticated in the viewdidload of the initial view. If they are already authenticated, I perform a segue to the main view. I'm even printing the uid to the log at this time and it's printing correctly.
override func viewDidLoad() {
super.viewDidLoad()
if ref.authData != nil {
let uid = ref.authData.uid
print(uid)
I then do the same later in the app to get some of the user's info when they click on their profile settings. I write the exact same code to fetch their uid, but this time the uid is returning nil and is crashing with the error
"fatal error: unexpectedly found nil while unwrapping an Optional value"
Is this a firebase or simulator issue?
Edit: This issue has only occurred twice. Otherwise, the code itself works as intended, which makes me wonder whether it is a firebase or simulator issue.
You want to use the observeAuthEventWithBlock method, which is a realtime authentication listener.
override func viewDidAppear() {
let ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com")
ref.observeAuthEventWithBlock({ authData in
if authData != nil {
// user authenticated
print(authData)
self.performSegueWithIdentifier("LoginToOtherView", sender: nil)
} else {
// No user is signed in
}
})
}
About the exact error you are encountering I am not sure, but a Swift-yer way of doing your code (and avoiding your error) would be to call:
if let uid = ref.authData.uid {
print(uid)
}
This code safely unwraps both authData and the UID.
I was having this problem and it took me hours. Then I realized that I'd just forgotten to do this:
ref = FIRDatabase.database().reference()
before
var ref: FIRDatabaseReference!

Parse User Auto Login

I'm trying to get my app to segue directly to the Profile Page after start is a PFUser is already logged in that way they won't have to keep logging in unless the completely stop the app. It's not working. Maybe I'm not checking for PFUser nil correctly. Please help. Here's what I'm putting in viewDidLoad.....
override func viewDidLoad()
{
super.viewDidLoad()
if PFUser.currentUser()?.username != nil
{
self.performSegueWithIdentifier("loginToProfileSegue", sender: self)
}
Please help.

Update cached copy of Parse currentUser() in Swift

I have an ios application, I have implemented Facebook login through Parse. Everything works great in the first trial.
I can login via Facebook
My details are stored in the PFUser.currentUser()
I am able to pull information from the Graph API
But things start to become a problem when I logout (PFUser.logOut()) , and delete the User row from the Parse class and delete the app from Facebook.
The following methods still show that the user is logged in and authenticated.
if let user = PFUser.currentUser() {
if user.isAuthenticated()
I am guessing it is because current user is still using the cached copy of the User. I try executing
do
{
try PFUser.currentUser().fetch()
}
catch{
print("User refreshed")
}
But it does not refresh the currentUser() and gives an error. How can I refresh the PFUser.currentUser(), so that it stops saying that the user is logged in and authenticated, even though it's not
Thanks
I got an answer here,
Swift & Parse - PFUser currentUser never equals nil
I've been struggling with logging out for a little while and I believe I have finally cracked it!
No matter what I did, when I used "PFUser.logOut()" would never set "PFUser.currentUser()" to nil, but it would set "PFUser.currentUser()!.username" to nil...
Because of this I used
var currentUser = PFUser.currentUser()!.username
as a global variable to track is a user is logged in.
On my login/first page I added
override func viewDidAppear(animated: Bool) {
if currentUser != nil {
self.performSegueWithIdentifier("login", sender: self)
}
}
and finally on my logout button i used
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "logout" {
PFUser.logOut() //Log user out
currentUser = PFUser.currentUser()!.username //Reset currentUser variable to nil
}
}

Parse PFUser.currentUser returns nil - Swift

I am logging in my users using Parse. As my app opens my LaunchVieWController determines whether users are already signed in or not:
override func viewDidLoad() {
super.viewDidLoad()
//Make sure cached users don't have to log in every time they open the app
var currentUser = PFUser.currentUser()
println(currentUser)
if currentUser != nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("alreadySignedIn", sender: self)
}
} else {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("showSignUpIn", sender: self)
}
}
}
If users are already signed in they are taken to the table view described below. If they are not logged in, they are taken to a view in which they can go to the signup view or the login view.
My signup works perfectly fine, and signs up users and then redirects them to the table view.
Here is the signup function (it is in a separate controller from the login function):
func processSignUp() {
var userEmailAddress = emailAddress.text
var userPassword = password.text
// Ensure username is lowercase
userEmailAddress = userEmailAddress.lowercaseString
// Add email address validation
// Start activity indicator
activityIndicator.hidden = false
activityIndicator.startAnimating()
// Create the user
var user = PFUser()
user.username = userEmailAddress
user.password = userPassword
user.email = userEmailAddress
user.signUpInBackgroundWithBlock {
(succeeded: Bool, error: NSError?) -> Void in
if error == nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("signInToNavigation", sender: self)
}
} else {
self.activityIndicator.stopAnimating()
if let message: AnyObject = error!.userInfo!["error"] {
self.message.text = "\(message)"
}
}
}
}
My login function looks like so:
#IBAction func signIn(sender: AnyObject) {
var userEmailAddress = emailAddress.text
userEmailAddress = userEmailAddress.lowercaseString
var userPassword = password.text
PFUser.logInWithUsernameInBackground(userEmailAddress, password:userPassword) {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("signInToNavigation", sender: self)
}
} else {
if let message: AnyObject = error!.userInfo!["error"] {
self.message.text = "\(message)"
}
}
}
}
After users log in they are sent to a table view. In that table view I am trying to access the current user object in the following function, as I want to load different data based on who the user is:
override func queryForTable() -> PFQuery {
var query = PFQuery(className:"MyClass")
query.whereKey("userID", equalTo: PFUser.currentUser()!)
return query
}
When I try to log in with a user I get thrown the following error: "fatal error: unexpectedly found nil while unwrapping an Optional value". It seems like the PFUser.currentUser() object is nil.
When users are sent to the table view after signing up the PFUser.currentUser() object is set and works perfectly.
My guess is that this is because of the fact that the PFUser.logInWithUsernameInBackground is happening in the background and that my query is trying to get the PFUser.currentUser() object before it has been loaded. Is there anyway I can work around this issue? In the table view the value of PFUser.currentUser() is needed to load the table data. Can I somehow make sure that PFUser.currentUser() gets assigned with the current user object before the function gets called (for example, by not loading in users in the background thread)?
All help is much appreciated!
EDIT: I've updated this post with some more of my code to help highlight any bug that I may be missing.
I discovered that this problem appeared because the segue signInToNavigation was wired from the Login-button, instead from the login view controller itself. This resulted in the segue being executed before the login function was executed, and therefore the PFUser.currentUser()object was not yet assigned when the table view loaded. I solved this by rewiring the segues. Stupid slip-up on my side.
Thanks to Ryan Kreager and Wain for taking time to help me figure out this issue!
You can try checking if PFUser.currentUser() != nil instead to make sure it's being set right after login. If it's not being set right off the bat, you know there is a deeper login problem.
Also, try removing the dispatch_async(dispatch_get_main_queue()){ wrapper around your call to the segue.
It's unnecessary (logInWithUsernameInBackground already returns to the main thread) and I have a hunch that it's creating a racing condition where the local object is not being set first because Parse can't do any post-call cleanup since you're going right for the main thread.

Performing automatic segue using PFUser.currentUser() on app launch

I have an app that is using Parse to grab the locally stored currently logged in user for my app. When I launch the app from the home screen I want to avoid forcing my users to login everytime so I grab the currently logged in user using PFUser.currentuser() like this:
override func viewDidLoad() {
super.viewDidLoad()
println(PFUser.currentUser())
if PFUser.currentUser()?.username != nil {
self.performSegueWithIdentifier("LoggedIn", sender: self)
}
}
I then just want to perform a segue to a "dashboard" if that user has already logged in. the code
self.performSegueWithIdentifier("LoggedIn", sender: self)
works beautifully everywhere else in my code. However, when I try to use it in ViewDidLoad() is when it is giving me trouble. Can anyone help me with this?
The solution is to perform the code snippet in my question in the ViewDidAppear() func like this:
override func viewDidAppear(animated: Bool) {
println(PFUser.currentUser())
if PFUser.currentUser()?.username != nil {
self.performSegueWithIdentifier("LoggedIn", sender: self)
}
}

Resources