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.
Related
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.
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.
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.
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!
Whenever you try and present a TWTweetComposeViewController and a user has no Twitter account added to their device they are prompted to go to the Settings app and add one. Once they are done they have to manually navigate back to the application.
Is there any way for my application to know that they have successfully added an account?
Actually, there is a way to be notified of new accounts while your application is running. ACAccountStore provides a notification ACAccountStoreDidChangeNotification which you can observe for changes using Key-Value Observing.
Ah, in that case, you can keep track of how many user accounts they had when they first launched the app, save that to NSUserDefaults. When you're putting up the TWTweetComposeViewController check to see if the number is the same as it was before.
__block BOOL accountSizeChanged = NO;
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted)
{
int oldSize = [[NSUserDefaults standardUserDefaults] integerForKey:#"myKey"];
int newSize = [accountStore accountsWithAccountType:accountType].count;
if(oldSize != newSize)
accountSizeChanged = YES;
[[NSUserDefaults standardUserDefaults] setInteger:newSize forKey:#"myKey"];
}
}];