swift – Can’t get cookies from Telegram page in IOS WebView


I am having an issue with getting cookies from Telegram web page in IOS WKWebView. With this code – WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in print("Cookies - \(cookies.count)") }
I am getting all cookies from the certain domain (https://web.telegram.org in our case). It works fine with all domains, such as Instagram, Facebook, Google, Reddit (usually returns from 10 to 24 cookies. But in case of Telegram, it returns cookies.count = 1 or 0. So it returns zero cookies found. I suppose it is an issue with Telegram security policy, but I have not managed to come up with a solution, how to get cookies under these restrictions or something. Thanks a lot in advance for any help!

import UIKit
import WebKit

class WebViewControllerTest: UIViewController, WKNavigationDelegate {

lazy var webView: WKWebView = {
    let webView = WKWebView()
    webView.navigationDelegate = self
    webView.translatesAutoresizingMaskIntoConstraints = false
    return webView
}()

override func viewDidLoad() {
    super.viewDidLoad()
    
    view.addSubview(webView)
    NSLayoutConstraint.activate([
        webView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
        webView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
        webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
        webView.bottomAnchor.constraint(equalTo:        view.safeAreaLayoutGuide.bottomAnchor)
    ])
    setupWebView()
}

private func setupWebView() {
    guard let url = URL(string: "https://web.telegram.org") else { return }
    let request = URLRequest(url: url)
    webView.load(request)
    overrideUserInterfaceStyle = .light
}

func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation:     WKNavigation!) {
    print("Started loading page...")
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    print("Page loaded successfully.")
    
    WKWebsiteDataStore.default().httpCookieStore.getAllCookies { cookies in
        print("Cookies - \(cookies.count)")
    }
}
}

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img