Share User Location in swift
the idea of these is to prepare a component that is responsible of getting the user location and redirecting (sharing) to another point as [ text message, email, normal iOS sharing ] .
so in my years of work I came a cross of multiple situation were the user would like to give his location to the other end point (for example the delivery guy) but asking the user for permission to take his location will most likely be rejected bcz most people these days are afraid from tracking…etc.
so I came up with this little component where the user location is never sent stored or read by the core app or the app servers it is a one way of taking the user data and sharing it directly (with a nun of my/your app).
now i want teach you to build it from scratch but i will show you how the UI is structured then give you the ViewController code and teach you how to use it.

now to the details , the screen contains the following [ navBar , navBar buttons , tool bar, map , location button , sharing methods view , table view ]

in pice
- NavBar : is there to provide the cancel button to the screen bcz iPhones that are not supported with the iOS 13 and above will open the screen (present modely segue) in full screen mode.
- Tool bar : is used to switch between the map display modes
- Table View Card View : is used to contain the table view , another thing in the code the “Card” have some animation of expanding and collapsing so it become interactive.
- Table View : is to contain the sharing options , two notes the table must be “Grouped” and close the scrolling.

- Location Button : is the floating button on the right of the screen it gust for requesting the user location or asking for a refresh.
i hope this was a good intro for the UI bcz now we are going to Drop the CODE 😎
this is the view controller Code but you need to connect your outlets.
Requirements
the ShareUserLocationViewController
requires the following from the parent controller
sharingMethodstargetPhoneNumbertargetEmail[SharingMethods][String]?String?
- sharingMethods : is an array of
sharingMethods
which is anenum
that contains the sharing methods implemented in the component, the types are.message
,.email
,.share
quick note Don't pass the.share
because it will always be added automatically at the end. - targetPhoneNumber : if on of your sharing methods is
.message
you must pass the phone number so the location can be sent to that number. - targetEmail : if on of your sharing methods is
.email
you must pass the email so the location can be sent to that email.
How to call
1 — Connection
the call has to be from a Present Modally
segues connected to the ShareUserLocation.storyboard
2 — Navigation Code
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "location" {
let navVC = segue.destination as? UINavigationController
let destenation = navVC?.viewControllers.first as! ShareUserLocationViewController destenation.sharingMethods = [.email ,.message]
destenation.targetEmail = "genoy@gmail.com"
destenation.targetPhoneNumber = ["+218719785204"]
}
}
Authorization
you must take the user authorization on taking his location, and that will be done from the info.plist adding the following:
Privacy — Location When In Use Usage DescriptionPrivacy — Location Always and When In Use Usage Descriptionfor direct location sharing to other usersResult
if you couldn’t make the storyboard properly you can send me an email and i will send it back to you : magid10magi@gmai.com