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.
Your app is responsible for configuring, presenting, and dismissing this view controller. Configuration for the view controller involves specifying the data objects on which the view controller should act. (You can also specify the list of custom services your app supports.) When presenting the view controller, you must do so using the appropriate means for the current device. On iPad, you must present the view controller in a popover. On iPhone and iPod touch, you must present it modally.
Apple doc: http://goo.gl/02L6J
Very easy to use, this is the simplest way:
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]UIImage *anImage = [UIImage imageNamed:@"SampleImg.png"];
NSArray *Items = [NSArray arrayWithObjects:
@"A text line",
anImage, nil];
UIActivityViewController *ActivityView =
[[UIActivityViewController alloc]
initWithActivityItems:Items applicationActivities:nil];
[self presentViewController:ActivityView animated:YES completion:nil];[/code]
Now you are able to share “A text line” and your Image.
But if you need to share different text for different social networks or actions, you need to subclass everithing creating two simple class that inherit from UIActivity and UIActivityItemProvider, in this way:
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@interface APActivityProvider : UIActivityItemProvider <uiactivityitemsource>
@end
@interface APActivityIcon : UIActivity
@end[/code]
and in your implementation file, copy/paste:
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@implementation APActivityProvider
– (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;
}
– (id) activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController { return @""; }
@end[/code]
The provider is used to customize the text for different actions.
If you want a new action, copy/paste this one:
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]@implementation APActivityIcon
– (NSString *)activityType { return @"it.albertopasca.myApp"; }
– (NSString *)activityTitle { return @"Open Maps"; }
– (UIImage *) activityImage { return [UIImage imageNamed:@"lines.png"]; }
– (BOOL) canPerformWithActivityItems:(NSArray *)activityItems { return YES; }
– (void) prepareWithActivityItems:(NSArray *)activityItems { }
– (UIViewController *) activityViewController { return nil; }
– (void) performActivity {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"maps://"]];
}
@end[/code]
Obviously you can combine the Provider and the Icons!
How to use?
When you want, use that code. It create a new provider with an image and a custom new icon that open Apple Maps App.
You can add or exclude icons from the controller, editing the setExcludedActivityTypes property.
You can also get the status implementing setCompletionHandler block method.
[code lang=”java” autolinks=”false” collapse=”false” firstline=”1″ gutter=”true” htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false”]
APActivityProvider *ActivityProvider = [[APActivityProvider alloc] init];
UIImage *ImageAtt = [UIImage imageNamed:@"lines.png"];
NSArray *Items = @[ActivityProvider, ImageAtt];
[ActivityProvider release];
APActivityIcon *ca = [[APActivityIcon alloc] init];
NSArray *Acts = @[ca];
UIActivityViewController *ActivityView = [[[UIActivityViewController alloc]
initWithActivityItems:Items
applicationActivities:Acts] autorelease];
[ActivityView setExcludedActivityTypes:
@[UIActivityTypeAssignToContact,
UIActivityTypeCopyToPasteboard,
UIActivityTypePrint,
UIActivityTypeSaveToCameraRoll,
UIActivityTypePostToWeibo]];
[self presentViewController:ActivityView animated:YES completion:nil];
[ActivityView 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];
[Alert release];
}
}];
[/code]
hope this helps.
Rif: albertopasca.it
One thought on “[Objective-C] Custom UIActivityViewController icons and text”
Comments are closed.