获取当前设备的UUID和经纬度

项目中需要自动注册或者统计的时候会用到UUID和经纬度等.

以下是获取的方法:

一. 拿到设备UUID

    NSString *UUID = [[[UIDevicecurrentDevice]identifierForVendor]UUIDString];

    [[NSUserDefaultsstandardUserDefaults]setObject:UUIDforKey:@"UUID"];



二. 获取用户的经纬度:导入CoreLocation.framework   

#import <CoreLocation/CoreLocation.h>

遵循代理:CLLocationManagerDelegate

info.plist引入:NSLocationAlwaysUsageDescription = 将获取您的地理位置信息
                   NSLocationWhenInUseUsageDescription  = 将获取您的地理位置信息


代码如下:

    if ([CLLocationManagerlocationServicesEnabled]) {

        self.locationManager = [[CLLocationManageralloc]init];

        _locationManager.delegate =self;

        _locationManager.desiredAccuracy =kCLLocationAccuracyBest;

        _locationManager.distanceFilter =10;

        [_locationManagerrequestWhenInUseAuthorization];//授权操作

        [_locationManagerstartUpdatingLocation];

    }else{

        NSLog(@"定位服务不可用");

    }


#pragma mark -- 获取用户的经纬度信息(代理方法)

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations

{

    CLLocation *currLocation = [locationslastObject];

    NSString *latitude = [NSStringstringWithFormat:@"%f",currLocation.coordinate.latitude];

    NSString *longitude = [NSStringstringWithFormat:@"%f",currLocation.coordinate.longitude];

    [[NSUserDefaultsstandardUserDefaults]setObject:latitudeforKey:@"latitude"];//纬度

    [[NSUserDefaultsstandardUserDefaults]setObject:longitudeforKey:@"longitude"];//经度

}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

{

    if ([errorcode] ==kCLErrorDenied)

    {

        UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"提示" message:@"请在iPhone设置”-“隐私”-“定位功能中,找到XXXX允许访问位置信息" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];

        [alert show];

        NSLog(@"访问被拒绝");

    }

    if ([errorcode] ==kCLErrorLocationUnknown) {

        NSLog(@"无法获取位置信息");

    }

}


猜你喜欢

转载自blog.csdn.net/zhaotao0617/article/details/52130551