在線工具-獲取iOS設備名稱JSON數據 https://www.cnblogs.com/wgb1234/articles/17555311.html
2022年7月22日 星期五 20時27分35秒更新, 自從發現油猴腳本這個好東西之后,一發不可收, 就總想着寫點工作上用得着的東西, 於是用處來了
直接上代碼:
// ==UserScript==
// @name 翻譯iOS設備名稱
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://xxxx.com.cn/tracking?uuid=*
// @icon https://www.google.com/s2/favicons
// @grant none
// ==/UserScript==
(function() {
'use strict';
const jsonData = [{
"name": "iPhone 2G",
"identifier": "iPhone1,1",
}, //此處略去眾多數據量
{
"name": "iPhone 3G",
"identifier": "iPhone1,2",
}
];
var targetNode = document.getElementById('dataList');
var config = { attributes: true, childList: true, subtree: true };
var callback = function(mutationsList) {
mutationsList.forEach(function(item, index) {
if (item.type == 'childList') {
if (index < 1) {//避免頻繁調用
setTimeout(function() {
const idf = targetNode.innerHTML.match(/iPhone[0-9]+\,[0-9]+/);
const iphoneId = idf.shift();
var filterArr = jsonData.filter(function(item) {
return iphoneId == item.identifier;
});
const repStr = filterArr[0].name;
if (repStr.length > 0) {
var tds = document.getElementsByTagName('td');
for (var i = 0; i < tds.length; i++) {
var td = tds[i];
td.innerHTML = td.innerHTML.replace(/iPhone[0-9]+\,[0-9]+/g, repStr);
}
}
}, 100);
}
}
});
};
var observer = new MutationObserver(callback);
observer.observe(targetNode, config);
})();
以上代碼,會在列表頁面刷新后實時監聽頁面變化,頁面刷新之后通過正則取出id匹配對應的設備name並完成替換,就是這么個原理
2022年 4月22日 星期五 19時42分53秒 更新 https://github.com/WangGuibin/Alfred-iOSDeviceName 寫了個Alfred插件方便查找
我的使用場景是這樣的,原先是寫了翻譯代碼在SDK內部的,僅僅只是為了優化管理后台看埋點信息定位問題而已,直到業務部門提需求過來說要優化SDK包的大小,於是從LinkMap分析,
再到可執行文件的優化,編譯選項設置各種嘗試之后,無奈只能刪減不是很必要的代碼了,因為類似iPhone11,8 這樣的字符串就足夠定位到機型了,但是人腦要記住這一系列玩意兒還是比較費勁的,實在記不住,就想着能不能造個輪子,查問題的時候直接翻譯出來 這樣就省事兒多了~
當然,如果后端配置json數據去匹配是最好不過的了
蘋果在獲取設備名稱這一點上做的確實不夠人性化,比如我的設備是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
通過下面👇下載的json文件轉成plist即可通過命令行工具轉一下,或者直接json解析最是直接
fileSubffix=${1##*.} #后綴名
if [[ "${fileSubffix}"x == "json"x ]] ;
then
plutil -convert xml1 "${1}" -o "${1%.*}".plist
fi
if [[ "${fileSubffix}"x == "plist"x ]] ;
then
plutil -convert json "${1}" -o "${1%.*}".json
fi
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Desktop/iOSDevices.plist"];
NSLog(@"%@",filePath);
NSArray *dataArr = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"dataArr.count=%ld",dataArr.count);
NSMutableArray *dataM = [NSMutableArray array];
for (NSDictionary *dic in dataArr) {
NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
dicM[@"deviceName"] = dic[@"name"];
dicM[@"identifier"] = dic[@"identifier"];
if([dic[@"name"] hasPrefix:@"iP"]){
[dataM addObject:dicM];
}
}
NSString *targetPath = [NSHomeDirectory() stringByAppendingFormat:@"/Desktop/AllDeviceInfo.plist"];
[dataM writeToFile:targetPath atomically:YES];
NSLog(@"dataM.count= %ld \n %@",dataM.count,dataM);
}
return 0;
}
等蘋果發布新品之后,一般這些網站會同步更新,只要對着更新對應的匹配規則即可
通過上面<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_4" data-clipboard-target="#post_copy_target_4" title="復制">
</div><pre class="language-json" id="post_copy_target_4" highlighted="true">
<code class="language-json" id="my-json" style="font-family: 'mono-font' !important;">${JSON.stringify(result,null,4)}</code></pre></div> `;
}
}
}
}
</script>