MBProgressHUD goes background - facebook

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];
}];
}

Related

is it possible to tag a post/photo using ios social framework?

In my app I need to tag my photo/posts. Is this possible using social framework or Should I use Facebook-sdk for this?. Also can I get friends list from social framework? any help to achieve this?
Tagging a photo or post can only be done using the FacebookSDK for iOS while getting your friends list is possible using both the SocialFramework and the FacebookSDK for iOS.
You can use this code to get your friends list using the SocialFramework.
- (void)getFbFriends
{
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
ACFacebookAppIdKey: facebookAppId,
ACFacebookPermissionsKey: #[#"email"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[store requestAccessToAccountsWithType:facebookTypeAccount
options:options
completion:^(BOOL granted, NSError *error){
if(granted)
{
NSArray *accounts = [store accountsWithAccountType:facebookTypeAccount];
ACAccount *facebookAccount = [accounts lastObject];
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:meurl
parameters:nil];
merequest.account = facebookAccount;
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"}];
request.account = facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if([list objectForKey:#"error"]!=nil)
{
// if error occured e.g. Invalid Auth etc.
}
else
{
NSMutableArray *FBFriends = [[NSMutableArray alloc] init];
[FBFriends addObject:[userInfo objectForKey:#"id"]];
NSLog(#"friends %#", list);
}
}
else
{
NSLog(#"Error");
}
}];
}
else
{
NSLog(#"Error grant access");
}
}];
}

Post image is no more working with new iOS Facebook SDK

I add the function working well on iOS6, but with iOS7, I had to update Facebook SDK because id and setId from the old ones are marked as non-public and are'nt validated.
So, I replaced the old SDK by the new one but, of course, the post image is no more working (as the id is certainly unknown).
I had this :
[params setObject:textViewMessageFacebook.text forKey:#"message"];
[params setObject:UIImagePNGRepresentation(screenshot) forKey:#"picture"];
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%#/photos", idAmiS]
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
but don't know how to correct it to have it working...
Thanks
Use SlComposer Controller to post image on facebook.
1) first you add the Social framework and Account framewok into your project and then try below code
writ this code where you want...
{
ACAccountType *facebookTypeAccount = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:#{ACFacebookAppIdKey: #"131740927031850", ACFacebookPermissionsKey: #[#"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
ACAccountCredential *fbCredential = [_facebookAccount credential];
accessToken = [fbCredential oauthToken];
NSLog(#"Facebook Access Token: %#", accessToken);
NSLog(#"Success");
[self facebook];
}else{
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[tweetSheet setInitialText:#"posting from my own app! :)"];
[tweetSheet addURL:[NSURL URLWithString:#"www.xyz.com"]];
// Here you add the image
[tweetSheet addImage:[UIImage imageNamed:#"yourimage.png"]];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)facebook
{
NSURL *meurl = [NSURL URLWithString:#"https://graph.facebook.com/me"];
NSLog(#"URl:-%#",meurl);
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];
NSLog(#"Parsed Data:-%#", meDataString);
}];
}

How to use iOS TWRequest for iOS 5 for getting Twitter User Details

How to use TWRequest for both iOS 5 for getting Twitter User Details,
TWRequest was working earlier and I was using in this way
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1/users/show.json"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,#"screen_name",nil];
request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
But recently, Twitter closed the api version 1 and implemented version 1.1, and Presently The above logic is not working in iOS 5, due to the API deprecation may be...
I am using SLRequest for iOS 6 it is working perfect, But I would like to know How can we get Twitter user details in iOS 5
Try this :
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com/1.1/users/show.json"];
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,#"screen_name",nil];
request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
// Runing on iOS 6
if (NSClassFromString(#"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
[accountStore requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
{
if (granted)
{
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url parameters:parameters];
[request setAccount:[twitterAccounts lastObject]];
dispatch_async(dispatch_get_main_queue(), ^
{
[NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (data)
{
[self loadData:data];
}
});
}];
});
}
}];
}
else if (NSClassFromString(#"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
{
[accountStore requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted)
{
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
[request setAccount:[twitterAccounts lastObject]];
dispatch_async(dispatch_get_main_queue(), ^
{
[NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)
{
dispatch_async(dispatch_get_main_queue(), ^
{
if (data)
{
[self loadData:data];
}
});
}];
});
}
}];
}
}

Use an ACAccount without saving it

My app has tokens from Twitter and wants to make a SLRequest with them but when I do this without saving the ACAccount it doesn't. How can I fix this?
Code:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountCredential *credential = [[ACAccountCredential alloc] initWithOAuthToken:#"token" tokenSecret:#"secret"];
ACAccountType *twitterAcctType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
ACAccount *newAccount = [[ACAccount alloc] initWithAccountType:twitterAcctType];
newAccount.credential = credential;
newAccount.username = #"username";
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:[NSURL URLWithString:#"https://api.twitter.com/1.1/statuses/user_timeline.json"] parameters:#{ #"screen_name" : #"username" }];
request.account = newAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(#"ResponseData: %#", [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding]);
NSLog(#"StatusCode: %d", urlResponse.statusCode);
NSLog(#"Error: %#", error);
}];
Log:
2013-06-20 00:07:55.554 MyApp[6009:c07] ResponseData:
2013-06-20 00:07:55.555 MyApp[6009:c07] StatusCode: 0
2013-06-20 00:07:55.555 MyApp[6009:c07] Error: Error Domain=SLErrorDomain Code=0 "Could not prepare the URL request." UserInfo=0x9499020 {NSLocalizedDescription=Could not prepare the URL request.}

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]