Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
Flutter Inappwebview
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
李增强
Flutter Inappwebview
Commits
40dccfde
Commit
40dccfde
authored
Oct 25, 2018
by
pichillilorenzo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
added getCookies() and getCookie() for Android
parent
35233f09
Changes
5
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
101 deletions
+107
-101
.idea/workspace.xml
.idea/workspace.xml
+37
-94
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java
...pichillilorenzo/flutter_inappbrowser/MyCookieManager.java
+28
-6
example/android/build.gradle
example/android/build.gradle
+1
-1
example/lib/main.dart
example/lib/main.dart
+7
-0
lib/flutter_inappbrowser.dart
lib/flutter_inappbrowser.dart
+34
-0
No files found.
.idea/workspace.xml
View file @
40dccfde
This diff is collapsed.
Click to expand it.
android/src/main/java/com/pichillilorenzo/flutter_inappbrowser/MyCookieManager.java
View file @
40dccfde
package
com.pichillilorenzo.flutter_inappbrowser
;
import
android.os.Build
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
android.webkit.CookieManager
;
import
android.webkit.ValueCallback
;
import
java.net.HttpCookie
;
import
java.
net.MalformedURLException
;
import
java.
net.URL
;
import
java.
util.ArrayList
;
import
java.
util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
io.flutter.plugin.common.MethodCall
;
import
io.flutter.plugin.common.MethodChannel
;
...
...
@@ -44,6 +44,9 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
MyCookieManager
.
setCookie
(
url
,
name
,
value
,
domain
,
path
,
expiresDate
,
isHTTPOnly
,
isSecure
,
result
);
}
break
;
case
"getCookies"
:
result
.
success
(
MyCookieManager
.
getCookies
((
String
)
call
.
argument
(
"url"
)));
break
;
default
:
result
.
notImplemented
();
}
...
...
@@ -70,9 +73,8 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
if
(
expiresDate
!=
null
)
cookieValue
+=
"; Max-Age="
+
expiresDate
.
toString
();
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
N
)
if
(
isHTTPOnly
!=
null
&&
isHTTPOnly
)
cookieValue
+=
"; HttpOnly"
;
if
(
isHTTPOnly
!=
null
&&
isHTTPOnly
)
cookieValue
+=
"; HttpOnly"
;
if
(
isSecure
!=
null
&&
isSecure
)
cookieValue
+=
"; Secure"
;
...
...
@@ -93,4 +95,24 @@ public class MyCookieManager implements MethodChannel.MethodCallHandler {
}
}
public
static
List
<
Map
<
String
,
Object
>>
getCookies
(
final
String
url
)
{
final
List
<
Map
<
String
,
Object
>>
cookieListMap
=
new
ArrayList
<>();
CookieManager
cookieManager
=
CookieManager
.
getInstance
();
String
[]
cookies
=
cookieManager
.
getCookie
(
url
).
split
(
";"
);
for
(
String
cookie
:
cookies
)
{
String
[]
nameValue
=
cookie
.
split
(
"="
,
2
);
String
name
=
nameValue
[
0
].
trim
();
String
value
=
nameValue
[
1
].
trim
();
Map
<
String
,
Object
>
cookieMap
=
new
HashMap
<>();
cookieMap
.
put
(
"name"
,
name
);
cookieMap
.
put
(
"value"
,
value
);
cookieListMap
.
add
(
cookieMap
);
}
return
cookieListMap
;
}
}
example/android/build.gradle
View file @
40dccfde
...
...
@@ -5,7 +5,7 @@ buildscript {
}
dependencies
{
classpath
'com.android.tools.build:gradle:3.2.
0
'
classpath
'com.android.tools.build:gradle:3.2.
1
'
}
}
...
...
example/lib/main.dart
View file @
40dccfde
...
...
@@ -66,6 +66,12 @@ class MyInAppBrowser extends InAppBrowser {
// await this.webViewController.injectScriptCode("console.debug('testDebug', true);");
//
print
(
await
this
.
webViewController
.
injectScriptCode
(
"document.cookie"
));
print
(
""
);
print
(
await
CookieManager
.
getCookies
(
"https://flutter.io/"
));
print
(
""
);
print
(
await
CookieManager
.
getCookie
(
"https://flutter.io/"
,
"_ga"
));
print
(
""
);
//
// print(await this.webViewController.injectScriptCode("null"));
// print(await this.webViewController.injectScriptCode("undefined"));
...
...
@@ -281,6 +287,7 @@ class _MyAppState extends State<MyApp> {
//"toolbarTop": false,
//"toolbarBottom": false
});
},
child:
Text
(
"Open InAppBrowser"
)
),
...
...
lib/flutter_inappbrowser.dart
View file @
40dccfde
...
...
@@ -1119,6 +1119,7 @@ class InAppLocalhostServer {
}
///Manages the cookies used by an application's [InAppWebView] instances.
class
CookieManager
{
static
bool
_initialized
=
false
;
static
const
MethodChannel
_channel
=
const
MethodChannel
(
'com.pichillilorenzo/flutter_inappbrowser_cookiemanager'
);
...
...
@@ -1131,6 +1132,7 @@ class CookieManager {
static
Future
<
dynamic
>
_handleMethod
(
MethodCall
call
)
async
{
}
///Sets a cookie for the given [url]. Any existing cookie with the same [host], [path] and [name] will be replaced with the new cookie. The cookie being set will be ignored if it is expired.
static
Future
<
void
>
setCookie
(
String
url
,
String
name
,
String
value
,
String
domain
,
{
String
path
=
"/"
,
int
expiresDate
,
...
...
@@ -1157,4 +1159,36 @@ class CookieManager {
await
_channel
.
invokeMethod
(
'setCookie'
,
args
);
}
///Gets all the cookies for the given [url].
static
Future
<
List
<
Map
<
String
,
dynamic
>>>
getCookies
(
String
url
)
async
{
assert
(
url
!=
null
&&
url
.
isNotEmpty
);
Map
<
String
,
dynamic
>
args
=
<
String
,
dynamic
>{};
args
.
putIfAbsent
(
'url'
,
()
=>
url
);
List
<
dynamic
>
cookies
=
await
_channel
.
invokeMethod
(
'getCookies'
,
args
);
cookies
=
cookies
.
cast
<
Map
<
dynamic
,
dynamic
>>();
for
(
var
i
=
0
;
i
<
cookies
.
length
;
i
++)
{
cookies
[
i
]
=
cookies
[
i
].
cast
<
String
,
dynamic
>();
}
cookies
=
cookies
.
cast
<
Map
<
String
,
dynamic
>>();
return
cookies
;
}
///Gets a cookie by its [cookieName] for the given [url].
static
Future
<
Map
<
String
,
dynamic
>>
getCookie
(
String
url
,
String
cookieName
)
async
{
assert
(
url
!=
null
&&
url
.
isNotEmpty
);
assert
(
cookieName
!=
null
&&
cookieName
.
isNotEmpty
);
Map
<
String
,
dynamic
>
args
=
<
String
,
dynamic
>{};
args
.
putIfAbsent
(
'url'
,
()
=>
url
);
List
<
dynamic
>
cookies
=
await
_channel
.
invokeMethod
(
'getCookies'
,
args
);
cookies
=
cookies
.
cast
<
Map
<
dynamic
,
dynamic
>>();
for
(
var
i
=
0
;
i
<
cookies
.
length
;
i
++)
{
cookies
[
i
]
=
cookies
[
i
].
cast
<
String
,
dynamic
>();
if
(
cookies
[
i
][
"name"
]
==
cookieName
)
return
cookies
[
i
];
}
return
null
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment