Sloan
Sloan

Reputation: 79

How to use Geofire with Swift to store location from one simulator and then show the location in another simulator.

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

Answers (1)

Lyju I Edwinson
Lyju I Edwinson

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

image gefire with firebase

A sample code is uploaded to : https://github.com/ledwinson/firebasegeo

A sample in firebase after storing location is shown below.

Upvotes: 5

Related Questions