requestAccessToAccountsWithType is deprecated in ios6.1, what should i use instead? - twrequest

-(void)getTwittersFollowers{
ACAccountStore *store=[[ACAccountStore alloc]init];
ACAccountType *twitterAccType=[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
**[store requestAccessToAccountsWithType:twitterAccType withCompletionHandler:^(BOOL granted,NSError *error)**{
if(!granted){
NSLog(#"User refused to access to his account");
}else{
NSArray *twitterAcc=[store accountsWithAccountType:twitterAccType];
if([twitterAcc count]>0){
ACAccount *account=[twitterAcc objectAtIndex:0];
NSLog(#"account name %#",[account accountDescription]);
NSMutableDictionary *params=[[NSMutableDictionary alloc]init];
[params setObject:#"1" forKey:#"inclue_entitles"];
NSURL *url=[NSURL URLWithString:#"http://api.twitter.com/1/followers.json"];
//TWRequest *request=[[TWRequest alloc]initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
SLRequest *request=[SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:url parameters:params];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responce,NSHTTPURLResponse *urlResponce,NSError *error){
if(!responce){
NSLog(#"%#",error);
}else{
NSError *jsonError;
NSMutableDictionary *followers=[NSJSONSerialization JSONObjectWithData:responce options:NSJSONReadingMutableLeaves error:&error];
if(followers!=nil){
for (int i=0; i<[[followers objectForKey:#"users"] count]; i++) {
NSLog(#"_____%#",[[[followers objectForKey:#"users"] objectAtIndex:i] objectForKey:#"screen_name"]);
NSLog(#"______________________________________%#",[[[followers objectForKey:#"users"] objectAtIndex:i] objectForKey:#"profile_image_url"]);
[nameArray addObject:[[[followers objectForKey:#"users"] objectAtIndex:i] objectForKey:#"screen_name"]];
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[[followers objectForKey:#"users"] objectAtIndex:i] objectForKey:#"profile_image_url"]]];
[imageArray addObject:imageData];
NSLog(#"%i", [imageArray count]);
}
}else{
NSLog(#"%#",jsonError);
}
}
}];
}
}
}];
}
what should i use instead of highlighted code...?

Though it's for some reason not explicitly mentioned in the documentation, the replacement for the method is - (void)requestAccessToAccountsWithType:(ACAccountType *)accountType options:(NSDictionary *)options completion:(ACAccountStoreRequestAccessCompletionHandler)completion (which can easily be found out by looking at the documentation)

Related

MBProgressHUD goes background

I am doing Facebook integration, first i have to authenticate and then i have to fetch details. During this process i am showing HUD loader. After authentication the Hud fades and not showing clearly.My code follows,please suggest me if i am doing anything wrong.
-(IBAction)btn_FacebookClicked:(id)sender
{
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText =#"Loading";
HUD.dimBackground=YES;
[HUD show:YES];
[self performSelectorInBackground:#selector(FacebookLogin) withObject:nil];
}
-(void)FacebookLogin
{
if(!accountStore)
accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:#{ACFacebookAppIdKey: #"**********", ACFacebookPermissionsKey: #[#"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
facebookAccount = [accounts lastObject];
NSLog(#"Success");
[self userfbDetail];
}else{
// ouch
[HUD hide:YES];
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
}
}
// Get user facebook details
- (void)userfbDetail{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = facebookAccount;
[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
// NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSMutableArray *arr_facebook=[[NSMutableArray alloc]init];
arr_facebook=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSString *firstname=[arr_facebook valueForKey:#"name"];
NSString *user=[NSString stringWithFormat:#"Welcome %#",firstname];
ALERT(user);
[HUD hide:YES];
}];
}

connectionDidFinishLoading shows an errors in the NSString *jsonString line

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:_buffer options:0 error:&error] description];
dispatch_async(dispatch_get_main_queue(), ^{
if (!error)
{
self.textField.text = jsonString;
}
else
{
self.textField.text = [error localizedDescription];
}
[self.spinner stopAnimating];
[self.fetchButton setEnabled:YES];
self.connection = nil;
self.buffer = nil;
});
});
}
When i write this method then it shows errors in the fifth line. please help me I am new for the Iphone.Thanks in advance.
You should write it like this:
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:_buffer options:0 error:&error] description];
Remove amp;
from
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:_buffer options:0 error:&error] description];
It should be
NSString *jsonString = [[NSJSONSerialization JSONObjectWithData:_buffer options:0 error:&error] description];

I get the Malformed access token for Facebook in ios 6

I want to fetch the facebook friends profile picture.I also give the access_token for it.
I search a lot on net but didn't got any good solution.
my code is as follow:
- (IBAction)advancedButtonPressed:(id)sender {
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
//NSString *key = #"xxxxxx";
NSString *key = #"xxxxxx";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
self.facebookAccount = [accounts lastObject];
NSLog(#"facebook account =%#",self.facebookAccount);
ACAccountCredential *fbCredential = [self.facebookAccount credential];
accessToken = [fbCredential oauthToken];
NSLog(#"Facebook Access Token: %#", accessToken);
[self get];
} else {
//Fail gracefully...
NSLog(#"error getting permission %#",e);
}
}];
}
-(void)fr
{
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends?fields=id,name,picture,first_name,last_name,gender&access_token=BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"Dictionary contains data: %#", list );
if([list objectForKey:#"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
nameLabel.text = [list objectForKey:#"username"];
});
}
else{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
}];
}
But i always got the error like this:
error = {
code = 190;
message = "Malformed access token BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD?access_token=BAAFcRQZCZAUeUBAHBZBSdTL9wWFDOVthxj78bBJlrNEkPiKHdxZA1HaRmO9dtBTJdsZCtZCu2vaE5ByZAdK7Ox30BpIR0KTg0ZC6pi1IOE0KnZCZBSxqbycG0C6Ws5Ct4ferL3G0VU1wIfDypKekFM3zB4r5gAiUREQ66Yuz2Fu2mp5NGqZCBE1p357H41P2lKOAQN9EZAmu4q3SRtjMuVIw4dtdC00l4RhMuDWTOIUQlO54bgZDZD";
type = OAuthException;
};
}
I can't able to solve this. Help me!
If you're using an SLRequest, it will auto append the access token for you, and you also should not have any request params in the URL itself. Instead, you should do something like:
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:#{#"fields":#"id,name,picture,first_name,last_name,gender"}];
You can also use this instead of using SLRequest.It is also
NSURL *meurl = [NSURL URLWithString:[NSString stringWithFormat:#"https://graph.facebook.com/me?access_token=%#",fbAccessToken]];
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:meurl];
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *userDataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}];

How to fetch Facebook friends in ios 6?

I want to send invite facebook friends from my app same as doing like in Instagram app.
But i can't find any good tutorial for that.
I able to get the name & id of the friend but I can't able to get the url for the profile image,name,etc. for the friends.
My code is as follow:
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"Dictionary contains data: %#", list );
if([list objectForKey:#"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
nameLabel.text = [list objectForKey:#"username"];
});
}
else{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
}];
So, how can i achieve this?
This is code for it for fetching friends list
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"add_here_your_project_FB_ID",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions",#"read_friendlists"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else {
NSLog(#"error.localizedDescription======= %#", error.localizedDescription);
}
}];
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
//NSString *acessToken = [NSString stringWithFormat:#"%#",facebookAccount.credential.oauthToken];
//NSDictionary *parameters = #{#"access_token": acessToken};
NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:#"picture,id,name,link,gender,last_name,first_name,username",#"fields", nil];
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/friends"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:feedURL
parameters:param];
feedRequest.account = facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
if(!error)
{
id json =[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSLog(#"Dictionary contains data: %#", json );
if([json objectForKey:#"error"]!=nil)
{
//[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
//nameLabel.text = [json objectForKey:#"username"];
});
}
else{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
}];
I hope it helps you. Thanks
you should include this in param
[ NSMutableDictionary dictionaryWithObjectsAndKeys:#"picture,id,name,link,gender,last_name,first_name",#"fields",nil]

Share an image on Facebook using App ID

I want to share my image on the facebook using my AppID. So I can see it on facebook as via App. Its working fine till iOS 5. But not in iOS 6.
If I'm using SLComposeViewController, so I can share image but is displays 'via iOS' instead of 'via App'. So I've reviewed code for it and applied, but it's not sharing the image. Please check the code for any issues and correct it.
facebookaccount = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ ACFacebookAppIdKey: #"xxxxxxxxxxxxx", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSArray *accountsArray = [facebookaccount accountsWithAccountType:facebookaccountType];
facebookAccount = [[accountsArray lastObject] retain];
NSLog(#" Account :::: %#", facebookAccount);
} else {
NSLog(#" Error :::: %#", error.description);
}}];
NSDictionary *parameters = #{#"message" : #""};
NSString *link = [NSString stringWithFormat:#"%simages/%#",ServerPath,[images objectAtIndex:imageNO]];
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression){
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
UIImage *img = [[UIImage alloc] initWithData:imageData];
NSLog(#"Image ::: %#", img);
NSData *myImageData = UIImagePNGRepresentation(img);
NSLog(#" Image Data ::: %#", myImageData);
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:requestURL parameters:parameters];
[facebookRequest addMultipartData:myImageData withName:#"source" type:#"multipart/form-data" filename:nil];
NSLog(#"Request Account :::: %#", facebookAccount);
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(#"Done with Facebook.......");
if (!error) {
NSDictionary *list = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#" Dictionary ::: %#", list);
} else {
NSLog(#" Error :::: %#", error.description);
}}];
I'm getting null values in the dictionary log.
What can be the issue? Thank you.
use below code rather than SLComposeViewController use SLRequest
NSDictionary *parameters = #{#"message": sendmessage};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: myImageData
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
i hope this will also help you
-(void)facebookPost:(NSString *)text{
ACAccountStore *facebookaccount1 = [[ACAccountStore alloc] init];
ACAccountType *facebookaccountType = [facebookaccount1 accountTypeWithAccountTypeIdentifier: ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{ ACFacebookAppIdKey: #"xxxxxxxxxxxxxxx", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[facebookaccount1 requestAccessToAccountsWithType:facebookaccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [facebookaccount1 accountsWithAccountType:facebookaccountType];
if ([accountsArray count] > 0)
{
NSDictionary *parameters = #{#"message": text};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
NSString *link = [NSString stringWithFormat:#"%simages/%#",ServerPath,[images objectAtIndex:imageNO]]; // image link
NSURL *url = [NSURL URLWithString:link];
NSData *data = [NSData dataWithContentsOfURL:url];
// code for compress image you can delete this code if you dont want to compress image
CGFloat compression = 1.0f;
CGFloat maxCompression = 0.0f;
int maxFileSize = 150*224;
UIImage *img1 = [[UIImage alloc] initWithData:data];
NSData *imageData = UIImageJPEGRepresentation(img1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(img1, compression);
}
// imageData is your image
[facebookRequest addMultipartData: imageData
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
ACAccount *facebookAccount1 = [accountsArray objectAtIndex:0];
[facebookRequest setAccount:facebookAccount1];
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
// Log the result
}];
}
}
}];
}