如何向SFSafariViewController添加自定义活动?

时间:2016-07-30 23:33:13

标签: ios objective-c sfsafariviewcontroller

我想向我在应用中展示的UIActivity添加自定义活动(SFSafariViewController)。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

1。创建UIActivity

的子类

实施该课程的所有required methods,并在初始化活动时,在该点传入该网页的网址并初始化您的UIViewController,因为prepareWithActivityItems:未在SFSafariViewController上下文(rdar://24138390)。如果您的活动未显示UI,请在初始化期间保存URL,以便在用户点击操作时对其进行处理。

完整示例:

@interface YourActivity : UIActivity {
    UIViewController *activityViewController;
}
- (id)initWithURL:(NSURL *)url;
@end


@implementation YourActivity

- (id)initWithURL:(NSURL *)url
{
    self = [super init];
    if (self)
    {
        [self prepareWithURL:url];
    }
    return self;
}

- (NSString *)activityType
{
    return @"YourTypeName";
}

- (NSString *)activityTitle
{
    return @"Perform Action";
}

- (UIImage *)activityImage
{
    return [UIImage imageNamed:@"YourActionIcon"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    return YES;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    NSURL* url = nil;
    for (NSObject* obj in activityItems)
    {
        if ([obj isKindOfClass:[NSURL class]])
        {
            url = (NSURL*)obj;
        }
    }

    [self prepareWithURL:url];
}

- (void) prepareWithURL:(NSURL*)url
{
    // initialize your UI using the given URL
    activityViewController = ... // initialize your UI here
}

- (UIViewController *)activityViewController
{
    return activityViewController;
}

+ (UIActivityCategory)activityCategory
{
    return UIActivityCategoryShare;
}


@end

2。将UIActivity添加到SFSafariViewController

SFSafariViewControllerDelegate中实施以下方法,该方法初始化活动并传入用户正在查看的网页的网址。

- (NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller
                            activityItemsForURL:(NSURL *)URL
                                          title:(NSString *)title
{
    YourActivity* activity = [[YourActivity alloc] initWithURL:URL];
    return @[activity];
}