iOS 6.0 Facebook Connectivity - iphone

I have to get connected to the facebook on button click in iOS 6.0. I have added the frameworks social and accounts to my project. I am able to check in to a place but, not able to tag friends for what I am posting to the facebook. How to fetch facebook friends list?
The code I have used is shown below :
- (IBAction)connectToFacebook:(id)sender
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = #{
#"ACFacebookAppIDKey": #"412590558803147",
#"ACFacebookAppVersionKey": #"1.0",
#"ACFacebookPermissionsKey": #"publish_stream",
#"ACFacebookPermissionGroupKey": #"write"
};
NSLog(#"options is %#",options);
[accountStore requestAccessToAccountsWithType:accountType options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
NSArray *accounts = [accountStore
accountsWithAccountType:accountType];
NSString *facebookAccount = [accounts lastObject];
NSLog(#"facebook account %#", facebookAccount);
} else {
NSLog(#"%#",error);
// Fail gracefully...
}
}];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Posted!!!" message:#"your status is posted to facebook successfully" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"This is a ios 6.0 facebook intergration application"];
[controller addImage:[UIImage imageNamed:#"spalshimage.jpeg"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
NSLog(#"UnAvailable");
}
}

The best way to integrate Facebook is to use the new iOS 6 Facebook SDK:
https://developer.apple.com/technologies/ios6/
Try this link for friend list:
https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/show-friends/

Related

Tweet, without using the tweet sheet

I'm using below code to share the content (from UITextView, UIImageView) through twitter
-(void)shareViaTweet:(NSString *)shareMessage{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:[NSString stringWithFormat:#"%#",shareMessage]];
if (self.imageString)
{
[tweetSheet addImage:[UIImage imageNamed:self.imageString]];
}
if (self.urlString)
{
[tweetSheet addURL:[NSURL URLWithString:self.urlString]];
}
[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];
}
}
But I need share this, without using the pop up view (I think tweet sheet). It's happening because the below code,
[self presentViewController:tweetSheet animated:YES completion:nil];
When I click the button "Share" of my app, I need to post that in twitter.
Edited:
- (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = messageTextView.text;
//hear before posting u can allow user to select the account
NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
for(ACAccount *acc in arrayOfAccons)
{
NSLog(#"%#",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview
}
NSURL *url = [NSURL URLWithString:#"https://api.twitter.com"
#"/1.1/statuses/user_timeline.json"];
NSDictionary *params = #{#"screen_name" : message,
#"forKey":#"status",
#"trim_user" : #"1",
#"count" : #"1"};
// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)
SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];
//Post the request
[request setAccount:acct];
//manage the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(error)
{
//if there is an error while posting the tweet
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"Error in posting" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else
{
// on successful posting the tweet
NSLog(#"Twitter response, HTTP response: %i", [urlResponse statusCode]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"Successfully posted" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"You have no twitter account" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
else
{
//suppose user not set any of the accounts
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"Permission not granted" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
} ];
//[widgetsHandler closeWidget:nil];
//[self postImage:shareImageView.image withStatus:messageTextView.text];
}
Update: Error
To send images u need do something like this
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
//create this request
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"https://api.twitter.com"#"/1.1/statuses/update_with_media.json"] parameters: [NSDictionary dictionaryWithObject:message forKey:#"status"]];
UIImage *imageToPost = [UIImage imageNamed:#"image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0f);//set the compression quality
[postRequest addMultipartData:imageData withName:#"media" type:#"image/jpeg" filename:#"image.jpg"];
//set account and same as above code
....
....
if u wanna share the tweet without using the tweet sheet see my answer it will post on twitter wall without using the tweet sheet see hear and also set the twitter account in the device. hope this helps
unfortunately the class TWRequest is deprecated in iOS 6 but alternatively we can use SLRequest present in the Social framework
the answer for this is similar to the old answer
i commented out something that i dont want but if u want to select which account to use then uncomment the commented code
- (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = _textView.text;
// NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
// for(ACAccount *acc in arrayOfAccons)
// {
// NSLog(#"%#",acc.username);
// NSDictionary *properties = [acc dictionaryWithValuesForKeys:[NSArray arrayWithObject:#"properties"]];
// NSDictionary *details = [properties objectForKey:#"properties"];
// NSLog(#"user name = %#",[details objectForKey:#"fullName"]);
// NSLog(#"user_id = %#",[details objectForKey:#"user_id"]);
// }
// Request access from the user to access their Twitter account
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
// Build a twitter request
// TWRequest *postRequest = [[TWRequest alloc] initWithURL:
// [NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:#"status"] requestMethod:TWRequestMethodPOST]; //commented the deprecated method of TWRequest class
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:#"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:#"status"]]; //use this method instead
//Post the request
[postRequest setAccount:acct];//set account
//manage the response
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(error)
{
//if there is an error while posting the tweet
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"Error in posting" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
// on successful posting the tweet
NSLog(#"Twitter response, HTTP response: %i", [urlResponse statusCode]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"Successfully posted" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"You have no twitter account" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
else
{
//suppose user not set any of the accounts
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Twitter" message:#"You have no twitter account" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
} ];
[account release];
}

Unable to post image into facebook using SLComposeViewController?

I would like to post image into facebook and twitter. I am fine with twitter but not with facebook using SLComposeViewController class. With out add image i am able to post text and url into facebook. The problem is when i use add image i was unable to post this image and also text, url. SLComposeViewController shows image, text and url when i send. I have correct appId and i did not get any errors. But the problem is still there. I don't where the problem is. Please help me.
- (void)performFBRequestUploadForImage{
[self showListOfFaceBookAccountsFromStore];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"ACtionCancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
[self dismissViewControllerAnimated:YES completion:nil];
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Face Book Message" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
};
[mySLComposerSheet addImage:[UIImage imageNamed:#"images4.jpg"]];
[mySLComposerSheet setInitialText:#"I am developer."];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://stackoverflow.com/"]];
[mySLComposerSheet setCompletionHandler:completionHandler];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
}
- (void)showListOfFaceBookAccountsFromStore
{
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if( [SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook] )
{
NSDictionary *options = #{
#"ACFacebookAppIdKey" : myAppId,
#"ACFacebookPermissionsKey" : #[#"publish_stream"],
#"ACFacebookAudienceKey" : ACFacebookAudienceFriends};
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error){
if(granted) {
ACAccount * account = [[accountStore accountsWithAccountType:accountType] lastObject];
NSLog(#"Facebook user: %#",[account username]);
if([account username]==NULL){
[self facebookAlert];
} else {
}
}
else{
NSLog(#"Read permission error: %#", [error localizedDescription]);
}
}];
} else {
[self facebookAlert];
}
}
I used below code in my projects to post the image it works fine.
if([SLComposeViewController instanceMethodForSelector:#selector(isAvailableForServiceType)] != nil)
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"Check out my Christmas Gift!"];
[controller addImage:#"gift.jpg"];
[self presentViewController:controller animated:YES completion:Nil];
}
You just try follow below tutorials
Tutorial 1
2.Tutorial 2
I checked the internet and noticed a few discussion in the recent 1 or 2 days related to this issue and they seem to related to Facebook bug. So if your code post messages and images successfully to Twitter, then don't worry! you are doing correctly.
https://stackoverflow.com/questions/14868518/sharekit-not-sharing-link-to-facebook/
https://discussions.apple.com/thread/4805777
I have resolved through open present-view controller in navigation-bar.it may help you.
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:shareFrom];
[controller addImage:self.photoImageView.image];
[controller addURL:[NSURL URLWithString:dayCareWebsite]];
dispatch_async(dispatch_get_main_queue(), ^ {
[self.navigationController presentViewController:controller animated:YES completion:nil];
});

Get facebook user details in ios 6 sdk?

How to retrieve facebook user details with ios 6 inbuilt facebook sdk? I tried few examples, but couldn't get work.
- (void) getFBDetails {
if(!_accountStore)
_accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:#{ACFacebookAppIdKey: #"514284105262105", ACFacebookPermissionsKey: #[#"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(#"Success");
[self me];
}else{
// ouch
NSLog(#"Fail");
NSLog(#"Error: %#", error);
}
}];
}
- (void)me{
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];
NSLog(#"%#", meDataString);
}];
}
But this fails to grab data from facebook. My app id is correct.
This the error message I got
Error: Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: no stored remote_app_id for app" UserInfo=0x1d879c90 {NSLocalizedDescription=The Facebook server could not fulfill this access request: no stored remote_app_id for app}
Not sure if this will fix it or not, but have you set the Facebook App ID in your AppName-Info.plist file of your app?
The key required is FacebookAppID, which is of type String.
Try filling in your App ID there as well and see if it works.
In iOS6.0 ,you have to ask read and write permissions separately.
First ask for read permissions that is email then ask for other permissions according to the app requirement.
in .h file
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#property (nonatomic, strong) ACAccountStore *accountStore;
#property (nonatomic, strong) ACAccount *facebookAccount;
in .m file
- (void) getuserdetails
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= nil;
//[self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
if (! FBaccountType) {
FBaccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
}
NSString *key =kFBAppId;
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];
self.facebookAccount = [accounts lastObject];
NSLog(#"facebook account =%#",self.facebookAccount);
[self get];
}
else
{
NSLog(#"fb error %#",e.description);
dispatch_async(dispatch_get_main_queue(), ^
{
[self performSelectorOnMainThread:#selector(hideLoader) withObject:nil waitUntilDone:YES];
NSLog(#"%#",e.description);
if([e code]== ACErrorAccountNotFound)
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Account not found"
message:msgSetUpFBAccount delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
}
else
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:msgFBAccessDenied
message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
}
});
NSLog(#"error getting permission %#",e);
}
}];
}
-(void)get
{
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
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: %#", list );
if([list objectForKey:#"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});
}
else
{
[self performSelectorOnMainThread:#selector(hideLoader) withObject:nil waitUntilDone:YES];
NSLog(#"error from get%#",error);
}
}];
}
-(void)attemptRenewCredentials{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(#"Good to go");
[self get];
break;
case ACAccountCredentialRenewResultRejected:
{
NSLog(#"User declined permission");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Access Denied"
message:#"You declined permission" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
break;
}
case ACAccountCredentialRenewResultFailed:
{
NSLog(#"non-user-initiated cancel, you may attempt to retry");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Access Denied"
message:#"non-user-initiated cancel, you may attempt to retry" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
break;
}
default:
break;
}
}
else{
//handle error gracefully
NSLog(#"error from renew credentials%#",error);
}
}];
}
T**o get this code work the bundle Identifier with which you have registered your application with facebook and bundle identifier in application plist file should be same**

How can we post something on facebook in ios 6 like twitter?

I am implementing facebook posting in my app. And add some code to post something on facebook account.
My code is as follows.
- (void)publishStory
{
NSLog(#"publish story called .......");
[FBRequestConnection
startWithGraphPath:#"me/feed"
parameters:self.postParams
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
NSString *alertText;
if (error) {
alertText = [NSString stringWithFormat:
#"error: domain = %#, code = %d",
error.domain, error.code];
} else {
alertText = [NSString stringWithFormat:
#"Posted action, id: %#",
[result objectForKey:#"id"]];
}
// Show the result in an alert
[[[UIAlertView alloc] initWithTitle:#"Result"
message:alertText
delegate:self
cancelButtonTitle:#"OK!"
otherButtonTitles:nil]
show];
}];
}
-(IBAction)cancelButtonAction
{
[[self presentingViewController] dismissViewControllerAnimated:YES completion:nil];
}
-(IBAction)shareButtonAction
{
// Add user message parameter if user filled it in
if (![self.postMessageTextView.text isEqualToString:#""]) {
[self.postParams setObject:self.postMessageTextView.text
forKey:#"message"];
}
// Ask for publish_actions permissions in context
if ([FBSession.activeSession.permissions
indexOfObject:#"publish_actions"] == NSNotFound) {
// No permissions found in session, ask for it
[FBSession.activeSession reauthorizeWithPublishPermissions:
[NSArray arrayWithObjects:#"publish_actions",#"publish_stream", nil]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
// If permissions granted, publish the story
NSLog(#"not error");
[self publishStory];
}
}];
} else {
// If permissions present, publish the story
NSLog(#"In else condition");
[self publishStory];
}
}
this is too much code for , "as ios 6 contains integrated facebook in settings."
But I want to post like twitter integration in ios.How can we do that
There are two ways for posting.
1)Post using FBNativeDialog. (inlcude FacebookSDK.framework)
2)Post via SLComposeViewController.
Which one you want to use is up to you.You need to add three frameworks named AdSupport.framework,Accounts.framework and Social.framework.
For using first one you have to include #import "FacebookSDK/FacebookSDK.h" and code for posting is as follows:
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"" message:#"" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
BOOL displayedNativeDialog = [FBNativeDialogs presentShareDialogModallyFrom:self initialText:#"" image:[UIImage imageNamed:#"iossdk_logo.png"] url:[NSURL URLWithString:#"https://developers.facebook.com/ios"]
handler:^(FBNativeDialogResult result, NSError *error)
{
if (error) {
alert.message=#"Fail posting due to some error!";
[alert show];
/* handle failure */
} else {
if (result == FBNativeDialogResultSucceeded) {
alert.message=#"Posted Successfully!";
[alert show];
/* handle success */
} else {
/* handle user cancel */
}
}}];
if (!displayedNativeDialog) {
/* handle fallback to native dialog */
}
For second one you need #import "Social/Social.h" and the code is as follows:
SLComposeViewController *fbComposer =
[SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeFacebook];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewControllerCompletionHandler __block completionHandler=
^(SLComposeViewControllerResult result){
[fbComposer dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:#"Sent"
message:nil
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles: nil];
[alert show];
}
break;
}};
[fbComposer addImage:[UIImage imageNamed:#"iossdk_logo.png"]];
[fbComposer setInitialText:#"The initial text you want to send"];
[fbComposer addURL:[NSURL URLWithString:#"https://developers.facebook.com/ios"]];
[fbComposer setCompletionHandler:completionHandler];
[self presentViewController:fbComposer animated:YES completion:nil];
}

Twitter.framework Image and message issue

I'm using Twitter.framework to post images with message to Twitter. But I see images without message in my Twitter account. I use this code
// 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];
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
UIImage *image =[UIImage imageNamed:#"logo"];
TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:[NSDictionary dictionaryWithObjectsAndKeys:#"Hello. This is a tweet.", #"status", #"My message", #"message", nil] requestMethod:TWRequestMethodPOST];
// "http://api.twitter.com/1/statuses/update.json"
[postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:#"media" type:#"multipart/png"];
// Set the account used to post the tweet.
[postRequest setAccount:twitterAccount];
[postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:#"HTTP response status: %i", [urlResponse statusCode]];
NSLog(#"output = %#\n\n", output);
}];
}
}
}];
What I see in twitter:
And when I unroll:
But I don't see a message
Try FOllowing code i m using it and its working fine. Hope it will help you as well
-(IBAction)goTweet:(id)sender{
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:[NSString stringWithFormat:#"YOUR TWEET HERE"]];
[tweetSheet addImage:[UIImage imageNamed:#"site-logo.jpg"]];
[self presentModalViewController:tweetSheet animated:YES];
[shareLoading stopAnimating];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Twitter"
message:#"Sorry, you can't send a Tweet yet, please make sure you are connected to the internet and have at least one Twitter account set up on your phone in Settings --> Twitter."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
I fount out, how to send text with image:
Add this code
[postRequest addMultiPartData:[#"This photo is taken with my iPhone using nPassword!" dataUsingEncoding:NSUTF8StringEncoding] withName:#"status" type:#"multipart/form-data"];