Does Mac Catalyst supports UIActivityViewController?

1.6k views Asked by At

I am trying to port over our app to Mac. But it seems that what works for iOS/iPadOS does not show up on Mac app. Nothing popups at all.

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)

Saw an error message that might be related:

setting preferences outside an application's container requires user-preference-write or file-write-data sandbox access

I have tried various settings in sandbox with no good result.

PS: Got it working after removing this line: activityController.setValue(NSLocalizedString("App Name", comment:""), forKey:"subject")

What option is shown also dependent. For example, if have a string and an image in items, then Save to Photos will not be shown.

3

There are 3 answers

5
Sunkas On

Hmm, I seem to have an interesting similar problem. I also get a "More" mini-popup. But if I use a UIButton instead of a UIView as a target it works.

Calling this code from with UIButton works:

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)
}

Could be a bug in macOS/Catalyst?

It also seems to be dependent on what type of item is shared. Same code with PDF-data wont share on macOS. But a UIImage is working just fine :/

1
叶志强 On
#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
0
Michael N On
    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")
            
        }
        
    }