How to share my current location to UIActivityViewController?

前端 未结 8 1395
别那么骄傲
别那么骄傲 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:33

    To share location like that you need to create a vcf file for that location... I just spent my whole day trying to do the same and gathering informations here and there managed to find out that apple does it in maps using a file name like "address you share.loc.vcf"

    You basically need to get the current location using the CLLocationManager, then use a CLGeocoder to get the address from the location you retrieved with the Location Manager.

    The CLGeocoder gets you a CLPlacemark that you can make into a MKPlacemark.

    You will use the addressDictionary stored in there

    With this info you can create an ABRecordRef using ABPersonCreate, store all the data for that location in there and then get the vcd data using ABPersonCreateVCardRepresentationWithPeople

    You'll need to also add a map url, just share a location with yourself from maps.app and look into the .vcf for the url structure...

    transform that data into an NSString and save to file.

    Create an NSURL to that file and share it with the UIActivityViewController...

    Pretty tedious work but it's definitely working great for me...

提交回复
热议问题