簡體   English   中英

Mac Catalyst 是否支持 UIActivityViewController?

[英]Does Mac Catalyst supports UIActivityViewController?

我正在嘗試將我們的應用程序移植到 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 上共享。 但是 UIImage 工作得很好:/

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM