Retrieve Facebook Account Information in ios 6 - iphone

i want retrieve the information of a facebook account on the ios 6 integrated Facebook, now i do it in this way and i can retrieve only the username:
ACAccountType *accountTypeF = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountTypeF];
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
if (facebookAccount.username) {
}
}
}];
i want know how i can retrieve also the Name and Surname of the account and the pic of the user...anyone can help me?

Jeffrey is right. Go to this post: Facebook iOS 6 - get user info and implement 2 methods given by Daniel.
After then make sure to make some changes in your application settings on facebook developer account.
On main settings page disable sandbox mode
Put your applications bundle id
Enable facebook login
Then go to Advance settings on the left
Select Native/Desktop as App Type
make sure App secret in client is NO
Save all settings and it really takes few minutes to populate the changes on server. Finally Must reset you simulator

ACAccount is a generic "social" class provided by Apple; it has only limited account information and no knowledge of Facebook data. After obtaining the account, you can instantiate an SLRequest to query the Facebook Graph API, using the account to authenticate it...
NSURL* URL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest* request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:URL
parameters:nil];
[request setAccount:account]; // Authentication - Requires user context
[request performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
// parse the response or handle the error
}];

Related

facebook login works on iphone simulator but not on iphone device

In my app, I want to take photos from the facebook. so i used facebook ios sdk. i can be able to authorize from the below code:
if (appDelegate.session.isOpen) {
//[appDelegate.session closeAndClearTokenInformation];
[self retrieveAlbums];
} else {
if (appDelegate.session.state != FBSessionStateCreated) {
// Create a new, logged out session.
appDelegate.session = [[FBSession alloc] init];
}
// if the session isn't open, let's open it now and present the login UX to the user
[appDelegate.session openWithCompletionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// and here we make sure to update our UX according to the new session state
//NSLog(#"%#",session.accessTokenData.accessToken);
[self retrieveAlbums];
}];
It works fine on iphone simulator. But I can't able to login on my iphone 4 device.
When I tried to login it will open a safari and then shows the message Updating.
Then it will redirect without authorization. access token retrieved as nil. So I cant able to retreive images.
Please help me.
EDIT
Thanks for everyone .Finally I solved my problem. It is because i enabled sandbox mode in facebook developer account settings. When i disabled the sandbox mode, it works normally.
Try clearing the cookies and cache of your safari, going to Settings.
Also if you do not want the app to redirect to Safari, just dont provide the URL Scheme in your info.plist that is something like "fb-AppId-" and it will open a popup for login
I was having the similar problem and solved it by composing couple of solutions in this website. First of all I got rid of the url scheme at my info plist. So now my app opens up the facebook login dialog inside my registration page. Which is a good thing because it doesn't go back and forth to safari for facebook authentication and you stay inside the app. The second thing I made is, putting an error handling mechanism inside my code for facebook login. So if login with facebook fails, it uses another strategy for logging in.
Here is my code.
- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"public_profile", #"user_friends", #"email",
nil];
FBSession *session = [[FBSession alloc] initWithPermissions:permissions];
[FBSession setActiveSession:session];
[self openSessionWithAllowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
if (!error && status == FBSessionStateOpen) {
NSString *token = session.accessTokenData.accessToken;
NSLog(#"Logged in! - Token -- %#", token);
} else {
NSLog(#"Something wrong happened");//you can check the error code here
}
}];
}

Get if my app is authorized to use facebook [iOS 6, FB SDK 3.2]

how can i know is my app is authorized by user to use Facebook?
You can enable or disable it in "Settings -> Privacy -> Facebook".
I have to get this state.
I'm using Facebook 3.2
Thank you all!
You can use the ACAccountStore to get the Facebook account(if it is available) and check if the access is granted:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if(accountType.accessGranted){
//app has permissions
}else{
//no permissions
}
If you are looking for a line of code that checks, it's this one right here:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
//code goes here
}
NOTE
This code will help only if you are using the Social Framework.

Getting error ACErrorPermissionDenied (error code 7) for Facebook when using Social Framework

I am fetching the user’s Facebook account ID and name using ACAccountStore.
When an alert comes up asking the user for permission (“XYZ would like to access your news feed and profile”) and the user taps on “Don’t Allow”, I get error code 7, i.e ACErrorPermissionDenied.
But after that, if I go to Settings and switch on the Facebook settings for my app and back in the app the alert pops up again and the user taps on “OK”, I’m still getting error code 7 (ACErrorPermissionDenied). So it’s enabled in Settings and I’ve tapped “OK” in the app, but it’s still giving me this error.
If the first time I allow access and after that switch access on or off from Settings, it gives me the proper error or success. This problem occurs only if the user taps “Don’t Allow” the first time. If he doesn’t allow it in the app when it asks, then he will not be able to allow FB access to my app.
I’ve tested this on iOS 6.0 and 6.0.1 using the following code:
NSMutableArray *acAccountArray = [[NSMutableArray alloc] init];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType =
[accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore
requestAccessToAccountsWithType:accountType
options:[self getOptionDictionary:ACAccountTypeIdentifierFacebook permission:[[NSArray alloc]initWithObjects:#"read_stream",#"user_about_me",#"email", nil]]
completion:^(BOOL granted, NSError *error) {
if(granted) {
// Doing what I want
} else {
NSLog(#"Error: %#", error);
}
}];
-(NSDictionary *)getOptionDictionary:(NSString * const)accountTypeIdentifier
permission:(NSArray*)permissionArray {
NSDictionary *options = nil;
if (accountTypeIdentifier == ACAccountTypeIdentifierFacebook) {
options = #{
#"ACFacebookAppIdKey" : #"dhksadhakjdhkshd",
#"ACFacebookPermissionsKey" : permissionArray,
#"ACFacebookAudienceKey" : ACFacebookAudienceFriends};
}
return options;}
Is your app still in "sandbox mode" on Facebook? If yes, it will be visible only to administrators and developers. If you're logged in to Facebook on your device with a non-administrator for your app, you might receive this error. Disabling the sandbox mode fixed the problem for me.
This is because you have removed your app in facebook, even i am facing same problem. If you try to login using any method other than iOS native login then it adds facebook app if it is not there or removed by the user in facebook settings but it is currently not happening if you try to login using iOS native login.
Please inform me if you come across ant solution for this issue and also is their a way i can remove my app being listed in iOS facebook settings in this case because if i remove and then user retries for login then it works correctly.

twitter update using TWRequest gives 403 error

i'm using the following code to tweet in user's timeline using the iOS 5 twitter API
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if(granted)
{
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:buttonIndex];
// Create a request, which in this example, posts a tweet to the user's timeline.
// This example uses version 1 of the Twitter API.
// This may need to be changed to whichever version is currently appropriate.
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:#"hello this is a tweet" forKey:#"status"] requestMethod:TWRequestMethodPOST];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
// Perform the request created above and create a handler block to handle the response.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSString *output = [NSString stringWithFormat:#"%i", [urlResponse statusCode]];
[self performSelectorOnMainThread:#selector(TweetStatus:) withObject:output waitUntilDone:NO];
}];
}
}];
i've been using this method for more than a month now for testing the application and it used to work fine with no problems.
few weeks a goo it started to return a 403 error in the following method.
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
NSString *output = [NSString stringWithFormat:#"%i", [urlResponse statusCode]];
[self performSelectorOnMainThread:#selector(TweetStatus:) withObject:output waitUntilDone:NO];
}];
it gives the following error:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x1f8b3250 {NSErrorFailingURLKey=http://api.twitter.com/1/statuses/update.json, NSErrorFailingURLStringKey=http://api.twitter.com/1/statuses/update.json, NSUnderlyingError=0x1ed19f90 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012
i've searched a lot and could not find any solution or real cause to this problem.
a few notes to keep in mind that might help understanding the problem:
the application is used for testing which means that we used the twitter sharing and mentions a lot with other users.
the sharing content is not always the same so no duplication issue should occur as we were clearing the timeline frequently.
thanks
The text you have been trying to tweet must be similar to what has been tweeted by you previously, hence you are getting this error. According to the documentation here, the same text cannot be posted more than once.
EDIT:
It might also depend on the total number of tweets as mentioned in their website.
i've been using this method for more than a month now for testing the application and it used to work fine with no problems.
I think that's the issue right there. this account you are testing might been marked as a spammer by other people.
If you use another account or other phone and the code works, then that's the only explanation my friend.
Good luck
I noticed you are using the following URL to post a tweet from your user:
http://api.twitter.com/1/statuses/update.json
This is using version 1.0 of the Twitter API which is deprecated and slowly will stop working over the next few months
Instead you should try using
https://api.twitter.com/1.1/statuses/update.json (There has been a recent change to the Twitter API. You can now only call it using HTTPS.)
This will specify the latest version of the API.

Error creating twitter ACAccount on iOS5: NSURLErrorDomain error -1012

I am having trouble creating and saving a new twitter account to ACAccountStore on iOS5.
After performing all the steps to init the account, ACAccountStore's saveAccount:withCompletionHandler: returns NSURLErrorDomain error -1012.
Does anyone have similar issues?
My code sample below is using requestToken to init ACAccountCrendential. This object is an OAToken (ShareKit object) initialized with the token and secret received from twitter after completing OAuth.
ACAccountStore *store = [[[ACAccountStore alloc] init] autorelease];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
ACAccount *account = [[[ACAccount alloc] initWithAccountType:twitterAccountType] autorelease];
account.username = #"twitterusername";
account.credential = [[[ACAccountCredential alloc] initWithOAuthToken:requestToken.key tokenSecret:requestToken.secret] autorelease];
[store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if (granted) {
[store saveAccount:account withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog(#"TWITTER ACCOUNT CREATED SUCCESSFULLY!");
}
else{
NSLog(#"ERROR creating twitter account: %#", [error description]);
}
}];
}
}];
The strange thing is that Apple's Accounts Framework Reference suggests that saveAccount:withCompletionHandler: attempts to perform OAuth itself:
If the account type supports authentication and the account is not authenticated, the account is authenticated
using its credentials. If the authentication is successful, the account is saved; otherwise, it is not saved.
I find that very strange since there is no way for the user to authenticate directly with the server by inputting username/password.
I also tried to initialize ACAccountCredential with my application's consumer key and consumer secret, with the same result (failed with NSURLErrorDomain error -1012.)
Any guidance on this topic would be greatly appreciated!
thanks.
I'm having the same trouble adding a Twitter account with ACAccount. However, I was able to create a Twitter account with ACAccount successfully using an authorized OAuth Access token and Access token secret, not the Consumer key and Consumer secret. (dev.twitter.com/apps->Twitter->OAuth->OAuth Setting-->Access token and Access token secret) This means that you still have to use an OAuth library to get an authorized Access token and Access token secret when the user is adding a Twitter account.
Update: You can see how Twitter recommends that you migrate existing accounts to iOS5 system accounts here. That link provided me with the best example of how to add an ACAccount.