2

Does anybody know how to do this (code below) in Swift? It's the first answer to this question How to exclude Notes and Reminders apps from the UIActivityViewController?

Header:

@interface UIActivityViewController (Private)

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

@end

@interface ActivityViewController : UIActivityViewController

@end

Implementation:

@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];
}
Community
  • 1
  • 1
MrASquare
  • 129
  • 8
  • Possible duplicate of [How to exclude Notes and Reminders apps from the UIActivityViewController?](http://stackoverflow.com/questions/31792506/how-to-exclude-notes-and-reminders-apps-from-the-uiactivityviewcontroller) – Daniel Storm May 07 '17 at 15:42

1 Answers1

3

I had the same requirement but could not work out how to override the _shouldExcludeActivityType method in swift either.

After some failed experimenting with method swizzling I came to the conclusion that using Objective-C to create the derived class and then using a bridging header to expose the derived class to the rest of my swift code was the simplest and best approach.

If you really want to implement most of the logic in swift just get the overridden _shouldExcludeActivityType method in the Objective-C derived class to delegate to some method that returns a BOOL and then create another derived class in swift that overrides that method.

Object-C Derived Class Header

#import <UIKit/UIKit.h>

@interface BaseBrowserActivityViewController : UIActivityViewController

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

@end

Object-C Derived Class Implementation

#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 Derived Class Implementation

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
    }
}
Community
  • 1
  • 1
Roj
  • 334
  • 4
  • 10