Can I use UIActivityViewController to share on Facebook ? - facebook

I am trying to use UIActivityViewController to share on facebook.
When I am using the
UIImage* image = [UIImage imageNamed:#"image1.png"];
NSArray* dataToShare = #[#"test",image];
UIActivityViewController* activityViewController =
[[UIActivityViewController alloc] initWithActivityItems:dataToShare
applicationActivities:nil];
activityViewController.excludedActivityTypes = #[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll];
I can't see the "Share on Facebook" icon although I didn't exclude "UIActivityTypePostToFacebook". isn't there a predefined UIActivity for facebook ?
Thanks

you must be logged in your iphone settings:
Settings -> facebook

Try this: if you want only facebook icon to share:
-(IBAction)shareButtonPressed:(id)sender { NSLog(#"shareButton pressed");
NSString *texttoshare = _txt;
UIImage *imagetoshare = _img;
NSArray *activityItems = #[texttoshare, imagetoshare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypePostToTwitter, UIActivityTypePostToWeibo];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
//in the list of excluded activity type insert any other kind of activitytypes you want to exclude

Related

Issue posting file url using UIActivityViewController

I am trying to share my app document (file url ) using UIActivityViewController. When activityviewcontroller pop up (testing on iPad device), I get only iMessage and Mail options. I DO NOT GET OTHER OPTIONS like Facebook, Twitter, Copy, Print, etc. Why? Please help me. I need Facebook, twitter options also. File type which I am sharing using NSURL is .txt
Note: I am already signed in to my Facebook, twitter account in iOS settings.
Source code which I am using:
NSURL *url = [NSURL fileURLWithPath:self.path];
NSArray *objectsToShare = #[url];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
[APP_VIEW_CONTROLLER presentViewController:activityViewController animated:YES completion:nil];
}
else
{
if (![self.activityPopover isPopoverVisible]) {
self.activityPopover = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[self.activityPopover presentPopoverFromRect:pathViewController.pathViewWrapper.pathView.bounds inView:pathViewController.pathViewWrapper.pathView permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
//Dismiss if the button is tapped while pop over is visible
[self.activityPopover dismissPopoverAnimated:YES];
}
}
It is indeed mysterious what shows up in the UIActivityViewController. Regarding Facebook and Twitter 1) it likely needs to be an http or https URL (not a file URL), 2) make sure those services are configured on the simulator or iOS device you're using.
For posting to Facebook. The file can be a file stored on the device locally. However, it needs to be converted to MP4. By default, videos taken by device camera are .MOV files. Once you convert to .MP4 if you provide the local file location, UIActivityController will share to Facebook.
I got FB to work, I'm working on Twitter now. I will edit my answer and provide details when I'm done.
Here is some code:
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:activities];
activityController.excludedActivityTypes = #[ UIActivityTypeMail,
UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypeCopyToPasteboard,
UIActivityTypeAddToReadingList,
UIActivityTypeSaveToCameraRoll];
NSMutableArray *items = [[[Engine sharedEngine].thingsManager getSharingMessageForThingWithId:self.thingId
categoryName:self.category.name
brandName:self.brand.name
andText:message
isVideo:([self.videoURL.absoluteString length] > 0 ? YES : NO)] mutableCopy];
if ([self.videoURL.absoluteString length] > 0) {
//Get the .mp4 converted video url
if (self.awsManager == nil) {
self.awsManager = [[AWSManager alloc] init];
}
NSURL *mp4Url = [self.awsManager convertVideoToMP4:self.videoURL];
//[items addObject:self.videoAmazonPath];
[items addObject:mp4Url];
} else {
[items addObject:self.image];
}
Hope this helps.

iOS UIActivity View Controller: Add To Reading List Button?

Is there a service to be able to add a URL to the iOS Safari's Reading List from in a app.
I would have a url to add and a UIWebView,but I have researched and I can't find anything.
Here is my working UIActivityViewController.
-(IBAction)actionButton:(id)sender;{
NSLog(#"shareButton pressed");
NSURL *URL = [NSURL URLWithString:self.feedItem[#"url"]];//this is your text string
NSArray *activityItems = #[URL];
ARChromeActivity *chromeActivity = [[ARChromeActivity alloc] init];
TUSafariActivity *TUSafari = [[TUSafariActivity alloc] init];
MLCruxActivity *cruxActivity = [[MLCruxActivity alloc] init];
NSArray *applicationActivities = [NSArray arrayWithObjects:TUSafari,chromeActivity,cruxActivity, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities: applicationActivities];
activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:applicationActivities];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
UPDATE: iOS 7 added an API to accomplish this:
#import <SafariServices/SafariServices.h>
SSReadingList * readList = [SSReadingList defaultReadingList];
NSError * error = [NSError new];
BOOL status =[readList addReadingListItemWithURL:[NSURL URLWithString:urlToAdd] title:titleToAdd previewText:previewText error:&error];
if(status)
{
NSLog(#"Added URL");
}
else NSLog(#"Error");
Currently (iOS SDK 6.1) there is no way to add a item to the Reading List from a third party app.
There are some alternatives like Readability you could use.

How to add different text to UIActivityViewController objects

I have implemented a UIActivityViewController, for sharing some information. In this case I have a question: Is it possible to make a different text between the facebook sharing/twitter sharing/ or mail sharing? That the text which is set is different from the others...
A good UIActivityViewController tutorial would be very useful.
My code at the time is, for displaying text and image:
NSString *text = #"Lime Cat";
UIImage *image = [UIImage imageNamed:#"MyApp Icon 512x512.png"];
NSArray *items = [NSArray arrayWithObjects:text,image , nil];
But how can I manage it that the NSString is just for the mail, and make a seperate NSString for the facebook share option?
Any suggestions?
Thanks.
you can go to the link bellow, there is a tutorial that can help you :
https://www.albertopasca.it/whiletrue/objective-c-custom-uiactivityviewcontroller-icons-and-text/
You can have your class conform to the UIActivityItemSource protocol and implement activityViewController:itemForActivityType:. The activityType will be FB, Twitter, Messages app, etc, so you can do a switch on it and return a different object based on the activity.
-(void)ShareImageandText:(UIButton *)sender
{
NSString *texttoshare =#"http://qrs.ly/l851gh4";
UIImage *image = [UIImage imageNamed:#"default"];
NSString *noteStr = [NSString stringWithFormat:#"Please follow this link below to install the Freedom.desi application on your IPhone. %#",texttoshare];
NSURL *url = [NSURL URLWithString:texttoshare];
NSArray *activityItems = #[noteStr,image];
NSLog(#"this %#",activityItems);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[noteStr,image] applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint];
[self shareText:noteStr
andImage:image andUrl:nil];
[self presentViewController:activityVC animated:TRUE completion:nil];
}
//if iPad
else {
// Change Rect to position Popover
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:#[noteStr, url] applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint];
[self shareText:noteStr
andImage:image andUrl:nil];
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityVC];
[popup presentPopoverFromRect:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/4, 0, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
}

Photo Posting in Twitter in iOS5

I am new one to twitter integration in iOS5.
I need to post photos in twitter coming from services.
I have searched many and also practiced sample tutorials on this.
I am getting only photo URL. I need to post photo.
Can anyone help me regarding this?
Thanks in advance.
First you need to save the images and then you can post saved image to twitter
self.shareImg.image = [UIImage imageNamed:#"Your Saved image"];
if ([[[UIDevice currentDevice] systemVersion] floatValue] > 5.0) {
// Create the view controller
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
NSString *str = [NSString stringWithFormat:#"I created this photo http://%#", iTune];
// Optional: set an image, url and initial text
[twitter addImage:self.shareImg.image];
[twitter setInitialText:[NSString stringWithFormat:#"I created this photo using USC Project Trojan App:http://uslofphoto.com. Download http://%#", iTune]];
// Show the controller
[self presentModalViewController:twitter animated:YES];
// Called when the tweet dialog has been closed
twitter.completionHandler = ^(TWTweetComposeViewControllerResult result)
{
NSString *title = #"Tweet Status";
NSString *msg;
if (result == TWTweetComposeViewControllerResultCancelled)
msg = #"Tweet compostion was canceled.";
else if (result == TWTweetComposeViewControllerResultDone)
msg = #"Tweet composition completed.";
// Show alert to see how things went...
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alertView show];
// Dismiss the controller
[self dismissModalViewControllerAnimated:YES];
};
}

How to do wall post on facebook with image stored on iPhone?

I have an application in which i have to post image on Wall of facebook.
Now problem is i used GraphAPI facebook and i have to pass link of photo but i have photo in my iPhone and not on any server.
So how do i post wall with image and other parameters without URL of image?
You may use NSURLConnection ASIHttpRequest library for downloading the images from a url.
You will have to do the following:
1. Create a NSUrlConnection and implement the delegate methods
2. Pass the url in your request and you will get the data in -(void)connectionDidFinishLoading: (NSURLConnection *)connection
3. You can then create a UIImage from this data using UIImage *image = [[UIImage alloc] initWithData:activeDownloadData];
FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
dialog.userMessagePrompt = #"Enter your message:";
NSDictionary *params = nil;
[[FBRequest requestWithDelegate:self] call:#"facebook.photos.upload" params:params dataParam:(NSData*)wallImage];
[dialog show];
Edit:
--
Actually, posting an image to the Facebook wall has to be from the web. So you will have to upload the image first; maybe try a 3rd party service like twitpic or something, return the response url and place it in your params.
--
You can use the following to post with image to Facebook. You can also replace UIImage with a physical URL as well.
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
kAppID, #"app_id",
#"http://bit.ly/dDZQXt", #"link",
#"www.has.to.be.web.based.com", #"picture",
EasyStrings(#"Mobile Profiles for ",device), #"name",
EasyStrings(#"Current Profile: ",currProfile), #"caption",
#"I am using Mobile Profiles for iPhone, iPod touch and iPad.", #"description",
message, #"message",
nil];
[_facebook dialog:#"feed"
andParams:params
andDelegate:self];
What it will look like:
The house icon is loaded from [UIImage imageNamed:#"something.png"].
I modified the facebook demo app. It uses the modal view to select a picture from your phone. You can use this code, granted you must add two buttons (i.e., selectPicture button and a uploadPhoto button) -- also be sure to add to UIViewController in the .h file:
/**
* Upload a photo.
*/
- (IBAction)uploadPhoto:(id)sender {
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, #"picture",
nil];
[_facebook requestWithGraphPath:#"me/photos"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
/**
* Select a photo.
*/
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissModalViewControllerAnimated:YES];
}
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction) selectPicture:(UIButton *)sender;
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
}
Try using BMSocialShare which is a simple lib I wrote. It is exactly doing what you want. Upload some local image (and even open a dialog for the user to add a comment) to the user's wall:
BMFacebookPost *post = [[BMFacebookPost alloc] initWithImage:[UIImage imageNamed:#"image.png"]];
[[BMSocialShare sharedInstance] facebookPublish:post];