Code about native API targeting

Source: Internet
Author: User

The first thing to add is the library corelocation.framework

Then you need to add two items in the Info.plist, Nslocationwheninuseusagedescription and Nslocationalwaysuseusagedescription.

This is asking whether the current device can access location permissions,

#import "ViewController.h"

#import <CoreLocation/CoreLocation.h>

@interface Viewcontroller () < cllocationmanagerdelegate >

@property (strong,nonatomic) Cllocationmanager *locationmanager;

@property (strong,nonatomic) UILabel *lab;

@property (strong,nonatomic) UILabel *lab2;

@end

@implementation Viewcontroller

@synthesize lab,lab2;

-(void) viewdidload {

[superviewdidload];

Additional setup after loading the view, typically from a nib.

Lab = [[UILabelalloc] initwithframe: CGRectMake( +, +, )];

Lab. text = @" Longitude " ;

[self. View Addsubview : Lab ];

lab2 = [[UILabelalloc] initwithframe: CGRectMake , 50 )];

lab2. text = @" Latitude " ;

[self. View Addsubview : LAB2 ];

UIButton *btn = [[UIButtonalloc] initwithframe: cgrectmake( A. )];

[btn settitle:@ " positioning "forstate: uicontrolstatenormal];

[btn settitlecolor: [uicolorredcolor] Forstate:uicontrolstatenormal];

[btn addTarget:selfAction:@selector :) forcontrolevents : UIControlEventTouchUpInside ];

[self. View Addsubview : btn];

//    instantiation of

}

-(void) Location: ( ID ) Sender

{

//    Determine if location services are available

if ([cllocationmanagerlocationservicesenabled]) {

NSLog(@ "Star location");

// set positioning accuracy for best accuracy

Self.locationManager.desiredAccuracy = Kcllocationaccuracybest;

// Set the distance filter to ten meters, indicating that the data is updated once per move

Self.locationManager.distanceFilter = 50;

self. Locationmanager = [[cllocationmanageralloc] init];

self. Locationmanager . Delegate = Self ;

// Access can be positioned to focus!!!!!!!!!!!!!!!!!!! For use with info.plist, there must be

if ([self. Locationmanager Respondstoselector : @selector (requestwheninuseauthorization)]) {

[self. Locationmanager requestwheninuseauthorization ];

[self. Locationmanager requestalwaysauthorization ];

}

// start listening position

[self. Locationmanager startupdatinglocation ];

}

Else

{

NSLog(@ "Unsupported");

}

}

#pragma Mark Cllocationmanagerdelegate

-(void) Locationmanager: ( Cllocationmanager *) Manager didupdatelocations: (nsarray*) Locations

{

NSLog(@ " get data ah ");

// get last location data

cllocation *location = [Locations lastobject];

// get longitude, latitude, altitude, speed, direction, etc.

lab2. text = @ "DDD" ;

Lab. text = [nsstringstringwithformat:@ "%f", location. coordinate . Longitude ];

lab2. text = [nsstringstringwithformat:@ "%f", location. coordinate . Latitude ];

}

// update data in real time

-(void) Locationmanager: ( Cllocationmanager *) Manager didchangeauthorizationstatus: (clauthorizationstatus) status

{

//NSLog (@ " get data ah ");

}

-(void) Locationmanager: ( Cllocationmanager *) Manager didfailwitherror: (nserror *) Error

{

NSLog(@ "Failed");

}

-(void) didreceivememorywarning {

[superdidreceivememorywarning];

//Dispose of any resources, can be recreated.

}

@end

Code about native API targeting

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.