FLTCookieManager.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "FLTCookieManager.h"
  5. @implementation FLTCookieManager {
  6. }
  7. + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
  8. FLTCookieManager *instance = [[FLTCookieManager alloc] init];
  9. FlutterMethodChannel *channel =
  10. [FlutterMethodChannel methodChannelWithName:@"plugins.flutter.io/cookie_manager"
  11. binaryMessenger:[registrar messenger]];
  12. [registrar addMethodCallDelegate:instance channel:channel];
  13. }
  14. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  15. if ([[call method] isEqualToString:@"clearCookies"]) {
  16. [self clearCookies:result];
  17. } else {
  18. result(FlutterMethodNotImplemented);
  19. }
  20. }
  21. - (void)clearCookies:(FlutterResult)result {
  22. if (@available(iOS 9.0, *)) {
  23. NSSet<NSString *> *websiteDataTypes = [NSSet setWithObject:WKWebsiteDataTypeCookies];
  24. WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore];
  25. void (^deleteAndNotify)(NSArray<WKWebsiteDataRecord *> *) =
  26. ^(NSArray<WKWebsiteDataRecord *> *cookies) {
  27. BOOL hasCookies = cookies.count > 0;
  28. [dataStore removeDataOfTypes:websiteDataTypes
  29. forDataRecords:cookies
  30. completionHandler:^{
  31. result(@(hasCookies));
  32. }];
  33. };
  34. [dataStore fetchDataRecordsOfTypes:websiteDataTypes completionHandler:deleteAndNotify];
  35. } else {
  36. // support for iOS8 tracked in https://github.com/flutter/flutter/issues/27624.
  37. NSLog(@"Clearing cookies is not supported for Flutter WebViews prior to iOS 9.");
  38. }
  39. }
  40. @end