Get ACAccount Facebook user full name is null - ios

i'm trying to get the user full name of the Facebook account, the app already have the permissione in the Facebook iPhone setting, some day ago this code works, but know doesn't, i can't understand why, this is the code:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount;
ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ACFacebookAppIdKey:#"mycode",ACFacebookPermissionsKey: #[#"email"]};
[accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
facebookAccount = [accounts lastObject];
NSLog(#"name: %#",[facebookAccount userFullName]);
dispatch_async(dispatch_get_main_queue(), ^{
[self.fb_name setText:[facebookAccount userFullName]];
});
}
}];
this: NSLog(#"name: %#",[facebookAccount userFullName]); is null

I believe userFullName is only available since iOS 7. Maybe you tested on iOS 7 and now you are experiencing the issue when you target iOS 6. See iOS: user's full name for an ACAccountTypeIdentifierFacebook for the iOS 6 fallback.

Related

Objective c How to retrieve Profile names of facebook and twitter from iPhone settings

I need to display the facebook and twitter profiles names in two labels
These profiles name should be getting from settings page setup
iif is there any facebook account setup in setting i have to get the user name and displaued it in my app
Same thing for twitter setup also how to achieve it.
Thanks,
Check in Settings
![enter image description here][2]
![enter image description here][3]
Using social framework, use you get details of accounts -
ACAccountStore *accountStore = [[ACAccountStore alloc] init] ;
ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if(granted)
{
NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
NSLog(#"twitterAccounts %#", twitterAccounts);
}
}];
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
//your app id is the key, i have used "Demo".
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:#"Demo", ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[accountStore requestAccessToAccountsWithType:facebookAccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *facebookAccounts = [accountStore accountsWithAccountType:facebookAccountType];
// fb accounts
NSLog(#"facebook accounts =%#", facebookAccounts);
} else {
//Error
NSLog(#"error getting permission %#",e);
}
}];
Similarly if there are more than 1 accounts configured, you can iterate through those and get the information.
EDIT - What are ACFacebookAppIdKey, ACFacebookPermissionsKey?
ACCOUNTS_EXTERN NSString * const ACFacebookAppIdKey NS_AVAILABLE(NA, 6_0); // Your Facebook App ID, as it appears on the Facebook website.
ACCOUNTS_EXTERN NSString * const ACFacebookPermissionsKey NS_AVAILABLE(NA, 6_0); // An array of of the permissions you're requesting.

Get logged in user detail from facebook [duplicate]

This question already has answers here:
Get Facebook uid from ACAccountStore in iOS?
(2 answers)
Closed 8 years ago.
Here is my code. but it got failed and not grant me to do further process. let me know what is wrong in this.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType
options: #{ACFacebookAppIdKey: #"640154745965666",
ACFacebookPermissionsKey: [NSArray arrayWithObjects:#"email", nil],
ACFacebookAudienceKey: ACFacebookAudienceFriends
}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSLog(#"granted");
}
else {
NSLog(#"fail ");
}
}];
This happens when authenticating using iOS6 Accounts API. All you have to do to get it working is: - log in to the developers.facebook.com - set your app to be an iOS Native app - type in your app's bundle ID.

Social, Accounts framework to get user info from facebook (profile image,name,etc)

This is my facebook method:
- (void)getMeFacebook{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ACFacebookAppIdKey : #"1405219416361729",
ACFacebookPermissionsKey : #[#"email", #"publish_stream"],
ACFacebookAudienceKey:ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
NSLog(#"Granted!!!");
}
else {
NSLog(#"error.localizedDescription======= %#", error.localizedDescription);
}
}];
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
NSLog(#"Break 1");
When debugging the code, it never evaluates if(granted), seems like the method requestAccessToAccountsWithType is not behaving correctly.I have already check for the facebook settings in the app but still not working.
Any idea of this issue?
After some hours of debugging and research i reach the solution, everytime you have to debug a block (callback) of a method in objective-c, its needed to add extra breaktimes inside the block.
The issue was a problem of synchronism, as the method was call in the main.m and the UIAlert requesting permission to access facebook data in the iPhone never appears. I move the call of the method to the viewDidLoad and the entire app work ok!

ACAccountStore Error 6 (ACErrorAccountNotFound) and 8

I'm attempting to get an account from ACAccountStore using the following code:
- (void) facebookLoginOnSuccess: (void (^)(void)) successBlock onError:(void(^)(NSError *error))errorBlock{
self.facebookPermissions = #[
#"offline_access",
#"publish_stream",
#"user_birthday",
#"user_location",
#"email"
];
NSDictionary *options = #{
#"ACFacebookAppIDKey": [[NSBundle mainBundle] objectForInfoDictionaryKey:#"FacebookAppID"],
#"ACFacebookAppVersionKey": #"1.0",
#"ACFacebookPermissionsKey": self.facebookPermissions,
#"ACFacebookPermissionGroupKey": #"write"
};
[self accountLoginFor:ACAccountTypeIdentifierFacebook withOptions:options OnSuccess:successBlock onError:errorBlock];
}
- (void) accountLoginFor: (NSString *) accountTypeID withOptions: (NSDictionary *) options OnSuccess: (void (^)(void)) successBlock onError:(void(^)(NSError *error))errorBlock{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:accountTypeID];
[accountStore requestAccessToAccountsWithType:accountType
options:options
completion:^(BOOL granted, NSError *error){
if (granted){
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(#"%#",accountsArray);
}
else {
NSLog(#"Error accessing account: %#", [error localizedDescription]);
}
}];
}
But I'm getting this error:
Error Domain=com.apple.accounts Code=6 "The operation couldn't be completed. (com.apple.accounts error 6.)"
And I can't find anything related, just this question. Any ideas what could be wrong?
Update
I found this on the Apple Developer Docs.
Accounts Framework
When requesting access to Facebook accounts, the only key required in your options dictionary is ACFacebookAppIdKey. ACFacebookPermissionGroupKey and ACFacebookAppVersionKey are now obsolete.
If you request a write permission under ACFacebookPermissionsKey, such as publish_stream, you must provide a value for ACFacebookAudienceKey, which can be one of ACFacebookAudienceEveryone, ACFacebookAudienceFriends, or ACFacebookAudienceOnlyMe.
So I changed my options to:
NSDictionary *options = #{
#"ACFacebookAppIDKey": [[NSBundle mainBundle] objectForInfoDictionaryKey:#"FacebookAppID"],
#"ACFacebookPermissionsKey": self.facebookPermissions,
#"ACFacebookAudienceKey": ACFacebookAudienceFriends
};
But I'm getting the same error.
Ok so if you haven't setup an account from your Settings in iOS 6 it throws error code 6. If the user simply denies permission than it throws error code 7. In case 6 i suggest you ask the user to first setup her account in Settings.
NSDictionary *options = #{
ACFacebookAppIdKey: #"1234567890",
ACFacebookPermissionsKey: #[#"publish_stream"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
if([accounts count]>0)
self.facebookAccount = [accounts lastObject];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
// Fail gracefully...
NSLog(#"%#",error.description);
if([error code]== ACErrorAccountNotFound)
[self throwAlertWithTitle:#"Error" message:#"Account not found. Please setup your account in settings app."];
else
[self throwAlertWithTitle:#"Error" message:#"Account access denied."];
});
}
}];
jAmi has the right idea, but I don't like the idea of the magic numbers in the code. There is an enum that has the numbers built into it (ACErrorCode).
My own solution was to use Facebook SDK instead of trying with the lib directly and now it's working.

Getting Twitter handle from Twitter Framework in iOS

I know that to get access to a configured twitter account using twitter framework in iOS 5, the following can be done:
ACAccountStore *t_account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted == YES) {
}
}
But the problem is that I don't have need for a full account, I just want the twitter name if he has configured any on the twitter accounts. So, Is there a way to get just the twitter handle from the framework (not the full account) without asking any user permission ?
You won't get access to any accounts without the user granting permission to access those accounts. Just try through the following in an app delegate in a new project so you know that app has not been given permissions to access the twitter accounts. Of course make sure you have at least one twitter account on the device or in the simulator and the accounts framework imported in your project and app delegate. Also you are making the assumption that the user has only one twitter account. Best to code for a case where a user may have more than one account.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
for (ACAccount *account in accountsArray ) {
NSLog(#"Account name: %#", account.username);
}
NSLog(#"Accounts array: %d", accountsArray.count);
Now change the code to return your results when a user has granted permission:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
for (ACAccount *account in accountsArray ) {
NSLog(#"Account name: %#", account.username);
}
NSLog(#"Accounts array: %d", accountsArray.count);
}
}];
So I guess the short answer is, there is a reason apple ask the user to grant permission to user accounts. If that access has not been granted you aren't going to get any data back.

Resources