I'm looking to add functionality that when an event happens on my app, it will post to my facebook timeline. That said, I'm trying to get it to look very similar to how when you post a youtube video to the timeline where there is a thumbnail image to the left and a title and subtitle next to it.
Anyone have any ideas how I can accomplish this?
You can't make a post automatically from code, but you can open a system window with predefined message and image attachment.
Sample code:
- (BOOL)postToFacebook:(NSString*)title andDescription:(NSString*)description {
UIWindow *frontWindow = [[UIApplication sharedApplication] keyWindow];
UIViewController *vc = [frontWindow rootViewController];
BOOL success = NO;
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultCancelled) {
} else {
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler = myBlock;
[controller setTitle:title];
[controller setInitialText:description];
[controller addURL:[NSURL URLWithString:#"http://example.com"]];
[controller addImage:[UIImage imageNamed:#"sampleImage.png"]];
[vc presentViewController:controller animated:YES completion:^{
}];
success = YES;
}
return success;
}
Remember to add two frameworks:
Social.framework
Accounts.framework
Related
In my application I used UIActivityViewController to share text in Facebook and Twitter. But how to do different text to share? please help ..thanks in advance
I used this code:
NSString *posturl2=#"twittwrer and facebook ";
UISimpleTextPrintFormatter *printData = [[UISimpleTextPrintFormatter alloc]
initWithText:#"Hi"];
NSArray *Itemsarray = #[posturl2,printData];
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:Itemsarray applicationActivities:nil];
[self presentViewController:activityController
animated:YES completion:nil];
want like this
Import Social Framework
#import <Social/Social.h>
Then just call
- (void)shareOnFacebookWithText:(NSString *)textToShare {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:textToShare];
[self presentViewController:controller animated:YES completion:nil];
}
}
- (void)shareOnTwitterWithText:(NSString *)textToShare {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:textToShare];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
I developed with WinDev Mobile , which generates a project I then opened with XCode to compile .
Only in WM sharing functionality on facebook has not yet been developed, I must to develop in ObjC
I started with the code:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] ;
[controllerSLC setInitialText:myMessage];
[controllerSLC addURL:[NSURL URLWithString:myUrl]];
[controllerSLC addImage:[UIImage imageNamed:myImage]];
// NSLog (#"% #", [UIApplication sharedApplication] keyWindow.rootViewController.)
[self presentViewController:controllerSLC animated:YES completion:Nil];
controllerSLC.completionHandler = ^(SLComposeViewControllerResult result) {
NSString *output = nil;
switch(result) {
SLComposeViewControllerResultDone box:
output = # "Your tweet has-been sent" ;
status = 2;
// NSLog (#" SENT ");
break;
SLComposeViewControllerResultCancelled box:
output = #"Your tweet has-been canceled ";
status = 3;
// NSLog (#" canceled ");
break;
default:
break;
}
[controllerSLC dismissViewControllerAnimated:YES completion:nil] ;
};
}
I only got an error :
Warning Attempt to present <SLComposeViewController > on <CFenPrincipaleViewController> Whose view is not in the window hierarchy !
I added the code :
UIViewController *activeController = [UIApplication sharedApplication] keyWindow.rootViewController. ;
if ([ activeController isKindOfClass:[UINavigationController class]]) {
activeController = [(UINavigationController *)activeController visibleViewController] ;
}
[activeController presentViewController:controllerSLC animated:YES completion:Nil];
It works on the window home application, I have no error .
But when I call the same function on another window , the error returns ....
Thank you in advance for your help
I had the same issue...give this a shot!
First get the root view controller....
-(UIViewController*) getRootViewController {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}
Now instead of activeController = [(UINavigationController *)activeController visibleViewController] ;
Do this:
UIViewController* activeController = [self getRootViewController];
Now your last line [activeController presentViewController:controllerSLC animated:YES completion:Nil]; should work.
Hope this helps.
I'm building an IOS7 Native app on behalf of a client - its for Fitness Instructors.
The brief requests that the clients can socially share progress updates - which include a link to the instructors site to help promotion, for example - 'Joe ran 3000 miles with the help of Debbie Personal Trainer' and ideally a little pic of the trainer.
I've looked at the SLComposeViewController and can easily create the tweet string but I don't know how to add a URL and image to this - does anyone know if its possible?
Import framework <Twitter/Twitter.h> and <Social/Social.h>.
-(void)sendFacebook:(id)sender {
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[composeController setInitialText:#"look me"];
[composeController addImage:[UIImage imageNamed:#"image.png"]];
[composeController addURL: [NSURL URLWithString:#"http://www.apple.com"]];
[self presentViewController:composeController animated:YES completion:nil];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"delete");
} else {
NSLog(#"post");
}
// [composeController dismissViewControllerAnimated:YES completion:Nil];
};
composeController.completionHandler =myBlock;
}
- (void)sendTwitter:(id)sender {
SLComposeViewController *composeController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[composeController setInitialText:#"look me"];
[composeController addImage:[UIImage imageNamed:#"image.png"]];
[composeController addURL: [NSURL URLWithString:
#"http://www.apple.com"]];
[self presentViewController:composeController
animated:YES completion:nil];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"delete");
} else {
NSLog(#"post");
}
// [composeController dismissViewControllerAnimated:YES completion:Nil];
};
composeController.completionHandler =myBlock;
}
This is almost the same answer as llario, but follows Apple docs instructions and employs defensive coding with some additional error checking.
#import <Social/Social.h>
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
if (composeViewController) {
[composeViewController addImage:[UIImage imageNamed:#"MyImage"]];
[composeViewController addURL:[NSURL URLWithString:#"http://www.google.com"]];
NSString *initialTextString = #"Check out this Tweet!";
[composeViewController setInitialText:initialTextString];
[composeViewController setCompletionHandler:^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultDone) {
NSLog(#"Posted");
} else if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Post Cancelled");
} else {
NSLog(#"Post Failed");
}
}];
[self presentViewController:composeViewController animated:YES completion:nil];
}
}
I am pretty new at incorporating Facebook into iOS apps. I have read 3 books, studied the Facebook examples, and studies the Facebook tutorials, and I don't understand Facebook integration. I understand how to register the app with Facebook, but what I don't understand is the code to post text and a picture to a user's news feed. I have tried to understand Facebook's tutorials, but they are all based on Xcode and iOS versions which are not current. Facebook's tutorials are also not consistent (e.g., the login tutorial does not match variables with the posting tutorial, etc.). I do understand how to add text to "initialText", but it's against Facebook policy to provide default text in this variable. Can anyone explain how to publish text and a picture to a user's news feed in Xcode?
Thanx!
Have you tried this one?
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"ResultCancelled");
} else
{
NSLog(#"ResultSuccess");
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
[controller setInitialText:#"Learn iOS6 Social Framework integration"];
[controller addURL:[NSURL URLWithString:#"http://google.com"]];
[controller addImage:[UIImage imageNamed:#"myimage.jpeg"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
//NSLog(#"UnAvailable");
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:#"Facebook"
message:[NSStringstringWithFormat:#"The application cannot post message at the moment. Please login under setting section with your Facebook account."]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
import social framework in your project.
- (void)ShareOnFB
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultCancelled) {
NSLog(#"Cancelled");
} else
{
NSLog(#"Done");
//Adding the Text to the facebook post value from iOS
}
[controller dismissViewControllerAnimated:YES completion:Nil];
};
controller.completionHandler =myBlock;
NSString *fb=[NSString stringWithFormat:#"you can add your text hereand change it as and when needed"];
[controller setInitialText:fb];
//Adding the Image to the facebook post value from iOS
[controller addImage:yourImageView.image];
[self presentViewController:controller animated:YES completion:Nil];
}
Im trying to add caption/meta data from the link that is attached from the the NSURL.
I followed this article: http://www.mobile.safilsunny.com/integrating-facebook-ios-6/
There are a picture attached to the article at the bottom, that includes the meta data from its link. ( Windows Phone 8 tutorials... etc).
When sharing links on Facebook web, it will automatically give you this meta data so my question is how can i achieve this from iOS?
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:#"google.se"];
[controller addURL:[NSURL URLWithString:#"http://www.google.se"]];
[controller addImage:[UIImage imageNamed:#"icon.png"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else{
NSLog(#"UnAvailable");
}
EDIT #1: I see that the Digg app do this.
Use Facebook SDK it is not possible now using SLComposeViewController