titanium-sdk
titanium-sdk copied to clipboard
chore(iOS):implement cookie in TiUIWebView using WKHTTPCookieStore
https://jira.appcelerator.org/browse/TIMOB-26915
Note -
- Code for cookie management is already there as part of https://jira.appcelerator.org/browse/TIMOB-26095. But it was not used and documented.
- This PR deletes the hacky way of implementing the cookie.
- This PR will document the new APIs.
- In android, it looks System Cookie are used for WebView only. Can we move that inside webview to have parity? cc @sgtcoolguy @jquick-axway
New APIs are - Method - a) webview.addCookie(Titanium.Network.Cookie); b) webview.removeCookie('domain', 'path', 'name'); c) webview.removeAllCookies(); d) webview. removeCookiesForDomain('domain') -> Array(Titanium.Network.Cookie) e) webview.getCookies(''domain', 'path', 'name') -> Array(Titanium.Network.Cookie) f) webview.cookiesForDomain('domain') -> Array(Titanium.Network.Cookie)
Property (read-only) - e) webview.allCookies -> Array(Titanium.Network.Cookie)
Test Case -
var win = Ti.UI.createWindow({
const win = Ti.UI.createWindow({
backgroundColor: 'white',
});
const button1 = Ti.UI.createButton({
top: 40,
left: 20,
title: 'AddCookie'
});
const button2 = Ti.UI.createButton({
top: 40,
right: 20,
title: 'Get All Cookie'
});
const button3 = Ti.UI.createButton({
top: 80,
left: 20,
title: 'Delete Cookie(google.com)'
});
const button4 = Ti.UI.createButton({
top: 80,
right: 10,
title: 'Delete All Cookie'
});
const button5 = Ti.UI.createButton({
top:120,
left: 20,
title: 'GetCookie For AppcDomain'
});
const label = Ti.UI.createLabel({
top: 160,
width: 320,
color: 'red',
backgroundColor: 'gray'
});
win.add(button1);
win.add(button2);
win.add(button3);
win.add(button4);
win.add(button5);
win.add(label);
const webview = Ti.UI.createWebView({
top: 200,
url: "http://httpbin.org/cookies"
});
webview.addEventListener('load', function(e) {
Ti.API.warn('\n\n\nLOADED');
const array = webview.allCookies;
label.text = 'No of cookies are: ' +array.length;
});
win.add(webview);
button1.addEventListener('click', function(e){
const cookie1 = Titanium.Network.createCookie({
name: 'CookieOne',
value: 'CookieOneValue',
domain: 'httpbin.org',
path: '/cookies',
version: 0,
expiryDate: new Date(2020, 1, 1)
});
webview.addCookie(cookie1);
const cookie2 = Titanium.Network.createCookie({
name: 'CookieTwo',
value: 'CookieTwoValue',
domain: 'httpbin.org',
path: '/cookies',
version: 1,
expiryDate: new Date(2021, 10, 10)
});
webview.addCookie(cookie2);
const cookie3 = Titanium.Network.createCookie({
name: 'Cookie1Google',
value: 'Cookie1GoogleValue',
domain: 'google.com',
path: '/cookies',
version: 1,
expiryDate: new Date(2021, 10, 10)
});
webview.addCookie(cookie3);
const cookie4 = Titanium.Network.createCookie({
name: 'Cookie1Appcelerator',
value: 'Cookie1GoogleValue',
domain: 'appcelerator.com',
path: '/cookies',
version: 1,
expiryDate: new Date(2021, 10, 10)
});
webview.addCookie(cookie4);
webview.reload();
});
button2.addEventListener('click', function(e){
const array = webview.allCookies;
label.text = 'No of cookies are: ' +array.length;
Ti.API.info('No of cookies are: \n' +array.length);
});
button3.addEventListener('click', function(e){
webview.removeCookie('google.com', '/cookies', null);
const array = webview.allCookies;
label.text = 'After deletion no of cookies are: ' +array.length;
Ti.API.info('After deletion no of cookies are: \n' +array.length);
});
button4.addEventListener('click', function(e){
webview.removeAllCookies();
const array = webview.allCookies;
label.text = 'After deletion no of cookies are: ' +array.length;
Ti.API.info('After deletion no of cookies are: \n' +array.length);
webview.reload();
});
button5.addEventListener('click', function(e){
const array = webview.cookiesForDomain('appcelerator.com');
label.text = 'No of cookies for appc domain are: ' +array.length;
Ti.API.info('No of cookies for appc domain are: \n' +array.length);
});
win.open();
Fails | |
---|---|
:no_entry_sign: |
Test suite crashed on iOS simulator. Please see the crash log for more details. |
:no_entry_sign: |
:microscope: There are library changes, but no changes to the unit tests. That's OK as long as you're refactoring existing code, but will require an admin to merge this PR. Please see README.md#unit-tests for docs on unit testing. |
Messages | |
---|---|
:book: | :fist: The commits in this PR match our conventions! Feel free to Rebase and Merge this PR when ready. |
:book: |
:white_check_mark: All tests are passing Nice one! All 4496 tests are passing. (There are 224 skipped tests not included in that total) |
:book: | :thumbsup: Hey!, You deleted more code than you added. That's awesome! |
Generated by :no_entry_sign: dangerJS against 45e2879ff191e8c2548f80f6c0b21fe53e4f58b8
Hi @vijaysingh-axway I was curious what the status of this ticket is? I have some issues keeping httpClient and webview cookies in sync since upgrading from ios 14.4 - are these API changes something you guys still explore?
I was just about to ask about relevance here but it seems to be a crucial topic for some apps? In that case, it'd be great to have some kind of parity with Android here. @hnestmann Do you use Android as well by any chance? I'd be curious how it's solved there.