Skip to content

Instantly share code, notes, and snippets.

@marcel-ploch
Created March 16, 2018 08:30
Show Gist options
  • Save marcel-ploch/3a606723c9f230db866fc250da9615e8 to your computer and use it in GitHub Desktop.
Save marcel-ploch/3a606723c9f230db866fc250da9615e8 to your computer and use it in GitHub Desktop.
Änderungen an der Datei damit wieder Cookies in der WebView gesetzt werden
...
function WebView() {
var _this = _super.call(this) || this;
var configuration = WKWebViewConfiguration.new();
_this._delegate = WKNavigationDelegateImpl.initWithOwner(new WeakRef(_this));
var jScript
= 'var meta = document.createElement(\'meta\'); meta.setAttribute(\'name\', \'viewport\'); meta.setAttribute(\'content\', \'initial-scale=1.0\'); document.getElementsByTagName(\'head\')[0].appendChild(meta);';
var cookieScript = '';
const cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies;
for (let i = 0; i < cookies.count; i++) {
const cookieObject = cookies.objectAtIndex(i);
// let cookie: any = cookies[i];
// console.log(cookie);
if (cookieObject.name === 'XSRF-TOKEN') {
console.log(cookieObject.name + '=' + cookieObject.value);
cookieScript += ' document.cookie = \'' + cookieObject.name + '=' + cookieObject.value + '\';'
// request.addValueForHTTPHeaderField(cookieObject.value, 'x-xsrf-token');
}
}
console.log(jScript);
var wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(jScript, 1, true);
var wkCookieScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(cookieScript, 0, false);
var wkUController = WKUserContentController.new();
wkUController.addUserScript(wkUScript);
wkUController.addUserScript(wkCookieScript);
configuration.userContentController = wkUController;
configuration.preferences.setValueForKey(true, 'allowFileAccessFromFileURLs');
_this.nativeViewProtected = _this._ios = new WKWebView({ frame : CGRectZero, configuration : configuration });
return _this;
}
...
WebView.prototype._loadUrl = function(src) {
if (src.startsWith('file:///')) {
this._ios.loadFileURLAllowingReadAccessToURL(NSURL.URLWithString(src), NSURL.URLWithString(src));
} else {
let cookiestring = '';
const request = NSMutableURLRequest.requestWithURL(NSURL.URLWithString(src));
const cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies;
console.dir(cookies.count);
for (let i = 0; i < cookies.count; i++) {
const cookieObject = cookies.objectAtIndex(i);
// let cookie: any = cookies[i];
// console.log(cookie);
console.log(cookieObject.name, cookieObject.value);
if (cookieObject.name === 'XSRF-TOKEN' && request.valueForHTTPHeaderField('cookie') === null) {
console.log(cookieObject.name + '=' + cookieObject.value);
// x-xsrf-token:5c92c57b80b34f1ca6b3b5e6082dd2c3
request.addValueForHTTPHeaderField(cookieObject.name + '=' + cookieObject.value + ';', 'Cookie');
// request.addValueForHTTPHeaderField(cookieObject.value, 'x-xsrf-token');
}
console.log(request.valueForHTTPHeaderField('Cookie'));
}
console.log(request.valueForHTTPHeaderField('Cookie'));
this._ios.loadRequest(request);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment