Subhash Jha
Subhash Jha

Reputation: 64

Can I invoke airdrop from my macOS app directly (without going through the share menu UI)?

I have a macOS app where I want to support sharing via airdrop only. Is there a way to enable users to directly go to the airdrop dialog without going through the share menu UI? Thanks.

Upvotes: 0

Views: 141

Answers (1)

Subhash Jha
Subhash Jha

Reputation: 64

The link suggested in the comment helped in finding the relevant resource. Here is the exact code to share directly via airdrop.

    std::string filepath = "path_to_file";
    NSString* fileUrl = [NSString stringWithUTF8String:filepath.c_str()];
    NSURL *url = [NSURL fileURLWithPath:fileUrl];
    
    std::string fileName = "file_name";
    NSString* fileName = [NSString fileName.c_str()];
    
    NSSharingService *service = [NSSharingService sharingServiceNamed:NSSharingServiceNameSendViaAirDrop];
    NSArray *items = @[fileName, url];
    
    if (![service canPerformWithItems:items]) {
        NSLog(@"Can't share that kind of stuff, sorry!");
        return;
    }

    [service performWithItems:items];

Upvotes: 0

Related Questions