iOS 获取手机 唯一标识
2015-07-21 21:38
302 查看
存贮在keychainQuery 可以统计用户使用情况
<p style="font-size: 14px; line-height: normal; font-family: Menlo;">-(<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">void</span>)gatherMessage{</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family:Menlo;color:#000000;line-height: normal;"> </span><span style="font-family:Menlo;line-height: normal;">//</span>采集用户设备信息</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>NSUserDefaults<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *userDefaults=[</span>NSUserDefaults<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">standardUserDefaults</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSDate</span> *loadDate=[<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSDate</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">date</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSDate</span> *lastData=[userDefaults <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">valueForKey</span>:<span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"loadForDay"</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>NSDateFormatter<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *dateFormatter=[[</span>NSDateFormatter<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">alloc</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">]</span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">init</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [dateFormatter <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">setDateFormat</span>:<span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"YYYY/MM/dd"</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *lastd=[dateFormatter <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">stringFromDate</span>:lastData];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *nowd=[dateFormatter <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">stringFromDate</span>:loadDate];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">if</span> (![lastd <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">isEqualToString</span>:nowd]) {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [userDefaults <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">setObject</span>:loadDate <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">forKey</span>:<span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"loadForDay"</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#008400;font-variant-ligatures: no-common-ligatures;">//</span><span style="font-family:'Heiti SC Light';color:#0840;line-height: normal;">获取设备型号</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *deviceModel=[<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">CommenData</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">getCurrentDeviceModel</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//identifierForVendor <span style="font-family:'Heiti SC Light';line-height: normal;">作为唯一标识</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *identifierStr = [[[</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">UIDevice</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>currentDevice<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">] </span>identifierForVendor<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">] </span>UUIDString<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> * <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">const</span> KEY_USERNAME_PASSWORD = <span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"com.int-yt.kyks.usernamepassword"</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> * <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">const</span> KEY_PASSWORD = <span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"com.int-yt.kyks.password"</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *usernamepasswordKVPairs = [<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">dictionary</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [usernamepasswordKVPairs <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">setObject</span>:identifierStr <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">forKey</span>:KEY_PASSWORD];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *readUserPwd = (<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *)[<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">CommenData</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">load</span>:KEY_USERNAME_PASSWORD];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">if</span>(readUserPwd==<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">nil</span>){</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#008400;font-variant-ligatures: no-common-ligatures;">//</span><span style="font-family:'Heiti SC Light';color:#0840;line-height: normal;">存</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">CommenData</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">save</span>:KEY_USERNAME_PASSWORD <span style="color:#31595d;font-variant-ligatures: no-common-li 13786 gatures;">data</span>:usernamepasswordKVPairs];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> readUserPwd = (<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *)[<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">CommenData</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">load</span>:KEY_USERNAME_PASSWORD];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> }</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//<span style="font-family:'Heiti SC Light';line-height: normal;">调用接口纪录登陆信息</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *string= [</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">stringWithFormat</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:</span>@"%@cdpt/api/uselog?access_token=123&imeiid=%@&mtype=%@&device_platform=2"<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">,</span><span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">BaseURLString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">, [readUserPwd </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">objectForKey</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:KEY_PASSWORD],deviceModel];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSURL</span> *url = [<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSURL</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">URLWithString</span>:string];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSURLRequest</span> *request = [<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSURLRequest</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">requestWithURL</span>:url];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">NSLog</span>(<span style="color:#d12f1b;font-variant-ligatures: no-common-ligatures;">@"%@"</span>,string);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>AFHTTPRequestOperation<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *operation = [[</span>AFHTTPRequestOperation<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">alloc</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">] </span><span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">initWithRequest</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:request];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(79, 129, 135);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> operation.</span>responseSerializer<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> = [</span>AFJSONResponseSerializer<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">serializer</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [operation <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">setCompletionBlockWithSuccess</span>:^(<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">AFHTTPRequestOperation</span> *operation, <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span> responseObject) {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> } <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">failure</span>:^(<span style="color:#4f8187;font-variant-ligatures: no-common-ligatures;">AFHTTPRequestOperation</span> *operation, <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSError</span> *error) {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> }];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [operation <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">start</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> }</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">//<span style="font-family:'Heiti SC Light';line-height: normal;">存</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">+ (<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">void</span>)save:(<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *)service data:(<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)data {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Get search dictionary</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *keychainQuery = [<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">self</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">getKeychainQuery</span>:service];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Delete old item before add new item</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">SecItemDelete</span>((<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFDictionaryRef</span>)keychainQuery);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Add new object to search dictionary(Attention:the data format)</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> [keychainQuery </span>setObject<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:[</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSKeyedArchiver</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>archivedDataWithRootObject<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:data] </span>forKey<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:(</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">kSecValueData</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Add item to keychain with the search dictionary</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">SecItemAdd</span>((<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFDictionaryRef</span>)keychainQuery, <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">NULL</span>);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">+ (<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *)getKeychainQuery:(<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *)service {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">return</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> [</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>dictionaryWithObjectsAndKeys<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> (</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecClassGenericPassword<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">,(</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecClass<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">,</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> service, (<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">kSecAttrService</span>,</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> service, (<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">kSecAttrAccount</span>,</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> (</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecAttrAccessibleAfterFirstUnlock<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">,(</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecAttrAccessible<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">,</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">nil</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);">//<span style="font-family:'Heiti SC Light';line-height: normal;">取</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">+ (<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)load:(<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *)service {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span> ret = <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">nil</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *keychainQuery = [<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">self</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">getKeychainQuery</span>:service];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Configure the search setting</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(0, 132, 0);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> [keychainQuery <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">setObject</span>:(<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">kCFBooleanTrue</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">forKey</span>:(<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span>)<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">kSecReturnData</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(112, 61, 170);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> [keychainQuery </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">setObject</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:(</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecMatchLimitOne<span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">forKey</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:(</span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">id</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">)</span>kSecMatchLimit<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFDataRef</span> keyData = <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">NULL</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">if</span> (<span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">SecItemCopyMatching</span>((<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFDictionaryRef</span>)keychainQuery, (<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFTypeRef</span> *)&keyData) == <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">noErr</span>) {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">@try</span> {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> ret = [<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSKeyedUnarchiver</span> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">unarchiveObjectWithData</span>:(<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSData</span> *)keyData];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> } <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">@catch</span> (NSException *e) {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(209, 47, 27);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">NSLog</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;">(</span>@"Unarchive of %@ failed: %@"<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">, service, e);</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> } <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">@finally</span> {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> }</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> }</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">if</span> (keyData)</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">CFRelease</span>(keyData);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">return</span> ret;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">+ (<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">void</span>)delete:(<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *)service {</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSMutableDictionary</span> *keychainQuery = [<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">self</span> <span style="color:#31595d;font-variant-ligatures: no-common-ligatures;">getKeychainQuery</span>:service];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">SecItemDelete</span>((<span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">__bridge</span> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">CFDictionaryRef</span>)keychainQuery);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; min-height: 16px;"> </p><p> </p><p style="font-size: 14px; line-height: normal; font-family: 'Heiti SC Light'; color: rgb(0, 132, 0);"><span style="font-family:Menlo;line-height: normal;">//</span>获得设备型号</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">+ (<span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span> *)getCurrentDeviceModel</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">{</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">int</span> mib[<span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">2</span>];</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">size_t</span> len;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">char</span> *machine;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> mib[<span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">0</span>] = <span style="color:#78492a;font-variant-ligatures: no-common-ligatures;">CTL_HW</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> mib[<span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">1</span>] = <span style="color:#78492a;font-variant-ligatures: no-common-ligatures;">HW_MACHINE</span>;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">sysctl</span>(mib, <span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">2</span>, <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">NULL</span>, &len, <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">NULL</span>, <span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">0</span>);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> machine = <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">malloc</span>(len);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">sysctl</span>(mib, <span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">2</span>, machine, &len, <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">NULL</span>, <span style="color:#272ad8;font-variant-ligatures: no-common-ligatures;">0</span>);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo; color: rgb(61, 29, 129);"><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> *platform = [</span><span style="color:#703daa;font-variant-ligatures: no-common-ligatures;">NSString</span><span style="color:#000000;font-variant-ligatures: no-common-ligatures;"> </span>stringWithCString<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:machine </span>encoding<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">:</span>NSUTF8StringEncoding<span style="color:#000000;font-variant-ligatures: no-common-ligatures;">];</span></p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#3d1d81;font-variant-ligatures: no-common-ligatures;">free</span>(machine);</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;"> <span style="color:#bb2ca2;font-variant-ligatures: no-common-ligatures;">return</span> platform;</p><p> </p><p style="font-size: 14px; line-height: normal; font-family: Menlo;">}</p>
相关文章推荐
- 开发第一个iOS项目 Hello iOS
- 安装iOS开发环境Xcode
- iOS初学者
- iOS开发 ----- C文件读写,以及字符串分割
- iOS 用代码添加模态视图
- iOS开发-使用第三方库AFNetWorking解析JSON和XML数据
- iOS开发 ----- 关于char *的赋值和字符串之间的复制问题以及sizeof strlen
- IOS开发- 访问通讯录,并将通讯录中姓名-头像-手机号码 发给服务器
- iOS OC05.2_数组
- iOS ---- CornerStone上传静态库.a
- iOS开发 ----- C小型学生信息管理系统,单向链表
- IOS第二天
- iOS WriteToFile的使用
- IOS开发-15.自定义cell(等高cell)
- ios Crash闪退日志获取和上传至服务器(NSSetUncaughtExceptionHandler)
- Table View Controller注意事项
- 即使通讯聊天界面搭建----iOS
- IOS开发-14.利用代码添加autolayout约束
- iOS中几种数据持久化方案
- 【iOS开发系列】scrollView中的directionalLockEnabled