1

我想将自定义活动 ( UIActivity) 添加到SFSafariViewController我在我的应用程序中呈现的活动。我怎样才能做到这一点?

4

1 回答 1

0

1. 创建一个子类UIActivity

实现类的所有必需方法,并在初始化活动时,在该点传入页面的 URL 并初始化您的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. 添加UIActivitySFSafariViewController

在 your 中实现以下方法SFSafariViewControllerDelegate,该方法初始化活动并传入用户正在查看的页面的 URL。

- (NSArray<UIActivity *> *)safariViewController:(SFSafariViewController *)controller
                            activityItemsForURL:(NSURL *)URL
                                          title:(NSString *)title
{
    YourActivity* activity = [[YourActivity alloc] initWithURL:URL];
    return @[activity];
}
于 2016-07-30T23:33:12.910 回答