I have seen this format (Image shown below) of share option in most of the iOS applications that support iOS 7.
Is there a default code/framework available to implement this share option as it is shown in the image below?
What you are looking for is the UIActivityViewController.
Since you asked a general question I can't do more than give you a link to the documentation
In addition to the accepted answer, a small piece of example code
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Call shareText, leave the things that you don't want to share at nil.
[self shareText:#"Hello world" andImage:nil andUrl:nil];
The Controller in the image you posted is the UIActivitiyViewController this is a link to the class documentation
some good example code:
How to display the default iOS 6 share action sheet with available share options?
I know this question is particular to iOS 7, and the code example specifies iOS 6, but AFAICT they are very similar one might find the example code as helpful as I did.
UIActivityViewController is what you are looking for.
You can specify either the items or the applications
UIActivityViewController *actCont = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
Just use following code for Default Sharing. You can able to add more items into shareItems array as per your requirement.
NSMutableArray *shareItems = [[NSMutableArray alloc] initWithObjects:
#"Hello",
[UIImage imageNamed:#"your_image.png"],
#"http://google.com/", nil];
[self shareItemToOtherApp:shareItems];
Following method is for default sharing Text or Image into other Apps:-
-(void)shareItemToOtherApp:(NSMutableArray *)shareItems{
UIActivityViewController *shareController = [[UIActivityViewController alloc]
initWithActivityItems: shareItems applicationActivities :nil];
[shareController setValue:#"Sharing" forKey:#"subject"];
shareController.excludedActivityTypes = #[UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
shareController.completionHandler = ^(NSString *activityType, BOOL completed)
{
//NSLog(#" activityType: %#", activityType);
//NSLog(#" completed: %i", completed);
};
[self presentViewController: shareController animated: YES completion: nil];
}
If you want to make Custom Sharing sheet then use following code. For this, you have to import <Social/Social.h> framework.
-(void)shareOnFacebook:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *faceSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// NSLog(#"%#", messageField.text);//This returns the appropriate string
[faceSheet setInitialText:#"Hellooooooo"];
//The facebook VC appears, but initial text is not set to messageField.text
[self presentViewController:faceSheet animated:YES completion:nil];
}
else
{
NSLog(#"Please first install Application and login in Facebook");
}
}
-(void)shareOnTwitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Hello"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else{
NSLog(#"Please first install Application and login in Twitter");
}
}
Hope, this is what you're looking for. Any concern get back to me. :)
Related
Please Help us..
We want to share projects (i.e. Its Images, text and also my app url).
But i can't get SDK to share all on whatsApp, facebook, twitter, mails and messages. same as android developer using Intent called ACTION_SEND
Try this -
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(NSURL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Call shareText like this -
[self shareText:#"Your text" andImage:nil andUrl:nil];
When i share a video using the UIActivityViewController, the twitter post does not show the video. However, if it take the same video and post it using the twitter app, the video shows and plays embedded in the post.
I am using this code:
MyActivityItemProvider * videoItem = [[MyActivityItemProvider alloc] initWithPlaceholderItem:#""];
NSArray * activityItems = [NSArray arrayWithObjects:
videoItem,
nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
[self presentViewController:activityViewController animated:YES completion:nil];
And in MyActivityItemProvider, I have this:
#implementation MyActivityItemProvider
- (MyActivityItemProvider * )initWithPlaceholderItem:(NSString *)placeholderItem
{
self = [super initWithPlaceholderItem:placeholderItem];
return self;
}
- (id)item
{
NSString * path = [[NSBundle mainBundle] pathForResource:#"video_name" ofType:#"mp4"];
NSURL * fileURL = [NSURL fileURLWithPath:path];
return fileURL;
}
#end
Is it possible for a twitter video post to have the video embedded in it (as if I posted it using the twitter app) when posting using the UIActivityViewController?? Any suggestions on how to achieve this (what it looks) rather simple task?
The video is .mp4 and it is 3.1 MB (like I said, appears to post just fine using the Twitter app and I can send the video via txt message fine).
Video cannot be shared with UIActivityViewController.
Thanks to the uploading media new API, you can share video to twitter by it. And I have tried it, it works.
Please check this: https://github.com/liu044100/SocialVideoHelper
You just need to call this class method.
+(void)uploadTwitterVideo:(NSData*)videoData comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion;
Try this :
+ (void)shareOnTwiiterFromView:(UIViewController *)vc userTitle:(NSString *)title shareURL:(NSString *)urlString
{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:
SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result)
{
switch(result)
{
case SLComposeViewControllerResultCancelled:
break;
case SLComposeViewControllerResultDone:
break;
}
};
NSString *twTitle = [NSString stringWithFormat:#"%# %#",title,#"Your App Name"];
[tweetSheet setInitialText:twTitle];
if (![tweetSheet addURL:[NSURL URLWithString:urlString]])
{
NSLog(#"Unable to add the URL!");
}
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
{
UIPresentationController *control = tweetSheet.presentationController;
[tweetSheet setModalPresentationStyle:UIModalPresentationFormSheet];
[vc presentViewController:control.presentedViewController animated:YES completion:^
{
NSLog(#"Tweet sheet has been presented.");
}];
}
else
{
[vc presentViewController:tweetSheet animated:NO completion:^
{
NSLog(#"Tweet sheet has been presented.");
}];
}
}
else
{
[APP_DEL showErrorAlert:#"No Twitter Account" description:#"There are no twitter accounts configured. You can add or create a Twitter account in Settings."];
}
}
I am trying to send user location to facebook and whatsapp from my app by UIActivityViewController.
- (IBAction)shareUserLocation:(id)sender {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"http://maps.apple.com/maps?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:shareArray applicationActivities:nil];
controller.excludedActivityTypes = #[];
[self presentViewController:controller animated:YES completion:nil];
}
Its working but opening the location in apple maps.
Is there a way I can open the location in Google maps.
P.S- I have not tried whatsApp i have only tested for facebook.
If you want to open a Google Maps URL in your Google Maps iOS app, you can use comgooglemapsurl://maps.google.com/?q=%f,%f. If you want to show it in browser, you can do https://maps.google.com/?q=%f,%f.
Sample code:
-(void)testURL:(CLLocation*)location {
NSArray *shareArray;
if ([[UIApplication sharedApplication] canOpenURL:[[NSURL alloc] initWithString:#"comgooglemaps://"]]) {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"comgooglemapsurl://maps.google.com/?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
} else {
shareArray = [[NSArray alloc]initWithObjects:[NSURL URLWithString:[NSString stringWithFormat:#"https://maps.google.com/?q=%f,%f",location.coordinate.latitude,location.coordinate.longitude]],nil];
}
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:shareArray applicationActivities:nil];
controller.excludedActivityTypes = #[];
[self presentViewController:controller animated:YES completion:nil];
}
I want to show the Alert message When no mail is configured on the device.But when i click Mail from document interaction it just simply dismiss the controller none of the two following Delegate method will call.Please refer Image for better understanding.
Please help. Thanks in Advance
- (void)openAppList:(FileInfo *)fileinfo {
NSURL *fileURL = [NSURL fileURLWithPath:fileinfo.fullName];
UIDocumentInteractionController *interactionController = [UIDocumentInteractionController interactionControllerWithURL:fileURL];
[interactionController retain];
interactionController.delegate = self;
BOOL present = [interactionController presentOptionsMenuFromRect:CGRectZero inView:self.tabBarController.view animated:YES];
if (!present) {
[MainteOrErrorDialog initWithErrorCode:kAlertNotOpenInFileId filename:fileInfo.filename target:nil action:nil];
} else {
[interactionController retain];
}
}
#pragma UIDocumentInteractionDelegate
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
willBeginSendingToApplication:(NSString *)application
{
}
- (void)documentInteractionController:(UIDocumentInteractionController *)controller
didEndSendingToApplication:(NSString *)application
{
}
Unfortunately, you can't really interact with the Mail delegates through UIDocumentInteractionController. UIDocumentInteractionController is extremely limited to what you can do with properties and attributes of all apps it supports, you can't even set a mails message body to a custom message. In fact, documentInteractionController:canPerformAction: is deprecated in iOS 6.0 because Apple was moving towards UIActivityViewController. They're own deprecated statement is :
Apps should use UIActivityViewController for actions
So alternatively, with UIActivityViewController you can.
A work around for this, implement UIActivityViewController, they both support the same apps and check if a user can send emails, if not, exclude Mail from the menu :
if ([MFMailComposeViewController canSendMail]) {
NSArray *shareItems;
shareItems = #[[Config emailMessage], snapshot]; //emailMessage is an NSString containing the body of the mail or mms message located in a different VC
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
[activityController setCompletionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)^(NSString *string, BOOL completed) {
}
[self presentViewController:activityController animated:YES completion:nil];
} else {
NSArray *shareItems;
shareItems = #[[Config emailMessage], snapshot]; //emailMessage is an NSString containing the body of the mail or mms message located in a different VC
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:shareItems applicationActivities:nil];
activityController.excludedActivityTypes = #[UIActivityTypeMail];
[activityController setCompletionWithItemsHandler:(UIActivityViewControllerCompletionWithItemsHandler)^(NSString *string, BOOL completed) {
}
[self presentViewController:activityController animated:YES completion:nil];
}
Some apps show a default action sheet in iOS 6 with sharing options.
Social Framework only has two classes, one for composing and one for request.
What I found though is about composing for a particular service with SLComposeViewController and before showing this I must query by hand if the service is available. And then I also have to create my own action sheet with own icons.
How do those apps show this default share options action sheet in iOS 6? Or are they using an open source framework?
The UIActivityViewController stated in the other answer makes this trivial. All you have to do is specify the text/image/URL that you want to share and present the activity view controller modally and iOS will automatically display all applicable sharing services. Examples:
Objective-C
- (void)shareText:(NSString *)text andImage:(UIImage *)image andUrl:(URL *)url
{
NSMutableArray *sharingItems = [NSMutableArray new];
if (text) {
[sharingItems addObject:text];
}
if (image) {
[sharingItems addObject:image];
}
if (url) {
[sharingItems addObject:url];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];
}
Swift
func share(sharingText: String?, sharingImage: UIImage?, sharingURL: URL?) {
let sharingItems:[AnyObject?] = [
sharingText as AnyObject,
sharingImage as AnyObject,
sharingURL as AnyObject
]
let activityViewController = UIActivityViewController(activityItems: sharingItems.compactMap({$0}), applicationActivities: nil)
if UIDevice.current.userInterfaceIdiom == .pad {
activityViewController.popoverPresentationController?.sourceView = view
}
present(activityViewController, animated: true, completion: nil)
}
Add this to use the UIActivityViewController.
-(IBAction)shareButtonPressed:(id)sender {
NSLog(#"shareButton pressed");
NSString *texttoshare = _txt; //this is your text string to share
UIImage *imagetoshare = _img; //this is your image to share
NSArray *activityItems = #[texttoshare, imagetoshare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVC.excludedActivityTypes = #[UIActivityTypeAssignToContact, UIActivityTypePrint];
[self presentViewController:activityVC animated:TRUE completion:nil];
}