不少時候須要獲取設備的UUID,好比在藍牙交互時,須要獲取服務和特徵的UUID,那麼如何獲取設備的UUID呢?請見以下代碼:ui
// // 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