Skip to content
This repository was archived by the owner on Dec 13, 2019. It is now read-only.

Commit a0350bc

Browse files
committed
feat: Allow devserver's SSL certificate while on dev with HTTPS for cordova iOS
1 parent a825ff0 commit a0350bc

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

lib/cordova/cordova-config.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const
22
fs = require('fs'),
3-
path = require('path'),
4-
appPaths = require('../app-paths'),
5-
log = require('../helpers/logger')('app:cordova-conf')
63
et = require('elementtree')
74

5+
const
6+
appPaths = require('../app-paths'),
7+
logger = require('../helpers/logger'),
8+
log = logger('app:cordova-conf')
9+
warn = logger('app:cordova-conf', 'red')
10+
811
const filePath = appPaths.resolve.cordova('config.xml')
912

1013
function setFields (root, cfg) {
@@ -58,6 +61,46 @@ class CordovaConfig {
5861

5962
if (this.APP_URL !== 'index.html' && !root.find(`allow-navigation[@href='${this.APP_URL}']`)) {
6063
et.SubElement(root, 'allow-navigation', { href: this.APP_URL })
64+
65+
if (cfg.devServer.https && cfg.ctx.targetName === 'ios') {
66+
const node = root.find('name')
67+
if (node) {
68+
const filePath = appPaths.resolve.cordova(
69+
`platforms/ios/${node.text}/Classes/AppDelegate.m`
70+
)
71+
72+
if (!fs.existsSync(filePath)) {
73+
warn()
74+
warn()
75+
warn()
76+
warn()
77+
warn(`AppDelegate.m not found. Your App will revoke the devserver's SSL certificate.`)
78+
warn(`Please report the cordova CLI version and cordova-ios package that you are using.`)
79+
warn(`Also, disable HTTPS from quasar.conf.js > devServer > https`)
80+
warn()
81+
warn()
82+
warn()
83+
warn()
84+
}
85+
else {
86+
this.iosDelegateFilePath = filePath
87+
this.iosDelegateOriginal = fs.readFileSync(this.iosDelegateFilePath, 'utf-8')
88+
89+
// required for allowing devserver's SSL certificate on iOS
90+
if (this.iosDelegateOriginal.indexOf('allowsAnyHTTPSCertificateForHost') === -1) {
91+
this.iosDelegateNew = this.iosDelegateOriginal + `
92+
93+
@implementation NSURLRequest(DataController)
94+
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
95+
{
96+
return YES;
97+
}
98+
@end
99+
`
100+
}
101+
}
102+
}
103+
}
61104
}
62105

63106
this.__save()
@@ -77,13 +120,22 @@ class CordovaConfig {
77120
root.remove(nav)
78121
}
79122

123+
if (this.iosDelegateOriginal && this.iosDelegateNew) {
124+
this.iosDelegateNew = this.iosDelegateOriginal
125+
}
126+
80127
this.__save()
81128
}
82129

83130
__save () {
84131
const content = this.doc.write({ indent: 4 })
85132
fs.writeFileSync(filePath, content, 'utf8')
86133
log('Updated Cordova config.xml')
134+
135+
if (this.iosDelegateFilePath && this.iosDelegateNew) {
136+
fs.writeFileSync(this.iosDelegateFilePath, this.iosDelegateNew, 'utf8')
137+
log('Updated AppDelegate.m')
138+
}
87139
}
88140
}
89141

0 commit comments

Comments
 (0)