Save Facebook account using ACAccountStore - ios

I'm trying to use iOS Social.framework to share to Facebook. However, most users will not have Facebook setup in Settings>Facebook so therefore Facebook doesn't show up when you bring up a UIActivityViewController, it just isn't listed. (See UIActivity with no settings for Facebook or just google "facebook doesn't show in UIActivityViewController," there's plenty of people trying to solve this problem.)
Our app already uses the Facebook SDK so I can get at the Facebook account info, so I thought I would just get the info and then save the info in Settings>Facebook. Here's my code. (Which I got from When do we use saveAccount:(ACAccount *)account of ACAccountStore class? and iOS requestAccessToAccountsWithType is Not Showing Permission Prompt / NSAlert)
- (void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret {
if (self.accountStore == nil) {
self.accountStore = [[ACAccountStore alloc] init];
}
//We start creating an account by creating the credentials object
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
ACAccountType *acctType =[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:acctType];
//Here we assign credentials and now can save account
newAccount.credential = credential;
NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
[VVEnvironment stringForKey:kVVEnvironmentKeyFacebookAppID], (NSString *)ACFacebookAppIdKey,
[NSArray arrayWithObject:#"basic_info"], (NSString *)ACFacebookPermissionsKey,
ACFacebookAudienceEveryone, (NSString *)ACFacebookAudienceKey,
nil];
[_accountStore requestAccessToAccountsWithType:acctType options:options completion:^(BOOL granted, NSError *error) {
if (granted == YES) {
[self.accountStore saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"the account was saved!");
}
else {
if ([error code] == ACErrorPermissionDenied) {
NSLog(#"Got a ACErrorPermissionDenied, the account was not saved!");
}
NSLog(#"the account was not saved! %d, %#", [error code], error);
}
}];
}
else {
NSLog(#"ERR: %# ",error);
}
}];
}
I added the call to requestAccessToAccountsWithType there because without I was getting a ACErrorPermissionDenied back from saveAccount. When I run this I do see my app slide briefly aside for the Facebook dialog (if I delete my app on Facebook's website it does bring up the full dialog asking me for permissions which I grant.)
The problem is that granted is always NO, so I never get a chance to save the account. What am I missing here?
I am running this on a real device that has the Facebook App installed and setup.
I'd appreciate any help on this, thanks!

There is no solution available for this. iOS native facebook applications are not allowed to write or save account to ACAccountStore. It only allow to access account from the ACAccountStore. iOS only allow to store 1 facebook account at a time. Try to do these steps you will understand this easily,
Add a facebook account to settings App and try to get permission to access it. It will grand permission.
Remove account and try to get the permission it will deny access.
Add a facebook account and get access and try to save new account to ACAccountStore, It will say that Native applications are not allowed to save account to ACAccountStore.
The proposed solution is, Add the account to settings App and login with it and Share using Social framework or presentOSIntegratedDialog.

Related

"Error occurred" facebook login in ios application

i have integrate the facebook sdk for post something on facebook.Its working for only single facebook-id which is used for integrating facebook it in my application, not working for other.
this is my code
-(IBAction)postOnFacebook:(id)sender {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:#"Sharing Tutorial", #"name",#"Build great social apps and get more installs.", #"caption", #"Allow your users to share stories on Facebook from your app using the iOS SDK.", #"description",#"https://developers.facebook.com/docs/ios/share/", #"link",#"http://i.imgur.com/g3Qc1HN.png", #"picture",nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"Error publishing story: %#", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// Handle the publish feed callback
NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
if (![urlParams valueForKey:#"post_id"]) {
// User cancelled.
NSLog(#"User cancelled.");
} else {
// User clicked the Share button
NSString *result = [NSString stringWithFormat: #"Posted story, id: %#", [urlParams valueForKey:#"post_id"]];
NSLog(#"result %#", result);
}
}
}
}];
}
// A function for parsing URL parameters returned by the Feed Dialog.
- (NSDictionary*)parseURLParams:(NSString *)query {
NSArray *pairs = [query componentsSeparatedByString:#"&"];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:#"="];
NSString *val =
[kv[1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
params[kv[0]] = val;
}
return params;
}
this meth
Error come when i use another id for login
You are using the publish_action permission which can be made availaible to all users only after approval from facebook.
Currently you might have created the facebook app in your account and with that account id you will be able to post to facebook. This is because of the reason that facebook gives all the permissions to users who are added as Developers/Testers in the App Dashboard.
You can add people to your app as Testers/Developers by following the steps as described below
You'll need to be an admin to give someone a role on your Page. If the person is your Facebook friend:
Go to your Page and tap More.
Tap Edit Settings > Page Roles.
Tap Add Person to Page. You may need to enter your password to continue.
Begin typing a friend's name and tap their name from the list that appears.
Tap to choose a role > Add.
If the person isn't your Facebook friend, you can log into Facebook from a computer and add them by entering their email address.
Depending on their settings, the person may receive a notification or an email when you give them a role.
These users who are added will have all the permission that you are requesting.
If you want the permissions to be availaible to everyone, your facebook app should be approved and for that you have to submit your app to facebook for review. The guidelines for facebook app submision can be found from the following link
https://developers.facebook.com/docs/apps/review
Hope this helps you
I'm not sure if I understood your question.
But, for what I'm seeing. If you have created app at facebook (to integrate with your iOS app) you have to use the facebook app ID only, presented at Facebook.
Or, if this error is because you are trying to login with another facebook account. This is happening probably because you did not publish your app yet (facebook app). So it's not available to everyone, but its ok because you probably don't want to make it available right now. So you just need to go to the left menu at Roles in your facebook app page and add test users (the account that you're trying to login) or add as administrator.

IOS: what is native "Facebook Login SDK for iOS"?

I am working on ios application where I have to implement facebook publish/share feature. what I have done for sharing is
1: if user is not logged in, I am checking with this
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded||FBSession.activeSession.state == FBSessionStateOpen) {
//NSLog(#"Open");
//Set already opened session to FBSession
[FBSession setActiveSession:appDelegate.session];
//if user logged in already, perform sharing on fb
[self sharePostOnFb];
}else{
//if user is not logged in
[self openFbLogin];
}
-(void)openFbLogin
{
[FBSession openActiveSessionWithReadPermissions:#[#"public_profile", #"email",#"publish_actions"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
// Retrieve the app delegate
//AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
// Call the app delegate's sessionStateChanged:state:error method to handle session state changes
//[appDelegate sessionStateChanged:session state:state error:error];
if (state == FBSessionStateOpen) {
NSLog(#"opened");
[self sharePostOnFb];
}
}];
}
-(void)sharePostOnFb
{
NSString *strBznsName = [[[Global sharedInstance]getBznsInfo] objectForKey:#"name"];
NSString *strUserReceive = [NSString stringWithFormat:#"%# %# received %#.",[[[Global sharedInstance]getUserData] objectForKey:#"firstname"],[[[Global sharedInstance]getUserData] objectForKey:#"lastname"],dataStampDeal.strDealTerms];
NSString *urlLogoBkString= #URL_BZNSImgPath;
NSURL *urlLogoBkImage = [NSURL URLWithString:[NSString stringWithFormat:#"%#/%#",urlLogoBkString,[[[Global sharedInstance]getBznsInfo] objectForKey:#"logo" ]]];
NSString *StrUrl = [NSString stringWithFormat:#"%#",urlLogoBkImage];
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
strBznsName, #"name",
#"Powered By Anyloyalty", #"caption",
strUserReceive, #"description",
StrUrl, #"picture",
nil];
// Make the request
[FBRequestConnection startWithGraphPath:#"/me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
// Link posted successfully to Facebook
NSLog(#"result: %#", result);
strFbPostId = [result valueForKey:#"id"];
NSLog(#"post id: %#", strFbPostId);
//[self WebServiceHandler:NO];
//[appDelegate startanimation];
} else {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(#"%#", error.description );
NSDictionary *errorInformation = [[[[error userInfo] objectForKey:#"com.facebook.sdk:ParsedJSONResponseKey"]
objectForKey:#"body"]
objectForKey:#"error"];
//NSLog(#"%#", errorInformation );
//__block NSString *alertText = #"Your action will not be published to Facebook.";
__block NSString *alertText = [errorInformation valueForKey:#"message"];
[[[UIAlertView alloc] initWithTitle:#"error"
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil] show];
}
}];
}
facebook login (-(void)openFbLogin()) is opening web dialog. After successfully logged in. I am sharing post ( -(void)sharePostOnFb()) on fb and I am getting error
"(#200) The user hasn't authorized the application to perform this action"
For publishing/sharing I have to get approval for "publish_actions" permission from FB. I have submitted my application to FB app developer center. I am getting this message.
"Your app must not use a Facebook Web Dialog. Utilize our native Facebook Login SDK for iOS, so that users do not need to login twice."
so Now I want to "what is native Facebook Login SDK for iOS"? and how can I integrate that with my app?
The native Facebook login is nothing but using the iOS FB framework and create login using that. It is clearly mentioned on the developers.facebook.com how to login using there framework. You can also see the samples provided by them for login.
What this actually does is that , it check if the user has a Facebook application installed in the device and is user logged in to the Facebook app. If user is already logged in then user won't be asked to log in again. If user isn't logged in then they will be redirected to safari where they will have to log in to FB.
Its quite simple to integrate. Its well explained.
Facebook clearly says,
All iOS and Android apps should use their SDK's for iOS and Android for requesting permissions. Other apps should use their JavaScript for requesting permissions wherever possible.
Native applications on iOS and Android especially should use their SDK flows as they are optimized and work robustly across various types of devices without any manual adjustment.
In particular, their native SDKs let people who are already logged in to their Facebook app grant permissions without having to log in to their Facebook accounts again. This will result in far better conversion than not using our SDK and rendering our login dialogs inside web views embedded within your native app since the web view will not have any Facebook session data initially and will require people to login to their Facebook account.
Please note: if you are using Apple's Social framework, you may continue to do so, but ensure that if the user is not using the iOS integration, that your app falls back to the Facebook SDK for iOS. This can create more work than necessary, though, so we highly recommend using their SDK instead.
Here are the SDK implementation details:Facebook Login SDK for iOS

Multiple Prompts for iOS Facebook Account Access

I am attempting to add facebook integration into a project. In iOS 6+, I request access to the user's facebook accounts with the following code.
if (self.accountStore == nil) self.accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeFacebook = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[self.accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options
completion:^(BOOL granted, NSError *error) {
if(granted) {
self.accountsArray = [self.accountStore accountsWithAccountType:accountTypeFacebook];
dispatch_sync(dispatch_get_main_queue(), ^{
[self updateTable:self.accountsArray];
});
} else {
NSLog(#"error: %#",error);
}
}];
If the user selects "cancel" in the modal popup that prompts for access to Facebook accounts, is it possible to prompt the user again the next time the app needs this access?
Currently, if the user first selects "cancel", the user must then go into the Settings app and manually change the permission to use facebook within the app. I am not attempting to change this permission directly, I just want to prompt the user for access multiple times.

When do we use saveAccount:(ACAccount *)account of ACAccountStore class?

Please clarify me when do we use this method ?
- (void)saveAccount:(ACAccount *)account withCompletionHandler:(ACAccountStoreSaveCompletionHandler)completionHandler
As I know we get access to the account through OAuth and we do not get user's credentials. So how do we create an account? I've found that ACAccount has the only one creation method:
- (id)initWithAccountType:(ACAccountType *)type
What actually happens when we create account this way? And can we save it now ?
Ok, finally I've found the information about it.
Consider this scenario:
Our app have already been authorized by the user and we've got both access token and secret. Now we want to support new iOS 6 features and create twitter (for example) account in Settings. To do this we need to migrate these tokens to the central account store.
Here's how:
- (void)storeAccountWithAccessToken:(NSString *)token secret:(NSString *)secret {
//We start creating an account by creating the credentials object
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
ACAccountType *twitterAcctType =[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:twitterAcctType];
//Here we assign credentials and now can save account
newAccount.credential = credential;
[self.accountStore saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"the account was saved!");
}
else {
//Handle error here
}
}];
}
For more information about it read here how to migrate tokens

Using a Hidden Twitter Account

Not sure if any one has tried this. I want to access twitter with an account that the user of the phone doesn't have access to.
The way I have looked at it is by adding a twitter account using the OAth (token/secret)
I can add an account using the following code.
NSString *token = #"blahblahblah";
NSString *secret = #"blahblahblah";
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:token tokenSecret:secret];
ACAccountType *twitterAcctType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:twitterAcctType];
newAccount.credential = credential;
[store saveAccount:<#(ACAccount *)#> withCompletionHandler:<#^(BOOL success, NSError *error)completionHandler#>
[store saveAccount:newAccount withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"Account was saved!");
} else {
//something went wrong, check value of error
}
}];
But this code stores the user id associataed with the secret in the iphones account store. The problem being that if the phone user then uses twitter the account privilages are still there for him to use...
There doesn't seem to be a store deleteAccount method implemented
For my second attempt i tried using just the user that was created without adding it to the account store but I think the SaveAccount method is what verifies the token and secret and gets the user name...
Any tips???
Just a friendly reminder that this will most likely have your app banned from the app store, as this can easily be classified as spying on the user/device, if you can pull it off which I doubt.
Also, beware of having thousands/millions of users twitting from the same account.
Finally, there does not seem to be a way to remove the account, as you said, plus in order to use it you'd need to use requestAccessToAccountsWithType:withCompletionHandler: which would trigger a notice to the user and would make them think you were requesting access to his tweeter account, rather than yours...

Resources