I need to implement a "share as..." function in iOS.
For example a button names "share as..." and popup a dialog which includes items like Email, SMS, Facebook, Twitter.
I wonder if there have a standard dialog do this job.
After searching, I found a way seems quite "standard" way in iOS6 by using UIActivityViewController.
Following is the description from developer.apple.com:
The UIActivityViewController class is a standard view controller that you can use to offer various services from your application. The system provides several standard services, such as copying items to the pasteboard, posting content to social media sites, sending items via email or SMS, and more. Apps can also define custom services.
And following is a dialog I managed to produce by using UIActivityViewController
And following is the source code I use:
NSArray *activityItems = [NSArray arrayWithObjects: share_text.text, share_image.image , nil];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
In your ViewController.m :
NSString *message = #"The Flyer http://flyerdream.tumblr.com";
UIImage *image = [UIImage imageNamed:#"flyer"];
NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:message, image, nil];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:arrayOfActivityItems applicationActivities:nil];
[self.navigationController presentViewController:activityVC animated:YES completion:nil];
After using UIActivityViewController, I believe it is a much better solution than what I've posted below. I've left my answer because the individual sharing methods may be of use to someone.
This is how I've implemented share features in my projects:
Email: Use MFMailComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/MessageUI/Reference/MFMailComposeViewController_class/Reference/Reference.html
Facebook & Twitter: Use SLComposeViewController. See documentation here: http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html
Example:
// prepare and present SLComposeViewController if service is available
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *socialPost = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[socialPost setInitialText:#"TEXT FOR POST"];
[socialPost addImage:[UIImage imageNamed:#"yourImage.png"]];
[self presentViewController:socialPost animated:YES completion:nil];
}
// set up a completion handler
[socialPost setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultDone:
// post was completed
break;
case SLComposeViewControllerResultCancelled:
// post was cancelled
break;
default:
break;
}
}
Note: If you want to post for Twitter, replace SLServiceTypeFacebook with SLServiceTypeTwitter in the code sample above.
Related
Please, can you tell me if I'm doing mistakes?
NSString *sharedMsg=[NSString stringWithFormat:#"Hello world"];
UIImage* sharedImg=[UIImage imageNamed:#"image"];
NSArray* sharedObjects=[NSArray arrayWithObjects:sharedMsg, sharedImg, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
At runtime when I select the icon of Facebook the text is missing, the image is correctly displayed.
My xcode is 6.3.1, tested on iPad.
It seems that a recent update to the Facebook application has replaced the in-built Facebook share activity with one that ignores status text - If you remove the Facebook app from your device the text will be displayed using the code you have. If you have the Facebook app installed you get images, URLs but not the text
Facebook's policies don't allow you to pre-populate status messages and require all content to be user generated - while I understand the intention behind this, I personally think it is kind of stupid in many cases - For example in my game I want to pre-populate the user's score, but now I can't, so the user is presented with an empty dialog box. I will probably simply remove the Facebook sharing option as no-one will ever use it now.
This response from Facebook confirms that the behaviour is by design
Here is the answer in Swift. It may not help your problem, but it might be helpful with someone looking for this title. Just create a button and it's action. And install the button's action like this.
Note: You have to log in your facebook or twitter by going to setting. Then do like this.
#IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "My beautiful photo! <3 <3"
let image: UIImage = UIImage(named: "yourImageNameHere")
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
NSString* text=#"Hello world";
NSURL *myWebsite = [NSURL URLWithString:#"http://www.website.com/"];
// UIImage * myImage =[UIImage imageNamed:#"myImage.png"];
NSArray* sharedObjects=#[text,myWebsite];
UIActivityViewController * activityViewController=[[UIActivityViewController alloc]initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
I am using UIActivityViewController to share some data on different social media options.
I can see Facebook but i am unable to see Facebook messenger. Does any body have any idea why i doesn't appear in the options list?
I have both Facebook and Facebook messenger installed in my iPhone.
UIActivityViewController * shareViewController = [[UIActivityViewController alloc] initWithActivityItems:#[activity] applicationActivities:nil];
if(self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular && self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular) {
shareViewController.popoverPresentationController.barButtonItem = self.shareButton;
}
[self presentViewController:shareViewController animated:YES completion:^{
}];
Any help will be great thanks
Please, can you tell me if I'm doing mistakes?
NSString *sharedMsg=[NSString stringWithFormat:#"Hello world"];
UIImage* sharedImg=[UIImage imageNamed:#"image"];
NSArray* sharedObjects=[NSArray arrayWithObjects:sharedMsg, sharedImg, nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
At runtime when I select the icon of Facebook the text is missing, the image is correctly displayed.
My xcode is 6.3.1, tested on iPad.
It seems that a recent update to the Facebook application has replaced the in-built Facebook share activity with one that ignores status text - If you remove the Facebook app from your device the text will be displayed using the code you have. If you have the Facebook app installed you get images, URLs but not the text
Facebook's policies don't allow you to pre-populate status messages and require all content to be user generated - while I understand the intention behind this, I personally think it is kind of stupid in many cases - For example in my game I want to pre-populate the user's score, but now I can't, so the user is presented with an empty dialog box. I will probably simply remove the Facebook sharing option as no-one will ever use it now.
This response from Facebook confirms that the behaviour is by design
Here is the answer in Swift. It may not help your problem, but it might be helpful with someone looking for this title. Just create a button and it's action. And install the button's action like this.
Note: You have to log in your facebook or twitter by going to setting. Then do like this.
#IBAction func onShareTouched(sender: AnyObject) {
print("share")
let myShare = "My beautiful photo! <3 <3"
let image: UIImage = UIImage(named: "yourImageNameHere")
let shareVC: UIActivityViewController = UIActivityViewController(activityItems: [(image), myShare], applicationActivities: nil)
self.presentViewController(shareVC, animated: true, completion: nil)
}
NSString* text=#"Hello world";
NSURL *myWebsite = [NSURL URLWithString:#"http://www.website.com/"];
// UIImage * myImage =[UIImage imageNamed:#"myImage.png"];
NSArray* sharedObjects=#[text,myWebsite];
UIActivityViewController * activityViewController=[[UIActivityViewController alloc]initWithActivityItems:sharedObjects applicationActivities:nil];
activityViewController.popoverPresentationController.sourceView = self.view;
[self presentViewController:activityViewController animated:YES completion:nil];
I'm looking for a drop in solution to add a Facebook and Twitter share button option to my app. I've googled this of course, and found shareKit but it's non-ARC so just throws lots of errors, which is of no use to me. Any other options? they need to be ARC compatible.
I want a SDK that I can drop in, throw in the App ID's and be done.
If you want simple sharing, the following can be used for Twitter and Facebook:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[slComposeViewController setInitialText:#"Your text"];
[slComposeViewController addImage:[UIImage imageNamed:#"NameOfImage"]];
[self presentViewController:slComposeViewController animated:YES completion:nil];
} else {
//Show alert or in some way handle the fact that the device does not support this feature
}
For Facebook, simply replace SLServiceTypeTwitter with SLServiceTypeFacebook. The "Social" framework must be imported :-)
You can use UIActivityViewController:
NSArray * activityItems = activityItems = #[#"title", [NSURL URLWithString:#"urlstring"]];
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
References : https://developer.apple.com/library/ios/documentation/uikit/reference/UIActivityViewController_Class/Reference/Reference.html
Yes use the native iOS sharing dialog: UIActivityViewController
I have done all steps that are explained here: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/3.1/
I have tested the demo projects and fail the same as on my project. (you can test it on HelloFacebookSample project).
When you don't have any Facebook account configured, you can't share something on facebook, or upload an image etc, (the same as twitter framework). So the frameworks shows you a message that tells this:
There are no Facebook accounts configured. You can add or create a Facebook account in Settings.
You click on settings but the only thing that happends is that this dialog is hided, but the framework doesn't open the Settings tab (as work on for example on the twitter framework).
Does anyone know how to solve this problem?
Thanks in advance.
Please use SLComposeViewController class for Facebook sharing, setting button work for you.
if([SLComposeViewController class])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:text];
[controller addURL:[NSURL URLWithString:encod]];
[self presentViewController:controller animated:YES completion:Nil];
//[message release];
SLComposeViewControllerCompletionHandler myBlock = ^(SLComposeViewControllerResult result)
{
NSString *output= nil;
switch (result)
{
case SLComposeViewControllerResultCancelled:
output= #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output= #"Post Succesfull";
[self displayText:#"done"];
break;
default:
break;
}
};
controller.completionHandler =myBlock;
}
This code is working for me
Why dont u use UIActivityViewController ?
iOS 6 have the best support with UIActivityViewController
UIActivityViewController *ActivityView = [[UIActivityViewController alloc] initWithActivityItems:Items applicationActivities:nil];
Best Integration for Facebook, Twitter and sms Service