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

前端之家收集整理的这篇文章主要介绍了ios – 向UIActivityViewController添加一个明显的(自定义)按钮前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我只想添加明显的按钮“在Safari中打开” @H_301_2@我怎么能以简单的方式做到这一点.
#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];


}

解决方法

因为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,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,UIActivityTypeSaveToCameraRoll];
    [self presentViewController:activityVC animated:TRUE completion:nil];


}

猜你在找的iOS相关文章