Commit ef63b1d0 authored by laurent l's avatar laurent l

Add cookies synchronization for iOS

parent 63a9b904
......@@ -345,6 +345,14 @@ export class LibraryController extends Controller {
}
public onWebsitePageLoaded(args) {
let webview = args.object.getViewById("webView");
new Cookies().syncHttpToWebView(webview).then(() => {
webview.src = this.get('website_url');
})
}
public onOauthPageLoaded(args) {
let webview = args.object.getViewById("webView");
let oauth_web_view = OAuthWebViewClient.newWithWebView(webview);
......@@ -427,7 +435,6 @@ export class LibraryController extends Controller {
this._turnBusyIndicator('on');
let doOpenWebsite = () => {
new Cookies().syncHttpToWebViews();
this.navigate({
moduleName: "views/library/website",
bindingContext: this
......
import * as Application from 'application'
import { WebView } from 'ui/web-view'
declare var android: any
declare var java: any
export class Cookies {
public syncHttpToWebViews(): this {
public syncHttpToWebView(webview: WebView): Promise<any> {
new WebViewCookies().setCookies(this.getCookies())
return this
return Promise.resolve()
}
......@@ -20,7 +21,7 @@ export class Cookies {
}
public addCookie(uri:string, name: string, value: string): this {
public addCookie(uri: string, name: string, value: string): this {
new JavaHTTPCookies().addCookie(uri, name, value)
return this;
}
......@@ -31,6 +32,18 @@ export class Cookies {
new WebViewCookies().clearCookies()
return this
}
public printCookies(): Array<String> {
let desc = new Array<String>()
this.getCookies().forEach((cookies, uri) => {
cookies.forEach((cookie) => {
desc.push(uri + ': ' + cookie.toString())
})
})
return desc
}
}
......@@ -45,20 +58,20 @@ class JavaHTTPCookies {
}
public addCookie(uri:string, name: string, value: string): this {
public addCookie(uri: string, name: string, value: string): this {
let cookie_store = this.getCookieStore()
if (null == cookie_store)
return this
cookie_store.add(new java.net.URI(uri),
new java.net.HttpCookie(name, value));
new java.net.HttpCookie(name, value));
return this;
}
public getCookies(): Map<java.net.URI, Array<java.net.HttpCookie>> {
let cookies:Map<java.net.URI, Array<java.net.HttpCookie>> = new Map<java.net.URI, Array<java.net.HttpCookie>>()
let cookies: Map<java.net.URI, Array<java.net.HttpCookie>> = new Map<java.net.URI, Array<java.net.HttpCookie>>()
let cookie_store = this.getCookieStore()
if (null == cookie_store)
......@@ -69,7 +82,7 @@ class JavaHTTPCookies {
let uri = uris.get(i)
cookies.set(uri, Array.from(cookie_store.get(uri).toArray()))
}
return cookies
}
......@@ -80,10 +93,10 @@ class JavaHTTPCookies {
return this
java.net.CookieHandler.getDefault().setCookiePolicy(java.net.CookiePolicy.ACCEPT_ALL)
cookie_store.removeAll()
cookies.forEach( (uri_cookies:Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach( (cookie: java.net.HttpCookie) => { cookie_store.add(uri, cookie) })
cookies.forEach((uri_cookies: Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach((cookie: java.net.HttpCookie) => { cookie_store.add(uri, cookie) })
})
return this
......@@ -107,8 +120,8 @@ class WebViewCookies {
cookie_manager.setAcceptCookie(true)
cookie_manager.removeSessionCookie()
cookies.forEach( (uri_cookies:Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach( (cookie: java.net.HttpCookie) => {
cookies.forEach((uri_cookies: Array<java.net.HttpCookie>, uri: java.net.URI) => {
uri_cookies.forEach((cookie: java.net.HttpCookie) => {
cookie_manager.setCookie(uri.toString(), cookie.toString())
})
})
......
export declare class Cookies {
public syncHttpToWebViews(): this;
public setCookies(cookies:any): this;
public syncHttpToWebView(webview: WebView): Promise<any>;
public setCookies(cookies: any): this;
public getCookies(): Array<any>;
public clearCookies(): this;
public addCookie(uri:string, name: string, value: string): this;
public addCookie(uri: string, name: string, value: string): this;
public printCookies(): Array<String>;
}
import { WebView } from 'ui/web-view'
export class Cookies {
public syncHttpToWebViews(): this {
return this
public syncHttpToWebView(webview: WebView): Promise<any> {
let promises: Array<Promise<any>>;
promises = this.getCookies().map((cookie) => {
return new Promise((resolve, reject) => {
webview.ios.configuration.websiteDataStore.httpCookieStore.setCookieCompletionHandler(cookie, resolve);
})
})
return Promise.all(promises);
}
public setCookies(cookies:Array<any>): this {
return this
public setCookies(cookies: Array<NSHTTPCookie>): this {
for (let i = 0; i < cookies.length; i++)
NSHTTPCookieStorage.sharedHTTPCookieStorage.setCookie(cookies[i]);
return this;
}
public getCookies(): Array<any> {
return new Array()
public getCookies(): Array<NSHTTPCookie> {
let nscookies = NSHTTPCookieStorage.sharedHTTPCookieStorage.cookies;
let cookies: Array<NSHTTPCookie> = new Array<NSHTTPCookie>();
for (let i = 0; i < nscookies.count; i++)
cookies.push(nscookies[i])
return cookies;
}
public clearCookies(): this {
return this
let cookies = this.getCookies();
for (let i = 0; i < cookies.length; i++)
NSHTTPCookieStorage.sharedHTTPCookieStorage.deleteCookie(cookies[i]);
return this;
}
public addCookie(uri:string, name: string, value: string): this {
public addCookie(uri: string, name: string, value: string): this {
return this
}
public printCookies(): Array<String> {
let desc = new Array<String>()
this.getCookies().forEach((cookie) => {
desc.push(cookie.toString())
})
return desc
}
}
import {HTTPClient} from './http-client';
import {Cookies} from '../';
import { HTTPClient } from './http-client';
import { Cookies } from '../';
import * as http from 'http';
export class NSHTTPClient implements HTTPClient {
public request(options: any): Promise<http.HttpResponse> {
console.dir([ 'on request', this._printCookies(), options]);
return http.request(options).then( (response) => {
console.dir([ 'on response', this._printCookies(), response.headers]);
console.dir(['on request', this._printCookies(), options]);
return http.request(options).then((response) => {
console.dir(['on response', this._printCookies(), response.headers]);
return response;
});
}
protected _printCookies(): Array<String> {
let desc = new Array<String>()
new Cookies().getCookies().forEach( (cookies, uri) => {
cookies.forEach( (cookie) => {
desc.push(uri + ': ' + cookie.toString())
})
})
return desc
return new Cookies().printCookies()
}
}
<Page xmlns:nsDrawer="nativescript-ui-sidedrawer" xmlns:shared="views/shared" loaded="{{ onPageLoaded }}">
<Page xmlns:nsDrawer="nativescript-ui-sidedrawer" xmlns:shared="views/shared" loaded="{{ onWebsitePageLoaded }}">
<Page.actionBar>
<ActionBar title="{{ page_title }}" icon="">
<NavigationButton android.systemIcon="ic_menu_back" tap="{{ goBack }}"/>
......@@ -18,7 +18,7 @@
<nsDrawer:RadSideDrawer.mainContent>
<StackLayout>
<WebView src="{{ website_url }}" height="100%"/>
<WebView id="webView" height="100%"/>
<ActivityIndicator busy="true"/>
</StackLayout>
</nsDrawer:RadSideDrawer.mainContent>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment