Skip to content

Instantly share code, notes, and snippets.

@marcel-ploch
Last active April 20, 2017 14:51
Show Gist options
  • Save marcel-ploch/a4bde0706d88edafa5d2c529c095c1ac to your computer and use it in GitHub Desktop.
Save marcel-ploch/a4bde0706d88edafa5d2c529c095c1ac to your computer and use it in GitHub Desktop.
/// <reference path="../../../../references.d.ts" />
import {Injectable} from '@angular/core';
import * as application from 'application';
import {WebViewHeader} from './webview-header.abstract.service';
declare const NSHTTPCookie;
declare const NSDictionary;
@Injectable()
export class IosWebviewHeader extends WebViewHeader {
constructor() {
super();
}
/**
* Set the cookie header for the webview
* @param header
*/
public setHeader(header: string, url: string): void {
//Receive the shared cookie instance
const sharedCookieInstance: NSHTTPCookieStorage = NSHTTPCookieStorage.sharedHTTPCookieStorage;
// set acceptance to always
sharedCookieInstance.cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always;
//Split the header String in its partials
const splittedHeader: Array<string> = header.split(';');
// The name of the cookie
const cookieName: Array<string> = splittedHeader[0].split('=');
// The path of the cookie
const cookiePath: Array<string> = splittedHeader[1].split('=');
// The Cookie Domain
const cookieDomain: Array<string> = splittedHeader[2].split('=');
// create a cookie dictonary
const dic: any = new NSDictionary(
[ cookieName[0],
cookieName[1],
cookiePath[1],
url ],
[ NSHTTPCookieName, NSHTTPCookieValue, NSHTTPCookiePath, NSHTTPCookieOriginURL ]);
const cookie: NSHTTPCookie = new NSHTTPCookie(dic);
// set the cookie to the current instance
sharedCookieInstance.setCookie(cookie);
}
/**
* Removes the current cookie header
*/
public removeHeader(): void {
//Receive all cookies from the current store
const cookies: any = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies;
if (typeof cookies !== 'undefined') {
//Iterate over each of them
for (let i = 0; i < cookies.count; i++) {
//Generate a cookie object
const cookie: NSHTTPCookie = <NSHTTPCookie>cookies.objectAtIndex(i);
//And delete it
NSHTTPCookieStorage.sharedHTTPCookieStorage.deleteCookie(cookie);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment