1.安裝ios_webkit_debug_proxy
Mac 可以簡單的使用 brew 安裝:
brew install ios-webkit-debug-proxy
Linux 或者 Mac:
sudo apt-get install \
autoconf automake \? ? ?
libusb-dev libusb-1.0-0-dev \? ? ?
libplist-dev libplist++-dev \? ? ?
usbmuxd \? ? ?
libimobiledevice-dev?
./autogen.sh
make
sudo make install
2.開啟ios_webkit_debug_proxy
ios_webkit_debug_proxy -c uuid:port -d
uuid:ios設(shè)備號可通過 idevice_id -l 查詢
port:端口
3.獲取websocket地址
get http://localhost:port/json *port為之前開啟ios_webkit_debug_proxy的端口號
返回內(nèi)容為:[{
"devtoolsFrontendUrl": "/devtools/devtools.html?ws=localhost:27753/devtools/page/7",
"faviconUrl": "",
"thumbnailUrl": "/thumb/https://www.baidu.com",
"title": "百度一下",
"url": "https://www.baidu.com",
"webSocketDebuggerUrl": "ws://localhost:27753/devtools/page/7",
"appId": "PID:1231"
}]
webSocketDebuggerUrl就是websocket地址
4.建立websocket連接,并發(fā)送請求通信
發(fā)送請求格式:
{"method":"Runtime.evaluate","params":{"objectGroup":"console","includeCommandLineAPI":true,"doNotPauseOnExceptionsAndMuteConsole":true,"expression":"window.location.href","returnByValue":true},id:1}
expression為js,id為標識隨意填寫
返回結(jié)果格式:
{"result":{"result":{"type":"string","value":"百度一下"},"wasThrown":false},"id":1}