Accessing the current PFUser from the iOS8 Today widget
I asked this question in the Google Parse group, but figured I'd open it up more broadly here. I'm still a little vague around the rules related to discussing anything related to iOS8 prior to the official release, but with discussions on GM and extensions, I suppose I can ask this question now. If not, he can wait.
I am using Parse for the backend. I would like to create a Today widget that displays multiple records associated with a specific currentUser. Loading and initializing Parse is not a problem, the framework is loaded and initialized, that's ok. But even when logging into the container app, it seems like I don't have a reference to currentUser.
This is the code I created to confirm that I can access the current user, which is not working. I tried storing it in NSUserDefault, which is probably a hack anyway, but with no success.
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 I only see the text "no freaking clue ...".
My first question is, can I access the current user? I guess I can and should get it in the group somehow, and I am missing something stupid. And if so, can someone point me to the source or information on how to do this? Once I can make requests with the current user, everything will be fine.
Thanks again for any suggestions you might have.
* UPDATE *
After Fosco Marotto's suggestion in Parse to store the sessionToken in NSUserDefaults and then use that token in the extension to call PFUser to become InBackground, I put the following code together to test the suggestion and it works. It needs to be improved because it is a bit hacky, so I am not marking it as an 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 currentUser and query against it. Thanks to Fosco for the direction and hopefully this is getting closer to a solution
source to share
Parse now offers an official solution for exchanging data between host application and extension. More information can be found at https://www.parse.com/docs/ios/guide#extensions
source to share