Search code examples
iosairdrop

Send NSData via Airdrop


I've got a (for me) big problem. I want to send a vcf-file with airdrop from my own app to another iOS device. I've got a NSData object, which i should convert to a vcf file, and this I should send with airdrop to another IOS device.
The NSData object works fine, i can send a vcc file with email, but with airdrop I left my limit. I tried everything i found here in the forum and on developer.apple.com. But nothing works, I think the reason is, that i have no idea how too start the fix the problem. Has anybody any idea how i can realize it?

THANKS


Solution

  • I believe this is roughly what you are looking for:

    NSString *contactName = nil; // name of person in vcard
    NSData *vcfData = nil; // vcard data
    NSURL *fileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.vcf", contactName]]];
    NSError *writeError;
    if ([vcfData writeToURL:fileURL options:NSDataWritingAtomic error:&writeError]) {
        NSArray *activityItems = @[fileURL];
        UIActivityViewController *avc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
        [self presentViewController:avc animated:YES completion:nil];
    } else {
        // failed, handle errors
    }
    

    If you still want to support providing NSData to some of the activities you will have to create some objects that conforms to UIActivityItemSource protocol and have some of them return nil where appropriate (see this SO for more details on that). You might find the AirDrop sample code project from Apple helpful too.