In my project,I am using UIActivityViewController to do Facebook,Twitter and email sharing.I want to share separate text and images for facebook,Email and twitter.How can i do that?
-(id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
return _shareText1;
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
return _shareText2;
if ( [activityType isEqualToString:UIActivityTypeMail] )
return _shareText3;
return nil;
}
I used the above code in the subclass of UIActivityItemProvider,and passed the text to be shared from my UIViewcontroller.How to return an image with the text?Is it by returning a NSDictionary?If so,What are the keys?Please Help.
The best way you can create the UIActivity Subclass and use in UIActivityViewController..
-(void)sharePressed:(id)sender
{
GooglePlus *gPlus = [[GooglePlus alloc]init];
FacebookShare *fb =[[FacebookShare alloc]init];
NSString *textToShare = #"Text u want to share";
NSURL *myWebsite = [NSURL URLWithString:#"http://www.mywebsite.com/"];
UIImage *image =[UIImage imageNamed:#"apple.png"];
}
NSArray *objectsToShare = #[textToShare, myWebsite,image];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:
[NSArray arrayWithObjects:gPlus,fb,nil]];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
// NSLog(#" activityType: %#", activityType);
// NSLog(#" completed: %i", completed);
};
NSArray *excludedActivities = #[UIActivityTypePostToTwitter,
UIActivityTypePostToWeibo,
UIActivityTypeMessage,
UIActivityTypePrint, UIActivityTypeCopyToPasteboard,
UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll,
UIActivityTypeAddToReadingList, UIActivityTypePostToFlickr,
UIActivityTypePostToVimeo, UIActivityTypePostToTencentWeibo,UIActivityTypePostToFacebook];
activityVC.excludedActivityTypes = excludedActivities;
[self presentViewController:activityVC animated:YES completion:nil];
// Create the subclass the UIActivity Class using delegates
-(NSString *)activityType
{
return #"GooglePlus";
//CustomActivity
}
-(NSString *)activityTitle
{
return #"GooglePlus ";
// use your custom ActivityTitle
}
-(UIImage *)_activityImage
{
// Note: These images need to have a transparent background and I recommend these sizes:
// iPadShare#2x should be 126 px, iPadShare should be 53 px, iPhoneShare#2x should be 100
// px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
return [UIImage imageNamed:#"G+-60x60.png"];
}
-(BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(#"%s", __FUNCTION__);
return YES;
}
-(void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(#"%s",__FUNCTION__);
}
-(UIViewController *)activityViewCtroller
{
NSLog(#"%s",__FUNCTION__);
return nil;
}
-(void)performActivity
{
// share what stuff u want to share
}
Related
In my application want to handle click of UIActivityTypePostToFacebook.
So how to achieve this click event.
Please check my code :
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
NSString *aText = #"Google";
UIImage *aPhoto = [UIImage imageNamed:#"3.png"];
NSURL *website = [NSURL URLWithString:#"http://www.google.com/"];
NSArray *objectsToShare = #[aText, aPhoto, website];
NSArray * applicationActivities = nil;
UIActivityViewController * activityViewController = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:applicationActivities];
activityViewController.excludedActivityTypes = #[
UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,
UIActivityTypeAirDrop,
UIActivityTypeAddToReadingList,
UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypePostToFlickr,
UIActivityTypePostToTencentWeibo,
// Whatever you want to exclude
];
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"8.0")){
activityViewController.popoverPresentationController.sourceView =
self.view;
}
if(isiPhone)
{
[self presentViewController:activityViewController animated:YES completion:nil];
}
else{
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[popup presentPopoverFromRect:CGRectMake(0, screenheight, 768, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
activityViewController.excludedActivityTypes = #[
UIActivityTypePostToWeibo,
UIActivityTypeAssignToContact,
UIActivityTypeAirDrop,
UIActivityTypeAddToReadingList,
UIActivityTypeCopyToPasteboard,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePrint,
UIActivityTypePostToFlickr,
UIActivityTypePostToTencentWeibo,
UIActivityTypePostToFacebook, UIActivityTypePostToTwitter,
// Whatever you want to exclude
];
if(isiPhone)
{
[self presentViewController:activityViewController animated:YES completion:nil];
}
else{
UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
[popup presentPopoverFromRect:CGRectMake(0, screenheight, 768, 0)inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
[activityViewController setCompletionHandler:^(NSString *act, BOOL done)
{
NSString *ServiceMsg = nil;
if ( [act isEqualToString:UIActivityTypeMail] ) ServiceMsg = #"Mail sended!";
if ( [act isEqualToString:UIActivityTypePostToTwitter] ) ServiceMsg = #"Post on twitter, ok!";
if ( [act isEqualToString:UIActivityTypePostToFacebook] ) ServiceMsg = #"Post on facebook, ok!";
if ( [act isEqualToString:UIActivityTypeMessage] ) ServiceMsg = #"SMS sended!";
if ( done )
{
UIAlertView *Alert = [[UIAlertView alloc] initWithTitle:ServiceMsg message:#"" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[Alert show];
}
}];
Choice-2
if above choice not working well try choice 2 , add UIActivityItemSource in your .h file
then call the following method in your .m file
- (id) activityViewController:(UIActivityViewController *)activityViewController
itemForActivityType:(NSString *)activityType
{
if ( [activityType isEqualToString:UIActivityTypePostToTwitter] )
return #"This is a #twitter post!";
if ( [activityType isEqualToString:UIActivityTypePostToFacebook] )
return #"This is a facebook post!";
if ( [activityType isEqualToString:UIActivityTypeMessage] )
return #"SMS message text";
if ( [activityType isEqualToString:UIActivityTypeMail] )
return #"Email text here!";
if ( [activityType isEqualToString:#"it.albertopasca.myApp"] )
return #"OpenMyapp custom text";
return nil;
}
I am using the following code to set up UIActivityViewController:
NSArray *activityItems = [NSArray arrayWithObjects:[self textMessageToShare], nil];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
[activityViewController setCompletionHandler:^(NSString *activityType, BOOL completed) {
if (completed) {
[self sendFeedbackWithIndexPath:indexPath AndLikeType:100 AndCell:nil];
}
}];
[self.navigationController presentViewController:activityViewController
animated:YES
completion:^{
// ...
}];
Issue is that when I copy a message or post to facebook or twitter or email or gmail app or to default Messages app, the new line characters that are in [self textMessageToShare] are maintained. However, if I share to other activities like WhatsApp or Viber - all the new line characters are removed, and the whole message is sent as one single line.
Whereas, if I share just text through iOS default Notes app, new line characters are maintained when shared to these apps. How would the Notes app be storing the new line characters? I am using \n as the new line character.
For my life unable to even find the reason. Can anyone help?
I was able to make it work by converting the newline characters to "<br/>":
_myDataString= self.textview.text;
_myDataString= [_myDataString stringByReplacingOccurrencesOfString:#"\n" withString:#"<br/>"];
Please check the new line issue in whats app share using uiactivityviewcontroller.
#import <UIKit/UIKit.h>
#interface ShareActivity : UIActivityItemProvider
#property (nonatomic, strong) NSString *message;
#property (nonatomic, strong) NSArray *activities;
#end
#import "ShareActivity.h"
#implementation ShareActivity
#synthesize message = _message;
#synthesize activities = _activities;
- (id) activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
if([activityType isEqualToString:#"net.whatsapp.WhatsApp.ShareExtension"])
{
return [self.message stringByReplacingOccurrencesOfString:#"\n" withString:#"<br/>"];
}
else if ([self.activities containsObject:activityType])
{
return [self.message stringByReplacingOccurrencesOfString:#"\n" withString:#"<br/>"];
}
else
{
return self.message;
}
return nil;
}
- (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
return #"";
}
In View Controller Class on any action button to pop up the share view
Apply this action on any button action
-(void)shareAction
{
ShareActivity *shareObj = [[ShareActivity alloc] initWithPlaceholderItem:#""];
NSString *message = #"New\nLine\nText\nMessage";
[shareObj setMessage:message];
NSArray* dataToShare = #[shareObj];
NSArray *excludeActivities = #[UIActivityTypePrint,UIActivityTypeOpenInIBooks,UIActivityTypeAddToReadingList,UIActivityTypePostToTencentWeibo,UIActivityTypeSaveToCameraRoll,UIActivityTypeAirDrop];
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:dataToShare applicationActivities:nil];
activityVC.excludedActivityTypes = excludeActivities;
[activityVC setCompletionHandler:^(NSString *act, BOOL done)
{
NSString *ServiceMsg = nil;
if ( [act isEqualToString:UIActivityTypeMail] )
{
ServiceMsg = #"Mail sent!";
}
else if ( [act isEqualToString:UIActivityTypePostToTwitter] )
{
ServiceMsg = #"Post on twitter, ok!";
}
else if ( [act isEqualToString:UIActivityTypePostToFacebook] )
{
ServiceMsg = #"Post on facebook, ok!";
}
else if ( [act isEqualToString:UIActivityTypeCopyToPasteboard] )
{
ServiceMsg = #"Message copy to pasteboard";
}
else if ( [act isEqualToString:UIActivityTypePostToFlickr] )
{
ServiceMsg = #"Message sent to flickr";
}
else if ( [act isEqualToString:UIActivityTypePostToVimeo] )
{
ServiceMsg = #"Message sent to Vimeo";
}
else
{
}
}];
[self presentViewController:activityVC animated:YES completion:nil];
}
hi its me again i was looking for the right answer
and i found that you can do it by using a Custom Share Message to Different Providers .
and you can find an example from this Code check for MyActivityItemProvider class .
https://github.com/apascual/flip-your-phone
i hove a problem posting the code here so i think the link above will help
Thanks to MuslimDev2015 I have been able to develop a solution:
https://github.com/lorenzoPrimi/NewlineActivityItemProvider
Try it and let me know.
You can send the text as multiple items each item is just one line.
let lines = text.components(separatedBy: "\n")
let activityViewController = UIActivityViewController(activityItems: lines, applicationActivities: nil)
How do i share image from imageView. Here is my code for imageView:
UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:
#"image%d.jpg", i]];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage];
[myImageView setFrame:CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
_postImage.image = myImage;
}
- (IBAction)shareButtonPressed:(id)sender {
NSArray *activityItems;
if (_postImage.image != nil) {
activityItems = #[_postImage.image];
}
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
I get error - No share action available.Thanks in advance.
You probably forgot to initialize _postImage. Try to replace:
_postImage.image = myImage;
with:
_postImage = [[UIImageView alloc] initWithImage:myImage];
To get your image on activityViewController you need to create your custom Activity with that image which you want to show. Following are step's to create and show your custom activity with your image.
1.Create a new file subclassing UIActivity namely, "CustomActivity". Then in the CustomActivity.m file you need to write down following method.
- (NSString *)activityType {
return #"yourappname.Review.App"; //type
}
- (NSString *)activityTitle {
return #"Review App"; //title for activity
}
- (UIImage *)activityImage {
return [UIImage imageNamed:#"Icon.png"]; //image
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
NSLog(#"%s", __FUNCTION__);
return YES;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems {
NSLog(#"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController {
NSLog(#"%s",__FUNCTION__);
return nil;
}
- (void)performActivity {
//What your cust activity woudl perform when user tap onto it.
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.apple.com/"]];
[self activityDidFinish:YES];
}
Then in your mainVC you could import this class and add below lines to use this custom activity.
NSString *textItem = #"provide your string";
UIImage *imageToShare = [UIImage imageNamed:#"Icon.png"]; //Provide your activity image
NSArray *items = [NSArray arrayWithObjects:textItem,imageToShare,nil];
custAct = [[CustomActivity alloc]init];
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:#[custAct]];
[self presentViewController:controller animated:YES completion:nil];
Now you would be able to see your image with activity that you want to perform.
If my understanding is wrong, do correct me.
I am using a UIActivityViewController in my app, and I am getting a EXC_BAD_ACCESS code=2 crash on iOS 6, but not iOS 7. Here is the code:
NSArray *activityItems;
NSString *shareText = [NSString stringWithFormat:NSLocalizedString(#"Listen to", nil), self.currentChannel.title, self.currentChannel.itunesUrl];
if (self.currentChannel.mediumThumbnailImage)
{
activityItems = #[shareText, self.currentChannel.mediumThumbnailImage];
}
else
{
activityItems = #[shareText];
}
UIActivityViewController *activityController = [[UIActivityViewController alloc]
initWithActivityItems:activityItems
applicationActivities:nil];
[activityController setCompletionHandler:^(NSString *activityType, BOOL completed) {
// once they have shared, check where they shared the content for analytics
if (completed)
{
NSString *actionName = nil;
NSString *socialName = nil;
if ([activityType isEqualToString:kMailActivity]) {
actionName = kSocialEmail;
socialName = kMail;
} else if ([activityType isEqualToString:kMessageActivity]) {
actionName = kSocialChat;
socialName = kMessage;
} else if ([activityType isEqualToString:kFacebookActivity] || [activityType isEqualToString:kTwitterActivity]) {
actionName = kSocialShare;
socialName = kFacebook;
}
if (actionName && socialName)
{
NSDictionary *data = #{kSocialName: socialName, kSocialContent: shareText};
if (data)
{
[ADBMobile trackAction:actionName data:data];
}
}
}
}];
if (activityController)
{
[activityController setExcludedActivityTypes:
#[UIActivityTypeAssignToContact,
UIActivityTypePrint,
UIActivityTypePostToWeibo,
UIActivityTypeSaveToCameraRoll,
UIActivityTypeAirDrop]];
[self presentViewController:activityController
animated:YES completion:nil];
}
I have used NSZombies to narrow down where the crash is happening, and it is happening when I call setExcludedActivityTypes: in iOS 6. I know that this error means that an object has been overreleased, and I am touching memory that doesn't belong to me. What I don't understand is why this crash is only occurring in iOS 6. Does anyone see something that could be causing this?
UIActivityTypeAirDrop is only available in iOS 7 and not in iOS 6.
You can check the availability of a constant like this:
if(&UIActivityTypeAirDrop) {
// UIActivityTypeAirDrop is available
} else {
// Its not available. Don't use it.
}
(I'm making this a community wiki, because I just copied the comment from user Larme above.)
I create a custom UIActivityViewController but when I load the icons that I do makes me see gray and you are pretty much loaded correctly, someone did it happen? how you have remedied?
ActivityViewCustomActivity *ca = [[ActivityViewCustomActivity alloc]init];
ca.service = #"avanti";
ca.image = image;
ca.act = #"com.avanti.app";
ActivityViewCustomActivity *fa = [[ActivityViewCustomActivity alloc]init];
fa.service = #"facebook";
fa.image = image;//[UIImage imageNamed:#"icon-facebook.jpg"];
fa.act = #"com.facebook.app";
ActivityViewCustomActivity *tw = [[ActivityViewCustomActivity alloc]init];
tw.service = #"twitter";
tw.image = image;
tw.act = #"com.twitter.app";
UIActivityViewController *activityVC =
[[UIActivityViewController alloc] initWithActivityItems:items
applicationActivities:#[ca,fa,tw]];
activityVC.excludedActivityTypes = #[UIActivityTypePostToTwitter,UIActivityTypePostToFacebook,UIActivityTypeMail,UIActivityTypePostToWeibo, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeSaveToCameraRoll];
activityVC.completionHandler = ^(NSString *activityType, BOOL completed)
{
if ([activityType isEqualToString:#"com.avanti.app"]) {
NSLog(#" activityType: %#", activityType);
NSLog(#" completed: %i", completed);
NSString *name = [q objectAtIndex:indexPath.row];
UIStoryboard *storyboar = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
ListViewController *list = [storyboar instantiateViewControllerWithIdentifier:#"ListViewController"];
list.ide = ide;
list.canale = name;
[self.navigationController pushViewController:list animated:YES];
}
else if ([activityType isEqualToString:#"com.facebook.app"]){
NSLog(#" activityType: %#", activityType);
NSLog(#" completed: %i", completed);
UIActionSheet *action = [[UIActionSheet alloc]initWithTitle:#"Facebook" delegate:self cancelButtonTitle:#"Annulla" destructiveButtonTitle:#"Vuoi pubblicarlo ?" otherButtonTitles:#"ok", nil];
action.actionSheetStyle = UIActionSheetStyleDefault;
[self actionSheet:action clickedButtonAtIndex:2];
[action showInView:[self.view window]];
}
else if ([activityType isEqualToString:#"com.twitter.app"]){
NSLog(#" activityType: %#", activityType);
NSLog(#" completed: %i", completed);
[self shareTwitter];
}
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
NSLog(#"ipad");
}
else
{
[self presentViewController:activityVC animated:YES completion:nil];
}
}
e l'activity è così
- (NSString *)activityType
{
return act;
}
- (NSString *)activityTitle
{
return service;
}
- (UIImage *)activityImage
{
// CGRect rect = CGRectMake(0.0f, 0.0f, 85.0f, 85.0f);
// UIGraphicsBeginImageContext(rect.size);
//
// rect = CGRectInset(rect, 15.0f, 15.0f);
// UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10.0f];
// [path stroke];
//
// rect = CGRectInset(rect, 0.0f, 10.0f);
// [service drawInRect:rect withFont:[UIFont fontWithName:#"Futura" size:15.0f] lineBreakMode:NSLineBreakByWordWrapping alignment:NSTextAlignmentCenter];
//
// UIImage *imag = UIGraphicsGetImageFromCurrentImageContext();
//
// UIGraphicsEndImageContext();
// //UIImage *ima = [UIImage imageNamed:#"facebook.jpg"];
// return imag;
UIImage *ima = [UIImage imageNamed:#"Icon_Facebook.png"];
return ima;
// if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
// {
// return [UIImage imageNamed:#"Facebook_43x43"];
// }
// else
// {
// return image;
// }
}
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
NSLog(#"%s", __FUNCTION__);
for (id obj in activityItems) {
if ([obj isKindOfClass:[NSString class]]) {
return YES;
}
}
return NO;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
NSLog(#"%s",__FUNCTION__);
}
- (UIViewController *)activityViewController
{
NSLog(#"%s",__FUNCTION__);
return nil;
}
- (void)performActivity
{
// This is where you can do anything you want, and is the whole reason for creating a custom
// UIActivity
[self activityDidFinish:YES];
}
+ (UIActivityCategory)activityCategory
{
return UIActivityCategoryShare;
}
and the screenshot is here http://i57.tinypic.com/332vtjo.png
and .h is
#import <UIKit/UIKit.h>
#interface ActivityViewCustomActivity : UIActivity
#property (nonatomic, strong) NSString *service;
#property (nonatomic, strong) UIImage *image;
#property (nonatomic, strong) NSString *act;
- (NSString *)activityType;
- (NSString *)activityTitle;
- (UIImage *)activityImage;
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems;
- (void)prepareWithActivityItems:(NSArray *)activityItems;
- (UIViewController *)activityViewController;
- (void)performActivity;
+ (UIActivityCategory)activityCategory;
#end
Try to add _ to your activityImage function
Something like
- (UIImage *)_activityImage
{
return [UIImage imageNamed:#"Icon_Facebook.png"];
}