iOS獲取設備名稱

蘋果在獲取設備名稱這一點上做的確實不夠人性化,比如我的設備是iPhone XR,根據#import <sys/utsname.h>框架獲取的字段是iPhone11,8,這一般人看不出什麼門道,實際上它代表的是手機固件的版本

struct utsname systemInfo;
    uname(&systemInfo);
    // 獲取設備標識Identifier即類似於"iPhone11,8"這種字符串
    NSString *platform = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];

網上很多維護通過Identifier去翻譯設備名稱的膠水代碼,新出的機型很多文章或者代碼都不及時更新的,畢竟維護成本在那裏,確實沒啥特別好的辦法,但是有沒有什麼一勞永逸的辦法呢,好像目前並沒有~
但是所幸找到以下兩個網站比較全面且可以作爲參考的:
https://www.theiphonewiki.com/wiki/Models
https://ipsw.me
以及API接口 https://api.ipsw.me/v4/devices

等蘋果發佈新品之後,一般這些網站會同步更新,只要對着更新對應的匹配規則即可
通過上面<sys/utsname.h>可以拿到類似iPhone11,8這種Identifier字段可以遍歷匹配https://api.ipsw.me/v4/devices 這個接口返回的數據,請求一次緩存起來使用即可,有新品發佈時再搞更新策略啥的
//類似於這樣通過identifier匹配對應的name字段即可

[
    {
        "name": "iPhone 2G",
        "identifier": "iPhone1,1",
        "boards": [
            {
                "boardconfig": "m68ap",
                "platform": "s5l8900x",
                "cpid": 35072,
                "bdid": 0
            }
        ],
        "boardconfig": "m68ap",
        "platform": "s5l8900x",
        "cpid": 35072,
        "bdid": 0
    },
    {
        "name": "iPhone 3G",
        "identifier": "iPhone1,2",
        "boards": [
            {
                "boardconfig": "n82ap",
                "platform": "s5l8900x",
                "cpid": 35072,
                "bdid": 4
            }
        ],
        "boardconfig": "n82ap",
        "platform": "s5l8900x",
        "cpid": 35072,
        "bdid": 4
    }
]
<!-- run -->
<style> 
.bg{
           display: flex;
            flex-direction: row;
            justify-content: flex-start;
}
  .btn{
           box-shadow: 2px 2px 3px #999;
            margin: 20px;
            background: linear-gradient(90deg, #00ff11 ,#ff0000);
            color: white;
            font-size: 15px;
            border-radius: 20px;
            width: 150px;
            height: 40px;
            line-height: 40px;
            text-align: center;
  }
</style>
<div class="bg"> 
<button class="btn" id="get-data-btn"> 獲取最新數據 </button>
<button class="btn" id="download-btn"  onclick="downloadJSON()"> 下載JSON數據 </button>
<div id="tips" style="color:red;"> 正在請求數據中...  </div>
</div>

<div id="show-json">
 </div>
<script>
function openDownloadDialog(url, saveName)
{
	if(typeof url == 'object' && url instanceof Blob)
	{
		url = URL.createObjectURL(url); // 創建blob地址
	}
	var aLink = document.createElement('a');
	aLink.href = url;
	aLink.download = saveName || ''; // HTML5新增的屬性,指定保存文件名,可以不要後綴,注意,file:///模式下不會生效
	var event;
	if(window.MouseEvent) event = new MouseEvent('click');
	else
	{
		event = document.createEvent('MouseEvents');
		event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
	}
	aLink.dispatchEvent(event);
}
function downloadJSON() {
         var json = document.getElementById('my-json').innerText;
          var blob = new Blob([json], {
                type: 'text/json,charset=UTF-8'
            });
           openDownloadDialog(blob, 'iOSDevices.json');
}
            var downloadBtn = document.getElementById('download-btn');
            var tipsLabel = document.getElementById('tips');
            tipsLabel.style.display = 'none';
            downloadBtn.style.display = 'none';

 window.onload = function() {
            var getDataBtn = document.getElementById('get-data-btn');
           getDataBtn.onclick = function () {
                 tipsLabel.style.display = 'block';
                 var linkUrl = "https://api.ipsw.me/v4/devices"
            var xmlhttp = new XMLHttpRequest();
            var type = "GET"; 
            xmlhttp.open(type, linkUrl, true); 
            xmlhttp.send(); 
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.status == 200 && xmlhttp.readyState == 4) {
                   tipsLabel.style.display = 'none';
                   downloadBtn.style.display = 'block';
                    var result = JSON.parse(xmlhttp.response); //獲取到的json數據
                     var div = document.getElementById('show-json')
                    div.innerHTML = `  <div class="copyItem" style="position: relative;">
                    <div class="codeType">json</div>
                     <div class="clipboard-button" id="copy_btn_0"  data-clipboard-target="#post_copy_target_0"  title="複製"> </div>
                     <pre class="language-json"  id="post_copy_target_0"  highlighted="true">
                    <code  class="language-json" id="my-json" style="font-family: 'mono-font' !important;">${JSON.stringify(result,null,4)}</code></pre>
                 </div> `;
                }
            }
       }   
    }
</script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章