How to share my current location to UIActivityViewController?

前端 未结 8 1343
别那么骄傲
别那么骄傲 2020-12-05 12:32

I know i can add things like text, URL, images to UIActivityViewController , but how to add my current location with a thumbnail of my location like in the tweet shown below

相关标签:
8条回答
  • 2020-12-05 12:56

    The other two answers use the complex AddressBook framework to create a vCard. It's easier to do it like this:

    NSString *vcardString = [NSString stringWithFormat:@"BEGIN:VCARD\n"
                             "VERSION:3.0\n"
                             "    N:;Shared Location;;;\n"
                             "   FN:Shared Location\n"
                             "item1.URL;type=pref:http://maps.apple.com/?ll=%f,%f\n"
                             "item1.X-ABLabel:map url\n"
                             "END:VCARD", lat, lng];
    

    Then save it to a file, get the NSURL of that file, and share it with UIActivityViewController as demonstrated in artillery129's answer.

    0 讨论(0)
  • 2020-12-05 12:57

    The correct way is to make the card using Data, not a file:

    Swift 4

        import MobileCoreServices
    
        let locationTitle: String = ...
        let coordinate: CLLocationCoordinate2D = ...
    
        let vCardString = [
            "BEGIN:VCARD",
            "VERSION:4.0",
            "N:;\(locationTitle);;;",
            "FN:\(locationTitle)",
            "item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)",
            "item1.X-ABLabel:map url",
            "END:VCARD"
            ].joined(separator: "\n")
    
        guard let vCardData = vCardString.data(using: .utf8) else { return }
    
        var items = [Any]()
        let vCardActivity = NSItemProvider(item: vCardData as NSData, typeIdentifier: kUTTypeVCard as String)
        items.append(vCardActivity)
        items.append(locationTitle)
        let activityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
        present(activityViewController, animated: true, completion: nil)
    
    0 讨论(0)
提交回复
热议问题