Search code examples
iosobjective-cslcomposeviewcontroller

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

![enter image description here][2]

![enter image description here][3]


Solution

  • 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.