从 Swift 中的 UIActivityViewController 中排除注释和提醒
Posted
技术标签:
【中文标题】从 Swift 中的 UIActivityViewController 中排除注释和提醒【英文标题】:Excluding Notes and Reminders from UIActivityViewController in Swift 【发布时间】:2016-01-13 13:59:14 【问题描述】:有人知道如何在 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];
【问题讨论】:
How to exclude Notes and Reminders apps from the UIActivityViewController?的可能重复 【参考方案1】:我也有同样的要求,但也不知道如何快速覆盖 _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
【讨论】:
以上是关于从 Swift 中的 UIActivityViewController 中排除注释和提醒的主要内容,如果未能解决你的问题,请参考以下文章