Having trouble retrieving cookie from UIWebview browser using the document.cookie method

In my iOS application, I am incorporating html content within a webview. This html is a login page where user credentials are sent to the server via ajax and an authentication success or failure response is received. The response includes a cookie in the Set-Cookie header field that I can extract on the Objective-C side as follows:

NSDictionary* headers = [(NSHTTPURLResponse *)response allHeaderFields]; 
NSString *strCookies = [headers valueForKey:@"Set-Cookie"]; 
NSLog(@"cookies -> %@", strCookies);

The response also contains a responseText that I use within the browser like this:

Ajax("/login", "POST", { onsuccess : function(response){ 
       /* Do HTML stuff with response */ }  
});

However, I require the cookie for making subsequent ajax requests after the login page. Despite this, the cookie does not get set in the webview browser. When attempting to access it using document.cookie, an empty string is returned. Although I can display all response headers using xhr.getAllResponseHeaders(), I am unable to retrieve the Set-Cookie field from the xhr object.

Is there a way to set the cookie inside the webview, or do I have no option but to conduct all ajax requests through Obj-C?

Answer №1

One way to assign the cookie is by including it in the request sent to the webview.

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:yourURL];
[request setValue:yourCookie forHTTPHeaderField:@"Cookie"];

This action can be performed within the UIWebview delegate shouldStartLoadWithRequest method.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Try utilizing the 'id name ends with' parameter within the on() function for creating dynamic bindings

I need help with dynamically binding the change event to multiple select input fields that are generated within my form. The item field is generated with IDs like: id="id_form-0-item" id="id_form-1-item" id="id_form-2-item" ... Although I have attempte ...

Using Jquery to monitor input field modifications following a background ajax refresh

I am facing a challenge with my two JQuery functions. The first function executes an ajax load based on the user's selection from a drop-down menu, and then updates an input field according to the data returned. This function is working correctly. // ...

center text above images in flexbox when page is resized

When resizing the page, I'm facing an issue where the links overlap with the images in a flexbox layout. It seems like the padding and margin between the images and links are not working due to them not being "related" or connected. I believe I need t ...

My array is experiencing issues due to the UITableView indexPath

After attempting to access the SECOND item in my array, I encountered an error. Strangely, when I access the first item in the array within my table view cell, everything seems fine. However, as soon as I try to access the 2nd cell/item in the array, I rec ...

Using AngularJS to pass the output of a unique filter to another custom filter

I have successfully developed two custom filters and am attempting to utilize them both within an ng-repeat loop. Is there a way for me to pass the output of the first filter as an input for the second one? I attempted using 'as' keyword in ng- ...

Difficulty encountered while AngularJS is integrating ASP.NET PartialView

I am facing an issue with processing HTML obtained from a PartialView Ajax call in AngularJS. The complication arises from the fact that some JavaScript code is updating the element.html property after the page has loaded. Unfortunately, I am unable to mod ...

Include category to the smallest element

I am attempting to use JQuery to find the height of the tallest element and then add that height to other elements that are shorter. My goal is to assign the class, main-nav-special-padding, to these shorter elements using my current JQuery code. I tried t ...

The function will not be triggered when the form is submitted

I've set up this form to send data to the server-side. It's built in HTML/CSS with AngularJS as well. I made sure that the button is placed inside the form, a common mistake that can cause issues. However, despite my efforts, the function "onAddE ...

Managing channel permissions in Discord.js

Having trouble creating a new channel and need some guidance. I am trying to use the following code: message.guild.channels.create('channel name', { type: 'voice', permissionOverwrites: [ { id: some_id, deny: [&ap ...

What is the best way to organize a complicated array of arrays in JavaScript?

Within my code, there exists an array known as generationArray. Each array contained within generationArray consists of 252 elements. The first 250 elements serve as internal coding data, while the final two positions are designated for sorting criteria. T ...

Avoid having Vue CLI delete all files in the dist folder

As I work on my Vue project, I am facing a challenge in syncing the dist folder with Git. Previously, this process ran smoothly when using webpack. However, after transitioning to @vue/cli and creating my project with vue create myProject instead of vue in ...

Managing callback functions within a for loop in Node.js: strategies and best practices

My mongoose schema looks like this: skills : [ { name : {type : String}, count : {type :Number, default:0} } ] I am currently executing an update query using a for loop in order to update an array of documents. ...

Formatting DateTime in Angular from an API

After confirming that the back end Java Rest API is indeed sending the correct date time format: 2018-05-17 19:08:25.203 Upon arrival to my Angular service, the date format mysteriously transforms into a large number: 1526555305203 The code snippet bel ...

Retrieving the selected value from a dropdown menu before it is altered using vanilla JavaScript

I am trying to implement a feature where the previous value of a dropdown is captured when it is changed. Essentially, I need to compare the previous and current values of the dropdown and perform an action based on that information. For example, if Option ...

Building HTML tables with JQuery for handling large datasets of over 4,000 rows and beyond

My goal is to create a table with 4000+ rows and 18 columns displaying parsed pages from my site. I need it all on one page for the following reasons: I require the use of filters in the table I need to be able to sort by more than 5 columns Every cell ...

`GatheringformData() fails to retrieve information after adding new information`

The problem here is that the formData object is not appending anything. What should I do to fix this? $('#btnSave').click(function () { var form = $("#myForm"); var formData = new FormData($(form)[0]); ...

Update Anchor Tag upon Every Click using Javascript

I am having issues with my ecommerce site where every time a user clicks on "Add to Pallet" (we refer to it as a pallet instead of a cart), the page refreshes and positions itself to the div of that product. You can try it out on our website: . This functi ...

An error occurred when trying to reopen a BrowserWindow after clicking a button in Electron, resulting in the destruction

Exploring the world of the Electron Framework, I am currently working on creating a basic desktop application. However, I have encountered an issue. Whenever I open a new window in my Electron app and then try to close it using the menu bar close button, ...

Uncovering the exported function objects for unit testing with jest: A comprehensive guide

Can you guide me on accessing objects of exported functions for unit testing with jest (node JS)? I need a way to access these objects and test them effectively. Any assistance would be greatly appreciated. //function.js async function createTempUsageSt ...

Convert a single JSON array into two separate jquery arrays

I'm currently working on extracting data from a JSON file using my jQuery script. Below is my jQuery code snippet: $.ajax({ type: "GET", url: "settings.json", dataType: "json", cache: false, error: function(){ $('.sl ...