How to share my current location to UIActivityViewController?

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

    My variant in 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")
            }
        }
    

提交回复
热议问题