iosswiftuiactivityviewcontrollerairdrop

Implementing Namedrop feature in Swift


I want to share a FileURL with Namedrop in iOS 17, but the following code is not working.

I have used UIActivityViewController.

let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = aView // so that iPads won't crash
present(activityViewController, animated: true, completion: nil)

Solution

  • By using this code, you can share a contact.vcf, a URL, an image, or any data via NameDrop on iOS 17 or later. Simply replace the 'fileURL' as needed." just replace the fileURL based on your requirement.

    let configuration = UIActivityItemsConfiguration(objects: [fileURL as NSURL])
    self.activityItemsConfiguration = configuration
    let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)
    present(activityViewController, animated: true, completion: nil)