I have recently started using Facebook SDK 3.1, and am encountering some problems with logging in using openActiveSessionWithReadPermissions.
Actually, loggin in works perfectly, if there is a cached token available, it logs in without presenting Facebook UI, and if not, it presents the UI.
The problem occurs after I make a call to reauthorizeWithPublishPermissions. If I call reauthorizeWithPublishPermissions, then close and reopen the application, and make a call to openActiveSessionWithReadPermissions, it presents the Facebook UI and requires the user to say "yes I'm OK with read permissions", even though there is a cached token available.
It only presents the Facebook UI erroneously if I make a call to reauthorizeWithPublishPermissions, otherwise everything works fine.
Open for read code:
[FBSession openActiveSessionWithReadPermissions:readpermissions allowLoginUI:YES
completionHandler:^(FBSession *aSession, FBSessionState status, NSError *error) {
[self sessionStateChanged:[FBSession activeSession] state:status error:error];
if (status != FBSessionStateOpenTokenExtended) {
// and here we make sure to update our UX according to the new session state
FBRequest *me = [[FBRequest alloc] initWithSession:aSession
graphPath:#"me"];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *aUser,
NSError *error) {
self.user = aUser;
aCompletionBlock(aSession, status, error);
}];
}
}];
the sessionStateChanged function:
- (void)sessionStateChanged:(FBSession *)aSession state:(FBSessionState)state error:(NSError *)error {
if (aSession.isOpen) {
// Initiate a Facebook instance and properties
if (nil == self.facebook || state == FBSessionStateOpenTokenExtended) {
self.facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
self.facebook.accessToken = FBSession.activeSession.accessToken;
self.facebook.expirationDate = FBSession.activeSession.expirationDate;
}
} else {
// Clear out the Facebook instance
if (state == FBSessionStateClosedLoginFailed) {
[FBSession.activeSession closeAndClearTokenInformation];
}
self.facebook = nil;
}
}
the Publish call, with an empty aPublishAction for testing:
- (void)doPublishAction:(void(^)(FBSession *aSession, NSError *error))aPublishAction {
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
NSArray *writepermissions = [[NSArray alloc] initWithObjects:
#"publish_stream",
#"publish_actions",
nil];
[[FBSession activeSession]reauthorizeWithPublishPermissions:writepermissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *aSession, NSError *error){
if (error) {
NSLog(#"Error on public permissions: %#", error);
}
else {
aPublishAction(aSession, error);
}
}];
}
else {
// If permissions present, publish the story
aPublishAction(FBSession.activeSession, nil);
}
}
Thanks for everything in advance, I would be grateful for any and all help!!
You need to add
[FBSession.activeSession handleDidBecomeActive];
to your -(void) applicationDidBecomeActive:(UIApplication *)application method in your app's delegate as stated in the migration guide.
What if you use openActiveSessionWithPermissions instead of openActiveSessionWithReadPermissions?
Related
Situation
I am trying to change an Objective-C project from Parse to firebase. What I am trying to do is replacing Parse codes with corresponding firebase code. This strategy worked for my login page and login was successful
The login page calls a segue to MainViewController. The MainViewController loads but the app crashes with an error
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: (null)'
What I Did
This is the firebase code I added in LoginViewController - (IBAction)loginButtonClicked:(id)sender
//Checking for username and password
_emailId=_emailTextField.text;
_password=_passwordTextField.text;
//reference for firebase
self.ref = [[FIRDatabase database] reference];
if([self isInternet])
{
//firebase authentication
[[FIRAuth auth]signInWithEmail:_emailId password:_password completion:^(FIRUser *user, NSError *error) {
// ...
if (error) {
//login failed
[self failsLoginWithError:#"Please try again later"];
}
else
{
NSLog(#"Login Successful");
[self successLoginWithResposne:user];
}
and this is the parse code I commented out
PFQuery *query=[PFQuery queryWithClassName:#"normal"];
[query whereKey:#"usermail" equalTo:_emailId];
[query whereKey:#"password" equalTo:_password];
[query whereKey:#"user_removed" equalTo:#(0)];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
//login failed
[self failsLoginWithError:#"Please try again later"];
}
if ([objects count]==0) {
NSLog(#"Invalid username or passowrd");
[self failsLoginWithError:#"Invalid email or password"];
} else {
PFObject *obj = [objects firstObject];
ud = [NSUserDefaults standardUserDefaults];
BOOL isBlocked = [[obj valueForKey:#"blocked"] boolValue];
BOOL isLogged = [[obj valueForKey:#"logged"]boolValue];
NSString *userID = [ud objectForKey:#"ObjectID"];
if (isLogged) {
if ([userID isEqualToString:obj.objectId]) {
if (isBlocked) {
[self failsLoginWithError:#"Login Blocked! Please contact your Doctor"];
}else{
// Login success
[self successLoginWithResposne:obj];
}
}
else{
// login Failed
[obj setValue:#(1) forKey:#"blocked"];
[obj saveInBackground];
[self failsLoginWithError:#"Login Blocked! Please contact your Doctor"];
}
}
else {
// login success
[obj setValue:#(1) forKey:#"logged"];
[obj saveInBackground];
[self successLoginWithResposne:obj];
}
}
} ];
The error message says "exception due to comparison with a null value".
Guess this is due to some parse code somewhere comparing a value that is received from the LoginViewController (which I commented out).
Question
How to fix this?
How do I find the part where the comparison is happening?
Do I have to replace every parse code with firebase code before executing?
if so, How to find every parse code in the project?
Crash report:
Exact line where code crashes:
You may look for the User system parse has build in
Example
[PFUser logInWithUsernameInBackground:_emailId password:_password
block:^(PFUser *user, NSError *error) {
if (user) {
if(user[#"blocked"] == #YES){
return [PFUser logOut];
}else{
// USER AUTHENTICATED
}
} else {
// Password, Conection
}
}];
All local store will be available by using this anywhare in your app:
PFUser *currentUser = [PFUser currentUser];
if (currentUser) {
// do stuff with the user
} else {
// show the signup or login screen
}
If you want to auth with the email insted of a "username" just use the username as the email in registration.
I can login with "[[Twitter sharedInstance] logInWithCompletion..."
I can showWithCompletion too.
But,I can't TWTRComposer showWithCompletion after login completion.
not Crash, but Composer dialog not show.And show error
This is my Code
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession * _Nullable session, NSError * _Nullable error) {
//
TWTRComposer *composer = [[TWTRComposer alloc] init];
NSString* composerText = #"Text";
[composer setText:composerText];
[composer setImage:[UIImage imageNamed:#"image.png"]];
[composer showFromViewController:self completion:^(TWTRComposerResult result) {
//
if (result == TWTRComposerResultDone) {
NSLog(#"Post Tweet successfully");
}
else {
NSLog(#"Unable to post Tweet");
}
}];
}];
Error:
CoreText performance note: Client called CTFontCreateWithName() using name "STHeitiTC-Medium" and got font with PostScript name "PingFangTC-Medium". For best performance, only use PostScript names when calling this API.
Thx~
When I use the webdialog for a friendrequest, everything is going fine, except no request or anything is made.
The code:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
facebookFriend.id, #"to",
nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:FBSession.activeSession
message:NSLocalizedString(#"FB_FRIEND_INVITE_MESSAGE", #"Facebook friend invite message")
title:NSLocalizedString(#"FB_FRIEND_INVITE_TITLE", #"Facebook friend invite title")
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
}];
This is the result I get:
fbconnect://success?request=xxxxxxxxxxxx&to%5B0%5D=xxxxxxxx
How can I debug what is going wrong?
Thanks in advance.
Ruud
For SDK 3.2 or above we have a facility to use FBWebDialogs class that will help us to show a popup along with the friend list and pick one or more from list to send invitation.
Lets do it step by step:
1) Download and setup SDK 3.2 or above.
2) First setup your application on facebook by following this url.
3) Then use the attached code.
Sample Code: (It generates invite friend request)
-(void)inviteFriends
{
if ([[FBSession activeSession] isOpen])
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:[self getInviteFriendMessage]
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error)
{
[self requestFailedWithError:error];
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
[self requestFailedWithError:nil];
}
else if([[resultURL description] hasPrefix:#"fbconnect://success?request="])
{
// Facebook returns FBWebDialogResultDialogCompleted even user
// presses "Cancel" button, so we differentiate it on the basis of
// url value, since it returns "Request" when we ACTUALLY
// completes Dialog
[self requestSucceeded];
}
else
{
// User Cancelled the dialog
[self requestFailedWithError:nil];
}
}
}
];
}
else
{
/*
* open a new session with publish permission
*/
[FBSession openActiveSessionWithPublishPermissions:[NSArray arrayWithObject:#"publish_stream"]
defaultAudience:FBSessionDefaultAudienceFriends
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
{
if (!error && status == FBSessionStateOpen)
{
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:nil];
[FBWebDialogs presentRequestsDialogModallyWithSession:nil
message:[self getInviteFriendMessage]
title:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error)
{
if (error)
{
[self requestFailedWithError:error];
}
else
{
if (result == FBWebDialogResultDialogNotCompleted)
{
[self requestFailedWithError:nil];
}
else if([[resultURL description] hasPrefix:#"fbconnect://success?request="])
{
// Facebook returns FBWebDialogResultDialogCompleted even user
// presses "Cancel" button, so we differentiate it on the basis of
// url value, since it returns "Request" when we ACTUALLY
// completes Dialog
[self requestSucceeded];
}
else
{
// User Cancelled the dialog
[self requestFailedWithError:nil];
}
}
}];
}
else
{
[self requestFailedWithError:error];
}
}];
}
}
and here are the helper functions that calls delegates function OnFBSuccess and OnFBFailed.
- (void)requestSucceeded
{
NSLog(#"requestSucceeded");
id owner = [fbDelegate class];
SEL selector = NSSelectorFromString(#"OnFBSuccess");
NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
_callback = [NSInvocation invocationWithMethodSignature:sig];
[_callback setTarget:owner];
[_callback setSelector:selector];
[_callback retain];
[_callback invokeWithTarget:fbDelegate];
}
- (void)requestFailedWithError:(NSError *)error
{
NSLog(#"requestFailed");
id owner = [fbDelegate class];
SEL selector = NSSelectorFromString(#"OnFBFailed:");
NSMethodSignature *sig = [owner instanceMethodSignatureForSelector:selector];
_callback = [NSInvocation invocationWithMethodSignature:sig];
[_callback setTarget:owner];
[_callback setSelector:selector];
[_callback setArgument:&error atIndex:2];
[_callback retain];
[_callback invokeWithTarget:fbDelegate];
}
So the class taht calls method InviteFriend MUST have these functions:
-(void)OnFBSuccess
{
CCLOG(#"successful");
// do stuff here
[login release];
}
-(void)OnFBFailed:(NSError *)error
{
if(error == nil)
CCLOG(#"user cancelled");
else
CCLOG(#"failed");
// do stuff here
[login release];
}
Recommended Reads:
Send Invitation via Facebook
API Permissions
An Example
NOTE:
1) Don't forget to setup Facebook application ID in plist.
2) Don't forget to adjust AppDelegate to handle urls.
Partial snippet taken from above link in point 2:
/*
* If we have a valid session at the time of openURL call, we handle
* Facebook transitions by passing the url argument to handleOpenURL
*/
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBSession.activeSession handleOpenURL:url];
}
Hope it helps!
EDIT
Here:
declaration for fbDelegate is:
#property (nonatomic, assign) id <FBLoginDelegate> fbDelegate;
#protocol FBLoginDelegate <NSObject>
#required
-(void) OnFBSuccess;
-(void) OnFBFailed : (NSError *)error;
#end
and this is how you can consume this code:
FBLoginHandler *login = [[FBLoginHandler alloc] initWithDelegate:self]; // here 'self' is the fbDelegate you have asked about
[login inviteFriends];
I think your application is not enable for Android and for web . And you are trying to get notification on web or on Android device.
Points : For getting notification on Android or on web you have to enable your app for Android and web too .
To Enable Android and Web on your App : GoTo your App > Setting > Click on + Add platform add enter necessary information and Save .
Lets Enjoy Notification . :-)
I'm opening the facebook session with the email permission like so:
- (void)facebookOpenSession {
NSArray *permissions = #[#"email"];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
And then the important snippet of session state changed looks like:
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error {
switch (state) {
case FBSessionStateOpen: {
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (error) {
//error
} else {
self.myFirstNameLabel.text = user.first_name;
self.myLastNameLabel.text = user.last_name;
// self.myEmailLabel.text = #"";
}
}];
}
...
How do I actually extract the user's email? Is it in one of the provided variables, or do I have to make another call within the completion handler?
Thanks in advance!
FBGraphUser doesn't have the property (i'm assuming because its optional/only available if you asked for the email permission) but you should still be able to access it from the dictionary like this:
[user objectForKey:#"email"]
You should check before yo ask the right permissions
loginView.readPermissions = #[#"public_profile", #"email"];
Currently playing around with GooglePlusSample with scope:
#"https://www.googleapis.com/auth/plus.me",
#"https://www.googleapis.com/auth/userinfo.email" and
#"https://www.googleapis.com/auth/userinfo.profile".
Tried calling auth.userEmail, auth.userData in callback method finishedWithAuth:error:, but both are empty...
-(void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error{
NSLog(#"Received Error %# and auth object==%#",error,auth);
if (error) {
// Do some error handling here.
} else {
[self refreshInterfaceBasedOnSignIn];
NSLog(#"email %# ",[NSString stringWithFormat:#"Email: %#",[GPPSignIn sharedInstance].authentication.userEmail]);
NSLog(#"Received error %# and auth object %#",error, auth);
// 1. Create a |GTLServicePlus| instance to send a request to Google+.
GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
plusService.retryEnabled = YES;
// 2. Set a valid |GTMOAuth2Authentication| object as the authorizer.
[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:#"me"];
// *4. Use the "v1" version of the Google+ API.*
plusService.apiVersion = #"v1";
[plusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLPlusPerson *person,
NSError *error) {
if (error) {
//Handle Error
} else
{
NSLog(#"Email= %#",[GPPSignIn sharedInstance].authentication.userEmail);
NSLog(#"GoogleID=%#",person.identifier);
NSLog(#"User Name=%#",[person.name.givenName stringByAppendingFormat:#" %#",person.name.familyName]);
NSLog(#"Gender=%#",person.gender);
}
}];
}
}
Once user is authenticated you can call [[GPPSignIn sharedInstance] userEmail] to get authenticated user's email.
This worked for me :
Firstly use the userinfo.email scope as per :
signInButton.scope = [NSArray arrayWithObjects:
kGTLAuthScopePlusMe,
kGTLAuthScopePlusUserinfoEmail,
nil];
Then define these methods :
- (GTLServicePlus *)plusService {
static GTLServicePlus* service = nil;
if (!service) {
service = [[GTLServicePlus alloc] init];
// Have the service object set tickets to retry temporary error conditions
// automatically
service.retryEnabled = YES;
// Have the service object set tickets to automatically fetch additional
// pages of feeds when the feed's maxResult value is less than the number
// of items in the feed
service.shouldFetchNextPages = YES;
}
return service;
}
- (void)fetchUserProfile {
// Make a batch for fetching both the user's profile and the activity feed
GTLQueryPlus *profileQuery = [GTLQueryPlus queryForPeopleGetWithUserId:#"me"];
profileQuery.fields = #"id,emails,image,name,displayName";
profileQuery.completionBlock = ^(GTLServiceTicket *ticket, id object, NSError *error) {
if (error == nil) {
// Get the user profile
GTLPlusPerson *userProfile = object;
// Get what we want
NSArray * userEmails = userProfile.emails;
NSString * email = ((GTLPlusPersonEmailsItem *)[userEmails objectAtIndex:0]).value;
NSString * name = userProfile.displayName;
NSString * profileId = userProfile.identifier;
} else {
// Log the error
NSLog(#"Error : %#", [error localizedDescription]);
}
};
GTLBatchQuery *batchQuery = [GTLBatchQuery batchQuery];
[batchQuery addQuery:profileQuery];
GTLServicePlus *service = self.plusService;
self.profileTicket = [service executeQuery:batchQuery
completionHandler:^(GTLServiceTicket *ticket,
id result, NSError *error) {
self.profileTicket = nil;
// Update profile
}];
}
And finally call the "fetchUserProfile" method in the "finishedWithAuth" as per :
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error
{
// An error?
if (error != nil) {
// Log
} else {
// Set auth into the app delegate
myAppDelegate *appDelegate = (myAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.auth = auth;
// Get user profile
self.plusService.authorizer = auth;
[self fetchUserProfile];
}
}
Note this may not be perfect as it's a 'work in progress', in particular re: getting the correct email address when the user has more than one but it's a start!
Good luck.
Steve
If you have Access not configured error check services in google api console. make sure you enable google plus api services.