How to post Facebook Feed on Friends wall from IOS application?
Social Framework is very uch useful for sharing and posting in
Fb,Twitter .
Here in the dialog it will ask for privacy we can set that to friends/Piblic/Me
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
// Initialize Compose View Controller
SLComposeViewController *vc = nil;
vc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// Configure Compose View Controller
[vc setInitialText:self.captionTextField.text];
[vc addImage:self.image];
[vc setCompletionHandler:^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
[[[UIAlertView alloc] initWithTitle:#"Congratulations" message:#"Your post is successfully Posted" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}else{
}
}];
// Present Compose View Controller
[self presentViewController:vc animated:YES completion:nil];
} else {
NSString *message = #"It seems that we cannot talk to Facebook at the moment or you have not yet added your Facebook account to this device. Go to the Settings application to add your Facebook account to this device.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
Via Graph API you cannot post to the friend's wall, it was deprecated.
You can use Feed Dialog to invoke the dialog and user can post the feed to any friend, here's the code:
NSMutableDictionary *params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"An example parameter", #"description",
#"https://developers.facebook.com/ios", #"link",
nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {}
];
Check documentation for more details and list of parameters available.
Related
Hello I am using parse to help with the backend of my iOS app and I created a button that will save my text in my textfields and upload it to the cloud when I click a button.
The code for the button and the alert that pops up is as follows
- (IBAction)savebutton:(id)sender {
// Create PFObject with recipe information
PFObject *recipe = [PFObject objectWithClassName:#"GolfTimes"];
[recipe setObject:_nameTextField.text forKey:#"Name"];
[recipe setObject:_prepTimeTextField.text forKey:#"MemberNumber"];
[recipe setObject:_NumberOfGolfers.text forKey:#"NumberOfGolfers"];
[recipe setObject:_RequestedTime.text forKey:#"RequestedTime"];
NSArray *ingredients = [_ingredientsTextField.text componentsSeparatedByString: #","];
[recipe setObject:ingredients forKey:#"Telephone"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Sending...";
[hud show:YES];
// Upload recipe to Parse
[recipe saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[hud hide:NO];
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Request Sent" message:#"We will get back to you with confirmation." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
// Notify table view to reload the recipes from Parse cloud
[[NSNotificationCenter defaultCenter] postNotificationName:#"refreshTable" object:self];
// Dismiss the controller
[self dismissViewControllerAnimated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];}
Although the data does send properly and the view controller does close as I made it do, the Alert seems to freeze the app and I cannot click the "Ok" most of the time. When I do click the "OK" button the app simulator shuts down.
Any suggestions or help?
I believe you need to dispatch the UIAlertView to the main thread like so when you want to show them: EDIT: You can't have the delegate of the UIAlertView be on the UIViewController you just dismissed! That's why it's crashing; setting the delegate to: nil should fix that.
dispatch_async(dispatch_get_main_queue(),^
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Request Sent" message:#"We will get back to you with confirmation." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[[NSNotificationCenter defaultCenter] postNotificationName:#"refreshTable" object:self];
[self dismissViewControllerAnimated:YES completion:nil];
});
I have iphone book app like all other text when tap and hold a specific words you will see a (copy,define) menu, I want to add another item which is (share) when the user tap it the sharing action sheet will pop up and the user can share that particular words on (Facebook , Twitter , mail or text).
Please help me with details, any help will be appreciated.
social framework:
share Fb or Twitter
- (IBAction)socialSheet:(id)sender {
// create Facebook controller
SLComposeViewController *socialController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// add initial text
[socialController setInitialText:#"Hello Facebook!"];
// add an image for you optional
[socialController addImage:[UIImage imageNamed:#"picture.jpg"]];
// add a URL for you optional
[socialController addURL:[NSURL URLWithString:#"http://wpguru.co.uk"]];
// present controller
[self presentViewController:socialController animated:YES completion:nil];
}
Facebook Api
-(void) postWithText: (NSString*) message_text
ImageName: (NSString*) image_name
URL: (NSString*) url
Caption: (NSString*) caption
Name: (NSString*) name
andDescription: (NSString*) description
{
NSMutableDictionary* params = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
url, #"link",
name, #"name",
caption, #"caption",
description, #"description",
message, #"message_name",
UIImagePNGRepresentation([UIImage imageNamed: image_name]), #"picture",
nil];
if ([FBSession.activeSession.permissions indexOfObject:#"publish_actions"] == NSNotFound)
{
// No permissions found in session, ask for it
[FBSession.activeSession requestNewPublishPermissions: [NSArray arrayWithObject:#"publish_actions"]
defaultAudience: FBSessionDefaultAudienceFriends
completionHandler: ^(FBSession *session, NSError *error)
{
if (!error)
{
// If permissions granted and not already posting then publish the story
if (!m_postingInProgress)
{
[self postToWall: params];
}
}
}];
}
else
{
// If permissions present and not already posting then publish the story
if (!m_postingInProgress)
{
[self postToWall: params];
}
}
}
-(void) postToWall: (NSMutableDictionary*) params
{
m_postingInProgress = YES; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Post Failed"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
m_postingInProgress = NO;
}];
}
In my project i am having 5 levels. In First and second level i can able to move and shoot at a time. When completing 2nd level i want to share it with Facebook. After facebook share i cant able to move and shoot at a time. Only one process is working(Either shoot or move).
What i want to do for solve this problem.
My coding is here:
{
UIViewController*v=[[UIViewController alloc]init];
[[[CCDirector sharedDirector]openGLView]addSubview:v.view];
SLComposeViewController*mySLComposerSheet;
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
NSString *strg=[NSString stringWithFormat:#" "];
NSString *bodyStr=[NSString stringWithFormat:#"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"!\n;
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:bodyStr]; //the message you want to post
[mySLComposerSheet addImage:[UIImage imageNamed:#"my.jpg"]]; //an image you could post
NSURL *url = [NSURL URLWithString:#"https:example.com"];
[mySLComposerSheet addURL:url];
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output,*head;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"FREE levels NOT unlocked";
head=#"Facebook Share Unsuccessfull";
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:head message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
[self reload];
break;
case SLComposeViewControllerResultDone:
output = #"Extra FREE levels successfully unlocked";
head=#"Successfull";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:head message:output delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
alert.tag=22;
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
You are presenting the ViewController mySLComposerSheet
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];
see the post https://stackoverflow.com/a/12651085/2787026
I'm providing a facebook share option on my iPhone app screen and need to share some images and URL on the user's facebook profile . I know there are several third party frameworks like ShareKit and other features like facebook sdk for iOS, but which is the best way I could so the same and i need to get the share button with the number of shares until now as shown in the image below
and on clicking should redirect to authentication.
And should be compatible with iOS5 and above, hope my question is clear.
Thanks in advance
Solved the question myself through research
I used Facebook Graph API since my application needs support from iOS4+, would have been simple if it was for iOS6 as iOS6 has Facebook integration and needs just a few lines of code to implement it.
Imported Graph APi files and added the following code:
#synthesize facebook;
//login with facebook
- (IBAction) facebookButtonPressed {
if (!facebook || ![facebook isSessionValid]) {
self.facebook = [[[Facebook alloc] init] autorelease];
NSArray *perms = [NSArray arrayWithObjects: #"read_stream", #"create_note", nil];
[facebook authorize:FACEBOOK_API_KEY permissions:perms delegate:self];
}
else {
[self fbDidLogin];
}
}
//upload image once you're logged in
-(void) fbDidLogin {
[self fbUploadImage];
}
- (void) fbUploadImage
{
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
resultImage, #"picture",
nil];
[facebook requestWithMethodName: #"photos.upload"
andParams: params
andHttpMethod: #"POST"
andDelegate: self];
self.currentAlertView = [[[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Facebook", #"")
message:NSLocalizedString(#"Uploading to Facebook.", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles: nil] autorelease];
[self.currentAlertView show];
}
//facebook error
- (void)request:(FBRequest*)request didFailWithError:(NSError*)error
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Error", #"")
message:NSLocalizedString(#"Facebook error message", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[myAlert show];
[myAlert release];
}
//facebook success
- (void)request:(FBRequest*)request didLoad:(id)result
{
[self.currentAlertView dismissWithClickedButtonIndex:0 animated:YES];
self.currentAlertView = nil;
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:NSLocalizedString(#"Facebook", #"")
message:NSLocalizedString(#"Uploaded to Facebook message", #"")
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"OK", nil];
[myAlert show];
[myAlert release];
}
And to get the share count of a particular URL in facebook,
use
http://graph.facebook.com/?id=url
But this returns the count of all likes,shares and posts altogether, so better find some alternatives to do so.
Hope this helps
Thanks
I'm creating an application that has integration with Twitter .. and wanted to know what have to do to create a button to add an image of the device in the tweet.and also wanted to know which framework to use it and also where the code
You can just use the twitter framework, it's included since iOS 5
And the code i use:
- (IBAction)twitter:(id)sender {
if([TWTweetComposeViewController canSendTweet])
{
NSLog(#"Ready to Tweet.");
TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
[tweetComposer setInitialText:[NSString stringWithFormat:#"My message"]];
[tweetComposer addImage:[UIImage imageNamed:#"114x114"]];
[tweetComposer addURL:[NSURL URLWithString:#"http://myPage"]];
tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
if(result == TWTweetComposeViewControllerResultDone){
NSLog(#"Tweeted.");
} else if(result == TWTweetComposeViewControllerResultCancelled) {
NSLog(#"Cancelled.");
}
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
};
[self presentModalViewController:tweetComposer animated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oops"
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:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}