Reference Link: HTTPS://WWW.JIANSHU.COM/P/ADE69F95BFFC
1 Import UIKit2 Import Corelocation3 4 classViewcontroller:uiviewcontroller, cllocationmanagerdelegate {5 6@IBOutlet weak var showlocationbtn:uibutton!7@IBOutlet weak var locationlabel:uilabel!8 9var locationmanager:cllocationmanager!Ten One Overridefunc viewdidload () { A super.viewdidload () - //additional setup after loading the view, typically from a nib. - theUIApplication.shared.statusBarStyle =. Lightcontent - - } - + @IBAction Func Showlocationaction (_ Sender:uibutton) { - +Locationmanager =Cllocationmanager () ALocationmanager.Delegate= Self at -Locationmanager.desiredaccuracy =kcllocationaccuracybest - locationmanager.requestalwaysauthorization () - locationmanager.startupdatinglocation () - } - in Overridefunc didreceivememorywarning () { - super.didreceivememorywarning () to //Dispose of any resources the can be recreated. + } - the } * $ extension Viewcontroller {Panax Notoginseng Func Locationmanager (_ Manager:cllocationmanager, Didfailwitherror error:error) { -Self.locationLabel.text ="Error while updating location:"+error.localizeddescription the } + A Func Locationmanager (_ Manager:cllocationmanager, didupdatelocations locations: [Cllocation]) { theClgeocoder (). Reversegeocodelocation (manager.location!, Completionhandler: { +(Placemarks, error), Voidinch - $ ifError! =Nil { $Self.locationLabel.text ="Reverse Geocoder failed with error:"+ error!. Localizeddescription - return - } the - ifPlacemarks!. Count >0 {Wuyilet PM = placemarks! [0] the Self.displaylocationinfo (PM) -}Else { WuSelf.locationLabel.text ="Error existed in the data received from Geocoder" - } About }) $ } - -Func Displaylocationinfo (_ Placemark:clplacemark?) { -Guard Let Containsplacemark = PlacemarkElse{return} A + locationmanager.stopupdatinglocation () the -Let locality = (containsplacemark.locality! = nil)? Containsplacemark.locality:"" $Let PostalCode = (containsplacemark.postalcode! = nil)? Containsplacemark.postalcode:"" theLet Adminstrativearea = (containsplacemark.administrativearea! = nil)? Containsplacemark.administrativearea:"" theLet country = (containsplacemark.country! = nil)? Containsplacemark.country:"" the theSelf.locationLabel.text = postalcode! +" "+ locality! -Self.locationLabel.text?. Append"\ n") inSelf.locationLabel.text?. Append (adminstrativearea! +", "+ country!) the } the}
Day three: Swift uses corelocation to get the current address