2017-05-07 1 views
1

UIActivityViewController에서 자신의 위치와 관련된 (위도, 경도)를 공유하려고합니다. 사용자가 다른 사람과 SMS의 위치를 ​​공유 할 수 있으며 클릭 가능한 작은지도.UIActivityViewController를 사용하여 경도와 위도를 공유하는 방법

주소를 텍스트로 공유하는 방법을 알고 있습니다. 여기

@IBAction func didTapShareLocation(_ sender: UIButton) { 
    guard let carAddress = self.adressLabel.text else { 
     return 
    } 
    let textToShare = "My car is at this address: \(carAddress)" 

    let objectsToShare = [textToShare] as [Any] 
    let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 

    activityVC.popoverPresentationController?.sourceView = sender 
    myParentVC?.present(activityVC, animated: true, completion: nil) 

} 

enter image description here

+0

다음 [검색 결과] (http://stackoverflow.com/search?q=UIActivityViewController+location)를 읽어 보시기 바랍니다. – rmaddy

답변

0

나는 여러 곳에서 정보의 조각을받은 후 함께 넣어 스위프트 3.1의 완전한 답은 여기에 주소를 공유 내 코드입니다. 나는 그것이 누군가를 돕기를 바랍니다.

@IBAction func didTapShareLocation(_ sender: UIButton) { 
    guard let carAddress = self.adressLabel.text, let lat = self.carCoordinates?.latitude, let lon = self.carCoordinates?.longitude else { 
     return 
    } 

    guard CLLocationCoordinate2DIsValid(self.carCoordinates!) else { 
     print("Location not valid!") 
     return 
    } 

    let carAddressString = "My car is at this address: \n\(carAddress)\n" 

    let vcardString = [ 
     "BEGIN:VCARD", 
     "VERSION:3.0", 
     "N:;Shared Location;;;", 
     "FN:Shared Location", 
     "item1.URL;type=pref:http://maps.apple.com/?ll=\(lat),\(lon)", 
     "item1.X-ABLabel:map url", 
     "END:VCARD" 
     ].joined(separator: "\n") 

    let directory = FileManager().urls(for: .cachesDirectory, in: .userDomainMask) 

    let path = directory.first!.path + "_vcard_for_location_sharing.loc.vcf" 
    do { 
     try vcardString.write(toFile: path, atomically: true, encoding: .ascii) 
     let url = NSURL(fileURLWithPath: path) 

     let objectsToShare = [url, carAddressString] as [Any] 
     let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) 

     activityVC.popoverPresentationController?.sourceView = sender 
     self.present(activityVC, animated: true, completion: nil) 

    } 
    catch { 
     print("problem saving vcard: \(error.localizedDescription)") 
    } 
}