如何将我的当前位置分享给UIActivityViewController?

我知道我可以添加文字,url,图像到UIActivityViewController的东西,但如何添加我的位置缩略图像我在下面显示的tweet位置的缩略图? 换句话说,如何在UIActivityViewController中共享坐标“纬度,经度”?

谢谢

与位置的推文

我有一个完整的代码示例和所有重要的类/方法来做到这一点。 我知道答案很晚,但这可能会帮助未来的人:

 - (void)shareTab { PFGeoPoint *geoPoint = self.vidgeoObject[kParseLocation]; NSString *coordinates = [NSString stringWithFormat:@"http://maps.apple.com/maps?q=%f,%f", geoPoint.latitude, geoPoint.longitude]; CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:geoPoint.latitude longitude:geoPoint.longitude]; CLGeocoder *geocoder; geocoder = [[CLGeocoder alloc]init]; [geocoder reverseGeocodeLocation:userLocation completionHandler:^(NSArray *placemarks, NSError *error) { CLPlacemark *rootPlacemark = placemarks[0]; MKPlacemark *evolvedPlacemark = [[MKPlacemark alloc]initWithPlacemark:rootPlacemark]; ABRecordRef persona = ABPersonCreate(); ABRecordSetValue(persona, kABPersonFirstNameProperty, (__bridge CFTypeRef)(evolvedPlacemark.name), nil); ABMutableMultiValueRef multiHome = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType); bool didAddHome = ABMultiValueAddValueAndLabel(multiHome, (__bridge CFTypeRef)(evolvedPlacemark.addressDictionary), kABHomeLabel, NULL); if(didAddHome) { ABRecordSetValue(persona, kABPersonAddressProperty, multiHome, NULL); NSLog(@"Address saved."); } NSArray *individual = [[NSArray alloc]initWithObjects:(__bridge id)(persona), nil]; CFArrayRef arrayRef = (__bridge CFArrayRef)individual; NSData *vcards = (__bridge NSData *)ABPersonCreateVCardRepresentationWithPeople(arrayRef); NSString* vcardString; vcardString = [[NSString alloc] initWithData:vcards encoding:NSASCIIStringEncoding]; NSLog(@"%@",vcardString); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"pin.loc.vcf"]; [vcardString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; NSURL *url = [NSURL fileURLWithPath:filePath]; NSLog(@"url> %@ ", [url absoluteString]); // Share Code // NSArray *itemsToShare = [[NSArray alloc] initWithObjects: url, nil] ; UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil]; activityVC.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeSaveToCameraRoll, UIActivityTypePostToWeibo]; if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone) { [self presentViewController:activityVC animated:YES completion:nil]; } else { popControl = [[UIPopoverController alloc] initWithContentViewController:activityVC]; } }]; } 

我已经创build了一个Swift替代品,它将一个vCard文件保存到可以在https://gist.github.com/naturaln0va/e1fed3f1d32ecf951aac中检出的caching目录中。

vCard结构与上面的whoKnows的回答相似。

 let vCardString = [ "BEGIN:VCARD", "VERSION:3.0", "N:;Shared Location;;;", "FN:Shared Location", "item1.URL;type=pref:http://maps.apple.com/?ll=\(coordinate.latitude),\(coordinate.longitude)", "item1.X-ABLabel:map url", "END:VCARD" ].joinWithSeparator("\n") 

要共享这样的位置,你需要为该位置创build一个vcf文件…我只花了整整一天的时间尝试做同样的事情,收集信息在这里和那里设法找出,苹果在地图上使用文件名称“address address share.loc.vcf”

您基本上需要使用CLLocationManager获取当前位置,然后使用CLGeocoder从您使用位置pipe理器检索的位置获取地址。

CLGeocoder为您提供一个CLPlacemark,您可以将其制作为MKPlacemark。

你将使用存储在那里的addressDictionary

有了这个信息,您可以使用ABPersonCreate创build一个ABRecordRef,将该位置的所有数据存储在那里,然后使用ABPersonCreateVCardRepresentationWithPeople获取vcd数据

您还需要添加地图url,只需要从maps.app与您自己分享一个位置,然后查看.vcf中的url结构…

将数据转换为NSString并保存到文件。

创build一个NSURL到该文件并与UIActivityViewController共享…

非常繁琐的工作,但它绝对是伟大的我…

另外两个答案使用复杂的AddressBook框架来创build一个vCard。 这样做更容易:

 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]; 

然后保存到一个文件,获取该文件的NSURL,并与UIActivityViewController共享,如artillery129的答案中所示。

我在Swift 2中的变种

  let passDictionaryInfo = placeMark.addressDictionary as! [String: AnyObject] @IBAction func shareLocation(sender: UIBarButtonItem) { // print(receivedDictionary) let postalAdress = CNMutablePostalAddress() postalAdress.street = receivedDictionary["Name"] as! String postalAdress.city = receivedDictionary["City"] as! String postalAdress.state = receivedDictionary["State"] as! String postalAdress.postalCode = receivedDictionary["ZIP"] as! String postalAdress.country = receivedDictionary["Country"] as! String postalAdress.ISOCountryCode = receivedDictionary["CountryCode"] as! String let streetName = receivedDictionary["Name"] as! String let urlAddress = receivedDictionary["FormattedAddressLines"] as! [String] // print(urlAddress) let postalContact = CNLabeledValue(label: streetName, value: postalAdress) let urlAddressContact = CNLabeledValue(label: "map url", value: "http://maps.apple.com/maps?address=\(urlAddress.description)") let contact = CNMutableContact() contact.contactType = .Organization contact.organizationName = streetName contact.departmentName = streetName contact.postalAddresses = [postalContact] contact.urlAddresses = [urlAddressContact] // create path let directory = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask) let path = directory.first!.path!.stringByAppendingString("/\(streetName).loc.vcf") // print(path) do { let contactData = try CNContactVCardSerialization.dataWithContacts([contact]) contactData.writeToFile(path, atomically: true) let url = NSURL(fileURLWithPath: path) // print(url) let activityViewController = UIActivityViewController(activityItems: [url], applicationActivities: nil) presentViewController(activityViewController, animated: true, completion: nil) } catch { print("CNContactVCardSerialization cannot save address") } } 

我已经更新了@ naturaln0va的答案Swift 3:
https://gist.github.com/ElegyD/510a17904917a7e7326254b824da1b2f

只需复制并粘贴到某处,然后像这样使用它:

 let coordinate = CLLocationCoordinate2D(latitude: 52.520007, longitude: 13.404954) let vCardURL = LocationVCard.vCardUrl(from: coordinate, with: "Berlin") let activityViewController = UIActivityViewController(activityItems: [vCardURL], applicationActivities: nil) present(activityViewController, animated: true, completion: nil)