1. Import 2 system Frameworks
Import Corelocation
Import Mapkit
2. Add a field to Plist
Nslocationalwaysusagedescription or nslocationwheninuseusagedescription or both add
3. In the Func application (application:uiapplication, didfinishlaunchingwithoptions launchoptions: [Nsobject:anyobject]?) -Bool
If cllocationmanager.locationservicesenabled () {
Manager.delegate = Self
Manager.startupdatinglocation ()
}else {
Uialertview (title: "Location service is not available, please turn on mobile location service.", Message:nil, Delegate:nil, Cancelbuttontitle: "Know"). Show ()
}
4.func Locationmanager (manager:cllocationmanager!, Didchangeauthorizationstatus status:clauthorizationstatus) {// Authorized prosecution
Preauthorstatus = Status
Switch status {
Case. notdetermined://not selected
If Manager.respondstoselector (Selector ("Requestalwaysauthorization")) {
Manager.requestalwaysauthorization ()
}
Case. RESTRICTED://Limited
Showalert ("Location service is not available, please turn on mobile location service.", nil, nil, byvc:nil, nil)
Case. DENIED://refused
Let actions = [
Uialertaction (title: "Close", Style:.) Cancel, Handler:nil),
Uialertaction (title: "Go to Settings", Style:.) Default, Handler: {(action:uialertaction!), Void in
Let URL = Nsurl (string:uiapplicationopensettingsurlstring)!
Uiapplication.sharedapplication (). OpenURL (URL)
})
]
Showalert ("Program location service not turned on", "You need to go to the settings interface to enable our app location Service", actions, Byvc:nil, nil)
Default
Break
}
}
5.func Applicationwillenterforeground (application:uiapplication) {
Called as part of the transition from the background to the inactive state; Here's can undo many of the changes made on entering the background.
If cllocationmanager.locationservicesenabled () {
Manager.delegate = Self
Manager.startupdatinglocation ()
If cllocationmanager.authorizationstatus () = = Preauthorstatus && Preauthorstatus = =. Denied
{
Let actions = [
Uialertaction (title: "Close", Style:.) Cancel, Handler:nil),
Uialertaction (title: "Go to Settings", Style:.) Default, Handler: {(action:uialertaction!), Void in
Let URL = Nsurl (string:uiapplicationopensettingsurlstring)!
Uiapplication.sharedapplication (). OpenURL (URL)
})
]
Showalert ("Program location service not turned on", "You need to go to the settings interface to enable our app location Service", actions, Byvc:nil, nil)
}
}else {
Manager.delegate = Nil
Manager.stopupdatinglocation ()
Showalert ("Location service is not available, please turn on mobile location service.", Nil, [Uialertaction (title: "Known", Style:.) Cancel, Handler:nil)], Byvc:nil, nil)
}
}
OK, call it off.
iOS7.0 above complete positioning