向UIActivityViewController添加一个明显的(自定义)按钮

时间:2013-02-13 09:48:00

标签: ios objective-c ipad uiactivityviewcontroller

我只想添加一个明显的按钮“在Safari中打开” 我怎么能以简单的方式做到这一点。

#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];


}

4 个答案:

答案 0 :(得分:14)

正如nevo shalev所说,你需要继承UIActivity class

以下是一个例子:

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

在主文件中(导入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];


}

答案 1 :(得分:3)

这个项目是为你做的:https://github.com/davbeck/TUSafariActivity它支持CocoaPods

答案 2 :(得分:0)

你需要继承UIActivity并实现这些方法,然后将类添加到applicationActivities

答案 3 :(得分:-6)

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