现在的IOS OTA安装应用需要使用https才能安装。
要使得服务器支持https,服务器必须要有https证书。证书可以同过购买获得或者自己生成。
这里我们使用自己生成的证书,使用自己生成的证书必须在iphone上手动安装之后才能访问https服务器,否则iphone会拒绝访问未受信任的https服务器。
使用openssl
创建https
证书
- 生成服务器的私钥
mkdir /private/etc/apache2/ssl
cd /private/etc/apache2/ssl
sudo openssl genrsa -out server.key 1024 - 生成签署申请(Common Name必须为服务器的ip或域名)
sudo openssl req -new -key server.key -out server.csr - 生成CA私钥
sudo openssl genrsa -out ca.key 1024 - 用CA的私钥产生CA的自签署证书
sudo openssl req -new -x509 -days 365 -key ca.key -out ca.crt - 创建demoCA
demoCA里面创建文件index.txt和serial,serial内容为01,index.txt为空,以及文件夹newcerts
sudo openssl ca -in server.csr -out server.crt -cert ca.crt -keyfile ca.key
这样我们就生成了server.crt,server.key,ca.crt文件。将 server.crt,server.key配置到服务器上,我们存放的位置是”/private/etc/apache2/ssl/server.crt”,”/private/etc/apache2/ssl/server.key”, ca.crt放到文件根目录中。
在使用以上过程生成证书的时候,要注意输入common name
的时候要输入网站的访问的域名或者是IP地址。
go语言实现证书安装和app安装
var installTemplate = `
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta content="telephone=no" name="format-detection"/>
<title>application name</title>
</head>
<style>
html {
width: 100%;
height: 100%
}
body {
width: 100%;
height: 100%;
background-color: #fafafa;
font-family: "Microsoft YaHei";
color: #0a0909;
-webkit-touch-callout: none;
-webkit-user-select: none;
}
div, p, header, footer, h1, h2, h3, h4, h5, h6, span, i, b, em, ul, li, dl, dt, dd, body, input, select, form, button {
margin: 0;
padding: 0
}
ul, li {
list-style: none
}
img {
border: 0 none
}
input, img {
vertical-align: middle
}
* {
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
outline: 0;
box-sizing: border-box;
}
a {
text-decoration: none;
}
h1, h2, h3, h4, h5, h6 {
font-weight: normal;
}
body {
padding: 20px;
}
.title {
font-size: 18px;
margin-bottom: 20px;
}
.install {
width: 150px;
height: 40px;
border: 1px solid #ccc;
background: transparent;
border-radius: 6px;
font-size: 14px;
margin-bottom: 10px;
display: block;
}
</style>
<body>
<p class="title">iOS应用OTA安装</p>
<a href="itms-services://?action=download-manifest&url=https://192.168.23.7:1718/static/manifest.plist">
<button class="install">安装应用</button>
</a>
<a title="iPhone" href="http://192.168.23.7:1717/static/ca.crt">
<button class="install">证书信任</button>
</a>
</body>
</html>
`
func main() {
http.Handle("/install/", http.HandlerFunc(install))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("test"))))
go func() {
http.ListenAndServe(":1717", nil)
}()
err := http.ListenAndServeTLS(":1718", "server.crt", "server.key", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func install(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, installTemplate)
}
首先通过http://ip:1717/install访问安装页面,在这个页面首先点击证书信任,以使iphone信任我们的临时证书,然后在点击安装应用则可以进行应用安装了。
这里使用了go自带的FileServer来进行静态文件下载。
OTA安装描述文件manifest.plist
这个文件可以自己写一个,也可以在xcode导出应用的时候通过xcode自动生成,其内容大概如下:
<?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>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>https://192.168.23.7:1718/static/majiang-mobile.ipa</string>
</dict>
<dict>
<key>kind</key>
<string>display-image</string>
<key>url</key>
<string>https://</string>
</dict>
<dict>
<key>kind</key>
<string>full-size-image</string>
<key>url</key>
<string>https://</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>com.dreamgame.jd.QWXY</string>
<key>bundle-version</key>
<string>1.0</string>
<key>kind</key>
<string>software</string>
<key>title</key>
<string>majiang-mobile</string>
</dict>
</dict>
</array>
</dict>
</plist>
其中bundle-identifier
必须和xcode中的设置一样和software-package
即是通过https访问apk文件的路径。