fryette / webview_cookie_manager

MIT License
48 stars 52 forks source link

Fails to get cookies on iOS #20

Closed iverc closed 4 years ago

iverc commented 4 years ago

It works as expected on Android, but on iOS (11.0+) it returns empty array. Flutter code is equivalent for both platforms, so I suppose there is an issue when interacting with native iOS cookie manager.

fryette commented 4 years ago

Hi! I would like to help but I need some input from you.

  1. Could you please share MRE(Minimum reproducible example)?
  2. After setting cookies could you please add await Future.delay(Duration(seconds:3)), and then get cookies again. Any result appears?
iverc commented 4 years ago

HI @fryette, thanks for quick response.

  1. Basically, I open a website in the webview (e.g. youtube.com). Then I use the cookie manager to get cookies from that website like in example final gotCookies = await cookieManager.getCookies('https://youtube.com'); When I do that on Android, everything works as expected and I get an array of cookies. Doing the same on iOS device returns an empty array.
  2. I do not set any cookies manually, so not sure if adding a delay would help.

Thank you!

JosephNK commented 4 years ago

It would be nice to have a function like getAllCookies. In hasCookies, something says there is a cookie... I don't know what cookies are in flutter.

fryette commented 4 years ago

@JosephNK do you know a native way to implement it?

JosephNK commented 4 years ago

@fryette I checked the ios native source. When you call getCookies(null), you get all cookies. :)

JosephNK commented 4 years ago

Strangely, when calling getCookies(null) in ios, there are times when cookie cannot be fetched.

JosephNK commented 4 years ago

Please consider importing as HTTPCookieStorage.shared.cookies, not WKWebsiteDataStore.default().httpCookieStore.

fryette commented 4 years ago

@JosephNK but for android you cannot, right?

JosephNK commented 4 years ago

@fryette PR[#21] please check. i am currently not able to check Android. and the PR part has been modified so that it does not conflict with android.

fryette commented 4 years ago

@JosephNK will check it today or tomorrow morning and will provide a new version if everything is OK

i-Senku commented 4 years ago

i have same issue. I can't get cookie from ios.

seukaiwokeo commented 4 years ago

Same problem for me.

fryette commented 4 years ago

@seukaiwokeo @i-Senku could you please grab PR(https://github.com/fryette/webview_cookie_manager/pull/21) and check it?

It can be done with:

  webview_cookie_manager:
    git:
      url: https://github.com/JosephNK/webview_cookie_manager.git

If everything starts to work for both of you, I will immediately produce a new version with these changes.

i-Senku commented 4 years ago

@fryette It's doesn't work. getCookies functions returned empty Cookie List.

JosephNK commented 4 years ago

@i-Senku When you set getCookies(null), in the case of Android, an empty list is correct. The reason is that Android can't get all the lists. And in the case of iOS, a list should appear. Did you test on Android? Did you do it with iOS?

i-Senku commented 4 years ago

@JosephNK yep i tested on IOS with offical webview package. But it return empty list. Did you tested on IOS ? Because it's doesnt work on my project :(

JosephNK commented 4 years ago

@i-Senku yep i tested on iOS. It works in my project with a modified version.

After modifying as below in the pubspec.yaml file

webview_flutter: ^1.0.7
webview_cookie_manager:
    git:
      url: https://github.com/JosephNK/webview_cookie_manager.git
      ref: 65979e2064fcdf5d98c0b2159ca5f4873f0259d8

After executing the command as below

$ flutter clean
$ flutter pub get

Try build

Run build iOS
i-Senku commented 4 years ago

@JosephNK
Webview Scene. I added breakpoint to getCookies function and it looks empty. Where am i doing wrong ?

    return SafeArea(
        child: Scaffold(
      body: WebView(
        initialUrl: "https://www.instagram.com/accounts/login/",
        javascriptMode: JavascriptMode.unrestricted,
        onPageFinished: (url) async {
          var cookies = await cookieManager.getCookies(url);
          cookies.forEach((element) {
            print(element.name);
          });
        },
      ),
    ));

pubspec file.

Ekran Resmi 2020-11-10 12 49 39
JosephNK commented 4 years ago

@i-Senku

getCookies(url) to getCookies(null)

can u try?

JosephNK commented 4 years ago

@i-Senku

When flutter passes the url value as a parameter, if you check the iOS native code, there may be cases where the host of the url value does not match when searching for the cookie value in the cookie storage. For example, in case of a wildcard, a case of mismatch occurs.

i-Senku commented 4 years ago

idk why i tried this code ( getCookies(null) ). What should I do now? Do you have a solution? I want to receive cookies from Instagram

Ekran Resmi 2020-11-10 13 08 12
JosephNK commented 4 years ago

@i-Senku @fryette Oops... I think the error is... I think you need to check the Future<List> getCookies(String currentUrl) function.

JosephNK commented 4 years ago

@i-Senku @fryette Fixed Invalid Char issue when creating a cookie for darts. https://github.com/fryette/webview_cookie_manager/pull/21/commits/70e8e0cbc7cfacfbb7abb7917e70a78a0744681b This error is an error when creating a cookie for darts. So, I modified the "Future getCookies(String currentUrl)" function.

i-Senku commented 4 years ago

ı fixed get cookies problem in ios. I founded problem in getCookies function. This code working

func getCookies(result: @escaping FlutterResult){
    var cookieList = [[String: Any]]()
    let cookieStore = WKWebsiteDataStore.default().httpCookieStore
    cookieStore.getAllCookies { (cookies) in
        for cookie in cookies {
            cookieList.append(cookieToDictionary(cookie: cookie))
        }
        result(cookieList)
    }
}
fryette commented 4 years ago

@JosephNK maybe we also can include @i-Senku changes to PR? Or your fix also fix it?

JosephNK commented 4 years ago

@fryette I don't know what kind of code he solved. I already PR the Invalid Char issue.

JosephNK commented 4 years ago

@fryette u check out the PR code. or try testing.

fryette commented 4 years ago

@JosephNK give me a few hours

fryette commented 4 years ago

@JosephNK merged and 1.0.4 version provided. Thx for your contribution! If anyone has problems with the new version, feel free to reopen an issue or create a new one

iverc commented 3 years ago

@fryette Might be a bit late, tried to raise PR with a fix right away but did not have permission for it. 🤣 In my particular case, the problem with was the part of logic that did URL check. it was too strict, so if you were on a website with a prefix (e.g. m.website.com), the .contains() check for URL match failed and no cookies were returned. The fix was about doing a mirrored check to account for website prefixes. Hope it could be helpful

iverc commented 3 years ago

@fryette Actually It looks like the problem is still there (unless you set URL tonull). I can't add a branch or a PR with the fix, could you please allow it?

fryette commented 3 years ago

@iverc Feel free to fork repo and then you can create a PR with fix

iverc commented 3 years ago

@fryette https://github.com/fryette/webview_cookie_manager/pull/29

fryette commented 3 years ago

@iverc new version availbale (1.0.6)