Skip to content

Instantly share code, notes, and snippets.

@codexss
Created October 31, 2018 18:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codexss/83fd528748bfa96ea324d506c9a5a2f5 to your computer and use it in GitHub Desktop.
Save codexss/83fd528748bfa96ea324d506c9a5a2f5 to your computer and use it in GitHub Desktop.
var port_number = 8080
var plist_url = "itms-services://?action=download-manifest&url=https://qcloud.coding.net/u/lentin/p/file/git/raw/master/ipa-installer.plist"
// 从应用内启动
if ($app.env == $env.app) {
$drive.open({
handler: function(data) {
fileCheck(data)
}
})
}
// 从 Action Entension 启动
else if ($app.env == $env.action) {
fileCheck($context.data)
}
else {
$ui.error("不支持此方式运行!")
delayClose(2)
}
function startServer(port) {
$http.startServer({
port: port,
path: "",
handler: function(result) {
var url = result.url
}
})
}
function fileCheck(data) {
if (data && data.fileName) {
var fileName = data.fileName;
if (fileName.indexOf(".ipa") == -1) {
$ui.error(fileName + "非 ipa 文件!")
delayClose(2)
} else {
install(fileName, data);
}
}
}
function install(fileName, file) {
var result = $file.write({
data: file,
path: "app.ipa"
})
if (result) {
startServer(port_number)
var preResult = $app.openURL(plist_url);
if (preResult) {
$ui.alert({
title: "正在安装…",
message: "\n" + fileName + "\n\n请返回桌面查看进度\n\n安装完成后请返回\n\n点击\"安装完成\"按钮",
actions: [{
title: "取消",
style: "Cancel",
handler: function() {
$http.stopServer()
$file.delete("app.ipa")
delayClose(0.2)
}
},
{
title: "安装完成",
handler: function() {
$http.stopServer()
$file.delete("app.ipa")
delayClose(0.2)
}
}]
})
} else {
$ui.alert({
title: "安装启动失败",
message: "请重新运行此脚本",
actions: [{
title: "OK",
style: "Cancel",
handler: function() {
delayClose(0.2)
}
}]
})
}
} else {
$ui.alert({
title: "导入失败",
message: "请重新运行此脚本",
actions: [{
title: "OK",
style: "Cancel",
handler: function() {
delayClose(0.2)
}
}]
})
}
}
function delayClose(time) {
$thread.main({
delay: time,
handler: function() {
if ($app.env == $env.action || $app.env == $env.safari) {
$context.close()
}
$app.close()
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment