I asked this question in the Parse google group, but figured I'd open it up wider here on SO. I'm still a little fuzzy around the rules surrounding discussing anything related to iOS8 prior to the official release, but with GM out and extensions discussed, I figure I can ask this question now. If not, it can wait.
I'm using Parse for the backend. I'd like to build a Today widget that displays a few records related to a specific currentUser. Loading and initializing Parse isn't the issue, framework is loaded and initialized, that is fine. But even when logged into the container app, it appears I have no reference to the currentUser.
This is the code I created just to confirm I could get access to the current user, which does not work. I attempted to save it in NSUserDefault, which is probably a hack anyway, but was unsuccessful.
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
nameTF.text = [NSString stringWithFormat:@"%@ %@", [currentUser objectForKey:@"firstName"], [currentUser objectForKey:@"lastName"]];
} else {
nameTF.text = @"No freaking clue who you are.";
}
But all I see is the "no freaking clue ..." text.
My first question is whether or not I can access the current user? I assume I can and need to somehow get it in the group and I'm missing something silly. And if so, can someone point me to a source or information on the best way to accomplish this? Once I can make queries with the current User I'll be fine.
Thanks again for any suggestions you can offer.
* UPDATE *
After a suggestion from Fosco Marotto at Parse to store the sessionToken in NSUserDefaults and then use that token within the extension to call PFUser becomeInBackground, I put the following code together to test the suggestion and this works. It needs to be improved on because it is a bit hackish, so I'm not marking it as the answer.
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"XXXXXXXXXXXXXXXXX"];
NSString *pfToken = [sharedDefaults valueForKey:@"PFUserSessionToken"];
[PFUser becomeInBackground:pfToken block:^(PFUser *currentUser, NSError *error) {
if (error) {
// The token could not be validated.
NSLog(@"error happened during becomeInBackground - token could not be validated");
// Show the token just to make sure we got it
nameTF.text = pfToken;
} else {
// The current user is now set to user.
// do stuff with the user
nameTF.text = [NSString stringWithFormat:@"%@ %@", [currentUser objectForKey:@"firstName"], [currentUser objectForKey:@"lastName"]];
}
}];
// Now lets see if we can get PFUser outside of the block
// Not sure if we need this
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// We have a reference to currentUser
nameTF.text = [NSString stringWithFormat:@"%@", [currentUser objectForKey:@"firstName"]];
} else {
nameTF.text = @"Don't think you're getting the PFUser thing right here";
}
I can get the currentUser and query against it. Thanks to Fosco for the direction and hopefully this is getting closer to a solution