IOS development and localStorage/sessionStorage, sessionlocalstorage

Source: Internet
Author: User
Tags sessionstorage

IOS development and localStorage/sessionStorage, sessionlocalstorage

I. Preface

In my recent work, a front-end student told me to clear localStorage. I had no idea about localStorage at the time, so I read the related content at w3c. The following is a brief introduction. It is a common knowledge for iOS developers to popularize H5. You can ignore it directly.

Ii. localStorage & sessionStorage

In HTML5, to store data on the client, HTML provides two new methods to store data on the client:

* LocalStorage: no time limit for data storage.

* SessionStorage: data storage for a session, that is, the sessionStorage on the re-opened page is invalid.

Previously, cookies were used for processing, but cookies were not suitable for storing a large amount of data. Because they were transmitted by every request to the server, the speed was slow and the efficiency was not high. In HTML5, JavaScript is used to access and store data.

1. localStorage

There is no time limit for data stored in localStorage. As long as it is not cleared, it will always exist. The following describes the related methods and usage.

① Determine whether the browser supports localStorage.

We only need to write the following code and then open the webpage in the browser:

<! DOCTYPE html> 

② LocalStorage storage instance

<! DOCTYPE html> 

The html page is now opened directly in the browser. Then refresh. The result is as follows:

I refreshed it 13 times and the result was 13 times. When I close the web page and open it again, the result is as follows:

As you can see, the browser saves the original number of times to open the console and you can see:

③ Store/access localStorage

   

localStorage.setItem("name","zhanggui");localStorage["name"] = "zhanggui";var valure = localStorage.getItem("name");var valure = localStorage["name"];

 

Its storage is similar to the dictionary in iOS. In this way, the key name can be saved to value: zhanggui and then retrieved.

④ Clear localStorage

 

LocalStorage. removeItem ("name"); // clear a localStoragelocalStorage. clear () according to the key value; // All localStorage is cleared here.

 

2. sessionStorage

SessionStorage is similar to localStorage. The only difference is that localStorage exists every time the page is opened, and sessionStorage initializes every time the page is opened. Just like counting the number of page visits:

If sessionStorage is used for storage, the number of visits to the next page is 1, rather than the number of previous requests.

Iii. Support for localStorage & sessionStorage in iOS

Directly write a storage.html file for testing. The code is here:

The test is conducted here. The result is that localStorage is supported. For details, see the code. When I open it for the first time, the running result is as follows:

When I re-open the application, the result is as follows:

When I delete the entire project and reinstall it, the result is one more time.

The method for clearing localStorage is as follows:

 [self.contentWebView stringByEvaluatingJavaScriptFromString:@"localStorage.clear()"];

In this way, localStorage can be cleared.

SessionStorage will not be introduced much. You can test it on your own.

Iv. Conclusion

This is a simple introduction. It is helpful for iOS developers.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.