Python獲取iOS的UDID
測試iOSapp時候,我們可以安裝一下4種類型的包 :
AdHoc --一般為正式環境驗證
AppStore -- 上傳AppStore,只有appstore稽核通過釋出出來後才能安裝
Development --一般為測試環境驗證
Enterprise -- 企業證書打包【僅企業賬號使用】
其中AdHoc 、Development只有100個裝置, Enterprise不限使用者、不限裝置,所以沒有Enterprise賬號的話,只有把裝置的udid加入賬號後才能重新打包才能安裝ipa包。
但獲取udid是一件比較麻煩的事情:
網上有很多種方式:https://www.jianshu.com/p/f0ed370a8bc7
對大眾來說,最簡單是蒲公英網站提供的方式,掃個碼,安裝個描述檔案就可以獲取到,但畢竟是第三方的網站,安全是一方面,udid同時也洩露了。
那麼怎樣搭建一個和蒲公英一樣的獲取udid的內部網站呢?
第一步:
新建一個mobileconfig.xml檔案
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>PayloadContent</key> <dict> <key>URL</key> <!--接收資料的介面地址--> <string>https://axx.thxxxer.cn/get_udid</string> <key>DeviceAttributes</key> <array> <string>UDID</string> <string>IMEI</string> <string>ICCID</string> <string>VERSION</string> <string>PRODUCT</string> </array> </dict> <key>PayloadOrganization</key> <!--組織名稱--> <string>com.cover.the</string> <key>PayloadDisplayName</key> <!--檔案標題--> <string>獲取UDID</string> <key>PayloadVersion</key> <integer>1</integer> <key>PayloadUUID</key> <!--隨機生成的唯一GUID字串--> <string>15113571-2012-654y-4511-f0ra10164cec</string> <key>PayloadIdentifier</key> <string>com.cover.the</string> <key>PayloadDescription</key> <!--檔案描述--> <string>本描述檔案僅用來獲取裝置的UDID,請允許安裝!</string> <key>PayloadType</key> <string>Profile Service</string> </dict> </plist>
上面的配置檔案裡URl是接收ios裝置返回udid等值的介面,其他都可以隨便填寫,也可以直接用這個配置檔案。
第二步:
如果mobileconfig.xml檔案不簽名的話,安裝到手機上會顯示紅色的“未簽名”,我們需要對這個配置檔案進行一下簽名:
兩個命令搞定:
/usr/bin/security find-identity -p codesigning -v
這個命令可以查詢到mac上可用的證書,做ios開發的或者經常打包ipa的都知道,若自己電腦上沒有的話,找ios開發的同學給你一個證書名稱就行:
這個雙引號之間的名稱AppleDevelopment:danxxaodang(qweqw4L=P3)就是我們想要的:
接著執行下面的命令,當然,把mobileconfig.xml檔名修改為udid.mobileconfig:
/usr/bin/security cms -S -N "AppleDevelopment:danxxaodang(qweqw4L=P3)" -i /Users/jakey/Desktop/udid.mobileconfig -o /Users/jakey/Desktop/udid_signed.mobileconfig
執行完後會生成udid_signed.mobileconfig檔案,我們可以再改回mobileconfig.xml檔名備用。
第三步:
還記得剛開始mobileconfig.xml檔案裡的接收介面麼,這個地址是需要https的,http是不能訪問的,怎麼配置https呢?
見前一篇文章:Flask、Tornado、Nginx搭建Https服務
第四步:
搭建flask網站:
1.建立檢視:
# -*- coding = utf-8 -*- # ------------------------------ # @time: 2021/6/29 17:30 # @Author: drew_gg # @File: get_ios_udid.py # @Software: cover_app_platform # ------------------------------ import os from flask import Blueprint, request, render_template, redirect, url_for, Response from app.common.common_ios_type import get_ios_type as ios_type ios_udid = Blueprint('ios_udid', "__main__") pl = os.getcwd().split('cover_app_platform') xml_path = pl[0] + r'cover_app_platform\\app\\static\\xml\\' # 定義全域性變數 udid_l = [] @ios_udid.route('/index_udid/', methods=['GET', 'POST']) def index_udid(): """ 獲取udid首頁 """ return render_template('/post/get_udid/udid.html') @ios_udid.route('/show_udid/', methods=['GET', 'POST']) def show_udid(): """ 展示獲取到的udid頁面 """ return render_template('/post/get_udid/show_udid.html', data=udid_l) @ios_udid.route('/down_config/', methods=['GET', 'POST']) def down_config(): """ ios裝置訪問下載配置檔案 """ def file_content(f_p): with open(f_p, 'rb') as f: return f.readlines() file_path = xml_path + 'mobileconfig.xml' filename = os.path.basename(file_path) response = Response(file_content(file_path)) # 這裡的Content-Type一定要設定為application/x-apple-aspen-config response.headers['Content-Type'] = 'application/x-apple-aspen-config; chatset=utf-8' response.headers['Content-Disposition'] = 'attachment;filename="{}"'.format(filename) return response @ios_udid.route('/get_udid/', methods=['GET', 'POST']) def get_udid(): """ 獲取裝置返回的值 """ global udid_l b_data = request.data data_str = str(b_data).split('<?xml')[-1].split('</plist>')[0].split('dict')[1].replace('\\n', '').replace('\\t', '')\ .replace('>', '').replace('<', '').replace('/', '').replace('string', '').split('key') udid = data_str[4] product = ios_type.get_phone(data_str[2]) version = data_str[6] udid_l = [udid, product, version] # 這裡一定要對301進行重定向 return redirect(url_for('ios_udid.show_udid'), code=301)
以上需要注意的地方我都註釋了,比如Content-Type和301重定向!
2.建立html頁面:
udid.html:
<!DOCTYPE html> <head> <title>封面 | 快速獲取 iOS 裝置的UDID</title> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta property="og:type" content="webpage"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> </head> <body class="tools-box"> <div class="container-fluid wrapper"> <div class="container content inner-content" style="padding-bottom:40px;"> <div class="row"> <div class="col-md-10 col-md-offset-1 col-sm-8 col-sm-offset-2"> <div class="row text-center"> <div class="margin-bottom-30"> <h2 class="font-36 color-333">一步快速獲取 iOS 裝置的 UDID</h2> </div> </div> <div class="row text-center"> <div class="col-md-12"> <p class="mb-40 color-8c9497 font-16">請使用 <span class="color-green">iPhone或iPad</span>上的<span class="color-green">Safari</span>瀏覽器或<span class="color-green">相機</span>掃描下面的二維碼,即可快速獲取 UDID</p> </div> <div class="row text-center" id="UDIDQR"> <div class="col-md-8 col-xs-12 col-md-offset-2 mt-40"> <div class="row" style=" "> <div class="col-md-6 col-xs-12 col-sm-6 mb-40"> <img src="https://pkg/fm/1.1.0/cover.png" style="height:220px" /> </div> <div class="col-md-6 col-xs-12 col-sm-6 mb-0"> <div id="UDIDQRImg" style=""> <img src="https://pkg/fm/1.2.0/iosudid.png?content=https://apxx.the.cn/down_config" class="udid img-responsive center-block"/> <p class="mt-5">掃描二維碼,獲取 UDID</p> </div> </div> </div> </div> </div> </div> <hr style="border-top:2px solide #e6e6e6;" /> <div class="col-md-10 col-md-offset-1 input-group margin-bottom-20 mt-60"> <p class="font-18 color-333 ">掃碼後怎麼操作呢</p> <p class="color-8c9497 mb-35">掃碼後會下載一個描述檔案,需要安裝,步驟如下:<br> <font color="red"> 1. ios手機開啟“設定”; <br> 2. 找到“通用”並點選; <br> 3. 找到“描述檔案與裝置管理”或“裝置管理”並點選; <br> 4. 找到“查詢裝置UDID”並點選; <br> 5. 點選右上角的“安裝”按鈕即可。<br> </font> </p> <p class="font-18 color-333">什麼是UDID?</p> <p class="color-8c9497 mb-35">UDID 是 iOS 裝置的一個唯一識別碼,每臺 iOS 裝置都有一個獨一無二的編碼,這個編碼,我們稱之為識別碼,也叫做UDID( Unique Device Identifier)。</p> </div> </div> </div> </div> </div> </body> </html>
show_udid.html:
<!DOCTYPE html> <head> <title>封面 | 快速獲取 iOS 裝置的UDID</title> <meta charset="UTF-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta property="og:type" content="webpage"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent"> </head> <body class="tools-box"> <div class="container-fluid wrapper"> <div class="container content inner-content"> <div class="row"> <div class="col-md-10 col-md-offset-1 col-sm-8 col-sm-offset-2"> <div class="row text-center" style="margin-left: 0; margin-right: 0;"> <div class="row text-center" id="UDIDQR"> <div class="col-md-8 col-xs-12 col-md-offset-2 mt-40"> <div class="row" style=" "> <div class="col-md-6 col-xs-12 col-sm-6 mb-40"> <img src="https://p1.0/cover.png" style="height:220px" /> </div> </div> </div> </div> <h5 class="color-878f92 font-16 mb-15" style="text-align: left;">裝置資訊UDID:</h5> <div style="text-align: left;word-break:break-all;background: #f3f3f3;border-radius: 4px;min-height: 40px;height: auto;padding: 8px 10px;border: 1px solid #ddd;" class="tag-box tag-box-v3 mb-25"> <p class="color-333" style="font-size: 14px">{{data[0]}}</p> </div> <h5 class="color-878f92 font-16 mb-15" style="text-align: left;">裝置型號:</h5> <div style="text-align: left;word-break:break-all;background: #f3f3f3;border-radius: 4px;min-height: 40px;height: auto;padding: 8px 10px;border: 1px solid #ddd;" class="tag-box tag-box-v3 mb-25"> <p class="color-333" style="font-size: 14px">{{data[1]}}</p> </div> <h5 class="color-878f92 font-16 mb-15" style="text-align: left;">版本號:</h5> <div style="text-align: left;word-break:break-all;background: #f3f3f3;border-radius: 4px;min-height: 40px;height: auto;padding: 8px 10px;border: 1px solid #ddd;" class="tag-box tag-box-v3 mb-25"> <p class="color-333" style="font-size: 14px">{{data[2]}}</p> </div> </div> </div> </body> </html>
然後app裡註冊藍圖即可
ok,到此為止,我們就自己搭建完成了flask獲取udid網站:
有興趣搭建遇到問題的,歡迎討論。