Reputation: 79
I'm using Swift to build an iOS to share locations. Is there anyone know How to use Geofire with Swift to store a location from one simulator and then show the stored location in another simulator?
Thanks a lot.
Upvotes: 0
Views: 1056
Reputation: 1834
First try using cocopods as below,
pod 'Firebase', '~>4.0'
pod 'Firebase/Auth'
pod 'Firebase/Database'
pod 'Firebase/Storage'
pod 'GeoFire', :git => 'https://github.com/firebase/geofire-objc.git'
Use the below code to set and get location from Geofire.
struct FndDatabase {
static let LOCATION = Database.database().reference().child("userlocation")
static let GEO_REF = GeoFire(firebaseRef: LOCATION)
}
func setUserLocation(location: CLLocation) {
let user = Auth.auth().currentUser?.uid //userid for the logged in user
if let uid = user {
FndDatabase.GEO_REF?.setLocation(location, forKey: uid)
}
}
func getLocation() -> CLLocation? {
var loc: CLLocation?
if let uid = Auth.auth().currentUser?.uid {
FndDatabase.GEO_REF?.getLocationForKey(uid, withCallback: { (location, err) in
if err != nil {
print( "Error getting Userlocation from Geofire \(err.debugDescription)")
} else {
print( "UserLocation is available \(location.debugDescription)")
loc = location
}
})
}
return loc
}
in case if you see this error 'FirebaseDatabase/FirebaseDatabase.h' file not found.
then do this, select FirebaseDatabase.framework from pods and link it to Geofire as shown in the image
A sample code is uploaded to : https://github.com/ledwinson/firebasegeo
Upvotes: 5