ios - How to save Pin Annotation once app is closed (Swift) -
currently, app pins location , location stores so:
func locationmanager(manager: cllocationmanager!, didupdatelocations locations: [anyobject]!) { // find location of user var userlocation:cllocation = locations[0] as! cllocation var latitude = userlocation.coordinate.latitude var longitude = userlocation.coordinate.longitude var latdelta:cllocationdegrees = 0.01 var longdelta: cllocationdegrees = 0.01 var span: mkcoordinatespan = mkcoordinatespanmake(latdelta, longdelta) var location:mkuserlocation = currentlocation; var region: mkcoordinateregion = mkcoordinateregionmake(location.coordinate, span) var coordinate:cllocationcoordinate2d = cllocationcoordinate2dmake(latitude, longitude); carinitiallocation = userlocation; locationsdefaults.append(carinitiallocation); carinitialcoordinate = coordinate; self.map.setregion(region, animated: true); }
where locationsdefaults array store location of pin.
i use nsuserdefaults, i'm not sure how i'd doesn't except non ns data. guideline/structure in swift appreciated!
you can save nsdata nsuserdefaults. need convert cllocation object nsdata using nskeyedarchiver method archiveddatawithrootobject follow:
// saving cllocation object let locationdata = nskeyedarchiver.archiveddatawithrootobject(carinitiallocation) nsuserdefaults.standarduserdefaults().setobject(locationdata, forkey: "locationdata") // loading if let loadeddata = nsuserdefaults.standarduserdefaults().dataforkey("locationdata") { if let loadedlocation = nskeyedunarchiver.unarchiveobjectwithdata(loadeddata) as? cllocation { println(loadedlocation.coordinate.latitude) println(loadedlocation.coordinate.longitude) } }
Comments
Post a Comment