如何在Xcode中从UIActivityViewController共享用户位置

时间:2015-10-11 09:57:39

标签: ios location share swift2 uiactivityviewcontroller

我知道如何分享其他对象,如图片,网址,字符串或其他。但我不知道如何从UIActivityViewController当前位置分享。 我试过了

    @IBAction func sendLocation(sender: UIBarButtonItem) {
    if mapView.userLocationVisible {
        let currentCoordinate = mapView.userLocation.location!
        print(currentCoordinate)
        let arrayLocation = [currentCoordinate]

        let activityController = UIActivityViewController(activityItems: arrayLocation, applicationActivities: nil)
        presentViewController(activityController, animated: true, completion: nil)
    } else {
        print("User location is not visible")
    }
}

但我在控制台中看到< + 53.09618071,+ 49.96449468> +/- 20.00米(速度0.00 mps /航向-1.00)@ 10/11 / 15,1:54:26 PM萨马拉标准时间UIActivityViewController并不共享任何位置。请帮帮我。

1 个答案:

答案 0 :(得分:1)

        @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")
        }
    }