我正在尝试将我们的应用程序移植到Mac。但似乎适用于iOS/iPadOS的内容并未显示在Mac应用程序上。根本没有弹出任何弹出窗口。
let activityController = UIActivityViewController(activityItems:items, applicationActivities:nil)
activityController.setValue(NSLocalizedString("App Name", comment:""), forKey:"subject")
activityController.modalPresentationStyle = .popover
let popoverController = activityController.popoverPresentationController
if popoverController != nil {
popoverController!.barButtonItem = sender
popoverController!.permittedArrowDirections = .down
}
self.present(activityController, animated:true, completion:nil)
看到可能相关的错误消息:
setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access
我在沙盒中尝试了各种设置,但没有很好的结果。
PS:删除此行后使其正常工作:activityController.setValue(NSLocalizedString("App Name", comment:"(, forKey:"subject"(
显示的选项也取决于。例如,如果项目中有字符串和图像,则不会显示"保存到照片"。
嗯,我似乎有一个有趣的类似问题。我还得到了一个"更多"迷你弹出窗口。但是,如果我使用 UIButton 而不是 UIView 作为目标,它可以工作。
使用 UIButton 调用此代码有效:
func shareImage(_ image: UIImage, from fromView: UIView) {
let activityViewController = UIActivityViewController(activityItems: [image], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = fromView
activityViewController.popoverPresentationController?.sourceRect = fromView.bounds
self.present(activityViewController, animated: true, completion: nil)
}
可能是macOS/Catalyst中的错误?
它似乎还取决于共享的项目类型。与 PDF 数据相同的代码不会在 macOS 上共享。但是UII法师工作得很好:/
let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let document = documentURL.appendingPathComponent("myFile.mp3")
let activityController = UIActivityViewController(activityItems: [document], applicationActivities: nil)
DispatchQueue.main.async {
activityController.modalPresentationStyle = .popover
let popoverController = activityController.popoverPresentationController
if popoverController != nil {
popoverController!.sourceView = self.myUIButton
popoverController!.sourceRect = self.myUIButton.bounds
popoverController!.permittedArrowDirections = .any
}
self.present(activityController, animated: true) {
print("UIActivityViewController was presented")
}
}
#if _MAC_CATALYST_
//fix mac catalyst bug: UIActivityViewController add image to photo
@interface NSObject (FixCatalystBug)
@end
@implementation NSObject (FixCatalystBug)
+ (void)load{
Class cls = NSClassFromString(@"NSXPCDecoder");
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
SEL selectors[] = {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
@selector(_validateAllowedClass:forKey:allowingInvocations:)
#pragma clang diagnostic pop
};
for (NSUInteger index = 0; index < sizeof(selectors) / sizeof(SEL); ++index) {
SEL originalSel = selectors[index];
SEL swizzledSel = NSSelectorFromString([@"fs_swizzled_" stringByAppendingString:NSStringFromSelector(originalSel)]);
[cls fs_exchangeImpWithOriginalSel:originalSel swizzledSel:swizzledSel];
}
});
}
- (BOOL)fs_swizzled__validateAllowedClass:(Class)cls forKey:(id)key allowingInvocations:(id)allowingInvocations{
BOOL _validate = NO;
@try {
_validate = [self fs_swizzled__validateAllowedClass:cls forKey:key allowingInvocations:allowingInvocations];
} @catch (NSException *exception) {
if ([key isEqualToString:@"NS.objects"] && [cls isKindOfClass:[NSURL class]]) {
_validate = YES;
}
}
return _validate;
}
@end
#endif