chewy
chewy

Reputation: 8267

Adding an obvious (custom) button to UIActivityViewController

I would simply like to add the obvious button "Open in Safari" How can I do it in a simple way.

#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {


    NSString *textToShare = @"I just shared this from my App";
   // UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    NSArray *activityItems = [NSArray arrayWithObjects:textToShare, urlToShare,nil];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:Nil];
    //This is an array of excluded activities to appear on the UIActivityViewController
    //activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:TRUE completion:nil];


}

Upvotes: 10

Views: 5400

Answers (4)

shim
shim

Reputation: 10116

This project does it for you: https://github.com/davbeck/TUSafariActivity and it supports CocoaPods

Upvotes: 3

The Windwaker
The Windwaker

Reputation: 1054

As nevo shalev said you need to subclass UIActivity class

Here is an example:

UrlActivity.h

#import <UIKit/UIKit.h>

@interface UrlActivity : UIActivity

@end

UrlActivity.m:

#import "UrlActivity.h"

@implementation UrlActivity

- (NSString *)activityType
{
    return @"your Custom Type";
}

- (NSString *)activityTitle
{
    return @"Title to display under your icon";
}

- (UIImage *)activityImage
{
    return [UIImage imageNamed:@"your icon.png"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    // basically in your case: return YES if activity items are urls  
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    //open safari with urls (activityItems)
}

+(UIActivityCategory)activityCategory
{
    return UIActivityCategoryShare; // says that your icon will belong in application group, not in the lower part;
}

@end

And in your main file (after importing UrlActivity.h):

#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {


    NSString *textToShare = @"I just shared this from my App";
   // UIImage *imageToShare = [UIImage imageNamed:@"Image.png"];
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    NSArray *activityItems = [NSArray arrayWithObjects:textToShare, urlToShare,nil];

    // create an array with your custom activity and add it to the activityVC
    NSArray *appActivities = [NSArray arrayWithObjects:[[UrlActivity alloc] init]]];
    UIActivityViewController *activityVC = [[UIActivityViewController alloc]initWithActivityItems:activityItems applicationActivities:appActivities];
    //This is an array of excluded activities to appear on the UIActivityViewController
    //activityVC.excludedActivityTypes = @[UIActivityTypeMessage, UIActivityTypeCopyToPasteboard,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:TRUE completion:nil];


}

Upvotes: 14

Nevo Shalev
Nevo Shalev

Reputation: 1

you need to subclass UIActivity and and implement the methods and after add the class to applicationActivities

Upvotes: 0

Paul de Lange
Paul de Lange

Reputation: 10633

#pragma mark - Share the link
- (IBAction)activityButtonPressed:(id)sender {
    NSURL *urlToShare = [NSURL URLWithString:@"http://www.google.com"];
    [[UIApplication sharedApplication] openURL: urlToShare];
}

Upvotes: -6

Related Questions