Read Cookie with PHP from WebView (iOs Xcode/Swift)

I want to read a cookie from an WebView (iOs) in PHP. It doesn't work.

First, I set an cookie named "testcookie" with value "test" and expiredate "2099" in xCode for my WebView Application and check the Log. I got the info, that my cookie saved successfully. Second, I start the simulator and opened my .php Script. In my .php Script I have the following code:


$testcookie = htmlentities($_COOKIE["testcookie"], ENT_QUOTES);

echo $testcookie;


After my webview has loaded the .php script, it shows me nothing.

So now my question, is it possible to read the cookies with php, which i have set in the webview.

Sorry for my bad English.

Thanks a lot!

     >want to read a cookie from an WebView (iOs)


Might want to reconsider, as webview has long been deprecated on iOS in favor of wkwebview.

Perhaps you misread my comment...

What do you get if you just


echo $_COOKIE["testcookie"]

I have tested the following code:

foreach ($_COOKIE as $key=>$val)

{

echo $key.' is '.$val."<br>\n";

}


This show me all cookies, but the testcookie is not available.


---


Here my Code in Xcode:


let ExpTime = TimeInterval(60 * 60 * 24 * 365 * 50)

let cookieProps: [HTTPCookiePropertyKey : Any] = [

HTTPCookiePropertyKey.domain: ".mydomain.de", // i dont want to show it here, but there is my domainname

HTTPCookiePropertyKey.path: "/",

HTTPCookiePropertyKey.name: "testcookie",

HTTPCookiePropertyKey.value: "test",

HTTPCookiePropertyKey.secure: "TRUE",

HTTPCookiePropertyKey.expires: NSDate(timeIntervalSinceNow: ExpTime),

HTTPCookiePropertyKey.version: "0"

]

if let cookie = HTTPCookie(properties: cookieProps) {

HTTPCookieStorage.shared.setCookie(cookie)

print("\(cookie)")

print("GET COOKIE \(HTTPCookieStorage.shared.cookies)")


}

else {

print("set cookie failed")

}

WKWebView
does not share cookies with the shared
HTTPCookieStorage
object. If you want to manipulate cookies in the web view, you’ll have to look at
WKHTTPCookieStore
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you!

Here is my code in Xcode. It works.

Do you have any further better solutions for my code?


// Create the WKWebView instance. Add a cookie store observer.

if #available(iOS 11, *){

let wkWebView = WKWebView()

wkWebView.configuration.websiteDataStore.httpCookieStore.add(OnShiftWKHTTPCookieStoreObserver())

}

// this is how we're adding cookies to our WKWebView instance.

let ExpTime = TimeInterval(60 * 60 * 24 * 365 * 50)

let cookies: [HTTPCookiePropertyKey : Any] = [

HTTPCookiePropertyKey.domain: ".mydomain.de", // fill up width my domain

HTTPCookiePropertyKey.path: "/",

HTTPCookiePropertyKey.name: "testcookie",

HTTPCookiePropertyKey.value: "test",

HTTPCookiePropertyKey.secure: "TRUE",

HTTPCookiePropertyKey.expires: NSDate(timeIntervalSinceNow: ExpTime),

HTTPCookiePropertyKey.version: "0"

]

if #available(iOS 11, *) {

let wkWebView = WKWebView()

let cookiesStore = wkWebView.configuration.websiteDataStore.httpCookieStore

if let cookie = HTTPCookie(properties: cookies) {

cookiesStore.setCookie(cookie, completionHandler: {

print ("cookie added: \(cookie.name)")

})

// print("GET COOKIE \(HTTPCookieStorage.shared.cookies)")

}

else {

print("set cookie failed")

}

}

// This is our WKHTTPCookieStoreObserver implementation. Note that "cookiesDidChange" isn't called

// even after several calls to setCookie above.

class OnShiftWKHTTPCookieStoreObserver: NSObject, WKHTTPCookieStoreObserver {

@available(iOS 11.0, *)

func cookiesDidChange(in cookieStore: WKHTTPCookieStore) {

cookieStore.getAllCookies({(cookies: [HTTPCookie]) in

cookies.forEach({(cookie: HTTPCookie) in

print("COOKIE name: \(cookie.name) domain: \(cookie.domain) value: \(cookie.value)")

})

})

}

}

Read Cookie with PHP from WebView (iOs Xcode/Swift)
 
 
Q