Get facebook friends list through social framework in ios7 - ios

I am trying to get facebook friends list through social framework.
- (void)getfriends {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *emailReadPermisson = #{
ACFacebookAppIdKey : #"1537296106498495",
ACFacebookPermissionsKey : #[#"user_friends"],
ACFacebookAudienceKey : ACFacebookAudienceEveryone,
};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:emailReadPermisson completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:facebookAccountType];
_facebookAccount = [accounts lastObject];
NSString *acessToken = [NSString stringWithFormat:#"%#",_facebookAccount.credential.oauthToken];
NSDictionary *parameters = #{#"access_token": acessToken};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:feedURL
parameters:parameters];
feedRequest.account = _facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"%#",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
}
}];
}
But data field in the response is empty. My response is:
2014-08-06 18:09:11.459 Social[5246:1303] {"data":[],"summary":{"total_count":1115}}

Related

What is wrong with Twitter API on iOs 8?

I want to create an action in Objetive-C that makes an instant "follow" in a Twitter Acount, but when I launch my app it crashes. Could anyone tell me what is wrong?
It returns thread 1 exc_bad_access.
- (IBAction)Twitter:(id)sender {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"user" forKey:#"UserName"];
[tempDict setValue:#"true" forKey:#"follow"];
NSURL *URLTwitter = [NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.format"];
SLRequest *postRequest = [SLRequest requestForServiceType:#"Twitter" requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %li", [urlResponse statusCode]];
NSLog(#"%#", output);
}];
}
}
}];
}
Your twitter url is incorrect. It should be
https://api.twitter.com/1.1/friendships/create.json
Here is what was wrong...
1. Firstly, since the new Twitter API version 1.1 the correct URL is: https://api.twitter.com/1.1/friendships/create.json.
2. The 'for key' value for the user name must to be "screen_name", and it will be like the following fragment:
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"UserNameOnTwitter" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
The value for 'requestForServiceType' must to be SLServiceTypeTwitter as the following:
SLRequest *postRequest = [SLRequest requestForServiceType: SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];
So the whole block will be:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:#"UserNameOnTwitterWithoutAt" forKey:#"screen_name"];
[tempDict setValue:#"true" forKey:#"follow"];
NSURL *URLTwitter = [NSURL URLWithString:#"https://api.twitter.com/1.1/friendships/create.json"];
SLRequest *postRequest = [SLRequest requestForServiceType: SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:URLTwitter parameters:tempDict];
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %li", [urlResponse statusCode]];
NSLog(#"%#", output);
}];
}
}
}];
Thanks anyway everyone.

User friends are showing empty list in Facebook Sdk Graph Api v2

I have updated the version for the graph Api to v2. Now the issue I am facing is,when I executed the following code It shows me empty array :
FBRequest *friendsRequest = [FBRequest requestForMyFriends];
[friendsRequest startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *result, NSError *error) {
NSArray *friendshipid;
NSString *username;
if (!error) {
NSLog(#"friends = %#", [result description]);
}
if (completion) {
completion(friendshipid, username, error);
}
}];
}
}
I got to know that facebook sdk had some changes for the user friends and now It has to take the permission for the user_friends, but I have no idea where to make changes to ask for Permission for user_friends
You can use the given code snippet. Here, i have used the Classes of Social Framework to get the Facebook friends. Hope it will work for you.
- (void) connectWithFacebookFriends
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountTypeFacebook = [accountStore accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{ACFacebookAppIdKey: kFaceBookId,
ACFacebookPermissionsKey: #[#"user_friends"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:accountTypeFacebook
options:options
completion:^(BOOL granted, NSError *error)
{
if(granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:accountTypeFacebook];
ACAccount* facebookAccount = [accounts lastObject];
NSString *acessToken = [NSString stringWithFormat:#"%#",facebookAccount.credential.oauthToken];
NSDictionary *parameters = #{#"access_token": acessToken};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *feedRequest =
[SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:feedURL
parameters:parameters];
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSString * str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
DLog(#"%#",str);
NSLog(#"Request failed, %#", [urlResponse description]);
}];
} else {
NSLog(#"Access Denied");
NSLog(#"[%#]",[error localizedDescription]);
}
}];
}

Getting error while fetching picture of facebook friends with Graph API in iOS Social Framework

I'm using below code to get facebook friends list, and its working fine. But there is a need of friends profile picture too.
NSString *fbPermissions = #[ #"user_about_me",#"email",#"user_friends"];
NSString *apiIdKey = #"App_Key";
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
id options = #{
ACFacebookAppIdKey: apiIdKey,
ACFacebookPermissionsKey: fbPermissions,
ACFacebookAudienceKey: ACFacebookAudienceFriends,
};
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [accountStore accountsWithAccountType:accountType];
ACAccount *tempAccount = [arrayOfAccounts lastObject];
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *fbURL = [NSURL URLWithString:string];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:fbURL
parameters:nil];
[fbRequest setAccount:tempAccount];
[fbRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!error)
{
NSError *jsonError = nil;
NSDictionary *data = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSMutableArray *array = [data valueForKey:#"data"];
NSLog(#"Friends array:- %#",array);
}
else
{
[[[UIAlertView alloc]initWithTitle:#"Error"
message:[error description]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil] show];
}
}];
}
}];
To get profile picture I have changed request url with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends?fields=picture,name"];
I got following response :-
{
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}
I'm not sure its access token expiration error because to revert back request Url to previous one, code works fine.
Finally it worked with:-
NSString *string = [NSString stringWithFormat:#"https://graph.facebook.com/me/friends"];
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *fbRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET URL:requestURL parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];

Twitter error - verify_credentials request fails but tweeting and other endpoints still accessible

I'm trying to access the user's credentials of Twitter with this code:
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *twitterAccount = [arrayOfAccounts lastObject];
NSURL *requestURL = [NSURL URLWithString:#"https:api.twitter.com/1.1/account/verify_credentials.json"];
SLRequest *getRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodGET
URL:requestURL parameters:nil];
[getRequest setAccount:twitterAccount];
[getRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];
NSLog(#"Twitter Response : %# ",json);
}];
}
};
}];
}
With this code I'm getting this error:
Twitter Response : {
errors = (
{
code = 32;
message = "Could not authenticate you";
}
);
}
The weird thing is that, if I try to tweet something, or access some other endpoint of Twitter, where it's required to have an oauth-token, the operation ends successfully.
try this
TwitterResponse twitterResponse1 = TwitterStatus.Update(tokens, "test", new StatusUpdateOptions { APIBaseAddress = "https://api.twitter.com/1.1/", UseSSL = true });

How to use SLRequest for change via iOS to via <AppName>

I goes through many link and tutorials but not working for me.
Can anybody explain me How to use SLRequest for change via iOS to via MyAppName ?? step by step or give me some links which gives this solution in step by step.
EDIT: I have tried. Below is my code may be help.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"012345678912345",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else
{
// Handle Failure
}
}];
NSDictionary *parameters = #{#"message": #"My first iOS 6 Facebook posting "};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = self->facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// Handle response
}];
}
First of all set you facebook setting using Facebook developer account setting
then reset your simular or delete app from device.
Add and import this frameworks
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <AdSupport/AdSupport.h>
#import <FacebookSDK/FacebookSDK.h>
use this code for further process
in yourclass.h file
#property (nonatomic, strong)ACAccountStore *accountStore;
in yourclass.m file
#synthesize accountStore;
- (IBAction)facebookButtonTouch:(id)sender {
if (self.accountStore == nil)
self.accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = #[#"publish_stream", #"publish_actions",#"email"];
NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"your app id", ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *readPermissions = #[#"read_stream", #"read_friendlists"];
[options setObject:readPermissions forKey: ACFacebookPermissionsKey];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted && error == nil) {
/**
* We now should have some read permission
* Now we may ask for write permissions or
* do something else.
**/
} else {
NSLog(#"error is: %#",[error description]);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else {
NSLog(#"error.localizedDescription======= %#", error.localizedDescription);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
//-------------- start code for posting message on wall via SLRequest------------------
NSDictionary *parameters = #{#"message": #"Hi Friends i m ....."};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"responseData %#",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
//-------------- end code for posting message on wall via SLRequest------------------
}
i hope it will help you. Thanks

Resources