从Swift中的UIActivityViewController中排除Notes和Reminders

时间:2016-01-13 13:59:14

标签: ios objective-c swift uiactivityviewcontroller

有没有人知道如何在Swift中执行此操作(下面的代码)? 它是此问题的第一个答案How to exclude Notes and Reminders apps from the UIActivityViewController?

部首:

@interface UIActivityViewController (Private)

- (BOOL)_shouldExcludeActivityType:(UIActivity*)activity;

@end

@interface ActivityViewController : UIActivityViewController

@end

实现:

@implementation ActivityViewController

- (BOOL)_shouldExcludeActivityType:(UIActivity *)activity
{
    if ([[activity activityType] isEqualToString:@"com.apple.reminders.RemindersEditorExtension"] ||
        [[activity activityType] isEqualToString:@"com.apple.mobilenotes.SharingExtension"]) {
        return YES;
    }
    return [super _shouldExcludeActivityType:activity];
}

1 个答案:

答案 0 :(得分:2)

我有相同的要求,但无法解决如何在swift中覆盖_shouldExcludeActivityType方法。

在尝试使用method swizzling失败后,我得出的结论是,使用Objective-C创建派生类,然后使用桥接头将派生类暴露给我的swift代码的其余部分是最简单的最好的方法。

如果你真的想在swift中实现大部分逻辑,只需在Objective-C派生类中获取重写的_shouldExcludeActivityType方法,以委托一些返回BOOL的方法,然后在swift中创建另一个派生类。覆盖那个方法。

Object-C派生类标题

#import <UIKit/UIKit.h>

@interface BaseBrowserActivityViewController : UIActivityViewController

- (BOOL)shouldExcludeActivityType:(UIActivity *)activity;

@end

Object-C派生类实现

#import "BaseBrowserActivityViewController.h"

@interface BaseBrowserActivityViewController ()

@end

@implementation BaseBrowserActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)_shouldExcludeActivityType:(UIActivity *)activity
{
    return [self shouldExcludeActivityType:activity];
}

- (BOOL)shouldExcludeActivityType:(UIActivity *)activity
{
    assert(false);  // shouldExcludeActivityType requires overriding.
    return false;
}

@end

Swift派生类实现

import UIKit

class BrowserActivityViewController: BaseBrowserActivityViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    override func shouldExcludeActivityType(_ activity: UIActivity!) -> Bool {
        // Do some testing of the activity here.
        return true
    }
}