很多时候需要获取设备的UUID,比如在蓝牙交互时,需要获取服务和特征的UUID,那么如何获取设备的UUID呢?请见如下代码:
// // ViewController.m // 获取UUID // // Created by sleeping sun on 16/8/26. // Copyright © 2016年 sleeping sun. All rights reserved. // #import "ViewController.h"
#import <CoreFoundation/CoreFoundation.h>
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"%@",[self uuid]); } - (NSString *)uuid { // 创建UUID CFUUIDRef myUUID = CFUUIDCreate(nil); // 把创建的UUID转换为字符串 CFStringRef uuidString = CFUUIDCreateString(nil, myUUID); // 把C语言的字符串转为OC字符串 NSString *result = (NSString *)CFBridgingRelease(CFStringCreateCopy(nil, uuidString)); // 释放内存 CFRelease(myUUID); CFRelease(uuidString); // 返回UUID return result; } @end