5

我正在尝试将我们的应用程序移植到 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")

显示什么选项也取决于。例如,如果项目中有字符串和图像,则不会显示“保存到照片”。

4

3 回答 3

1

嗯,我似乎有一个有趣的类似问题。我还得到一个“更多”的迷你弹出窗口。但是,如果我使用 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 上共享。但是 UIImage 工作得很好:/

于 2019-10-18T11:15:05.823 回答
0
    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")
            
        }
        
    }
于 2021-08-15T08:19:51.127 回答
-2
#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
于 2020-04-17T08:26:14.177 回答