iOS7.0 above complete positioning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.