A method, apparatus, and storage medium for accessing local files based on WebView.

By establishing a mapping relationship between local files and URLs and using encryption algorithms, the problem of mobile web pages being unable to securely access local files was solved, achieving secure access and data isolation, and improving user experience and data security.

CN117194830BActive Publication Date: 2026-03-13BEIJING MOMO INFORMATION TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-05-25
Publication Date
2026-03-13

AI Technical Summary

Technical Problem

Mobile web pages cannot securely access and retrieve local files without requiring users to actively trigger them, resulting in a poor user experience. Existing technologies lack data security isolation design.

Method used

By constructing a mapping relationship between local files and URLs, generating local file protocol addresses, and using encryption algorithms to form a whitelist, WebView intercepts web page requests, achieving data isolation and secure access.

Benefits of technology

It enables mobile web pages to securely access local files without user intervention, improving user experience and enhancing data security and privacy protection.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure HDA0004250670140000011
    Figure HDA0004250670140000011
  • Figure HDA0004250670140000021
    Figure HDA0004250670140000021
  • Figure HDA0004250670140000031
    Figure HDA0004250670140000031
Patent Text Reader

Abstract

This application provides a method for accessing local files based on WebView, comprising: obtaining the local path of a local file of a webpage to be returned; converting the local path into a local file protocol address; wherein, the local path and the local file protocol address are mapped one-to-one; returning the local file protocol address to the webpage; the webpage requesting the local file protocol address of at least one local file; WebView intercepting each request for a local file initiated by the webpage, and if an association between a local file and a webpage URL exists in a first mapping library, then the local file is obtained based on the local file protocol address and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol; wherein, the first mapping library is a mapping between URLs and the corresponding allowed local files for upload. This invention solves the problem that webpages are unable to actively obtain local files due to the sandbox mechanism, enabling secure access and retrieval of local files on mobile devices without introducing triggers, thus improving the user experience.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of mobile Internet, and in particular to a method, apparatus and storage medium for accessing local files based on WebView. Background Technology

[0002] Browsers employ strict sandbox mechanisms to prevent web pages from arbitrarily accessing and retrieving local files, thus ensuring the security of local files and protecting them from attacks that might be launched when the browser displays insecure web pages. Users actively triggering system files through web page input controls is the primary way to allow web pages to access user files. While this active triggering method often satisfies the requirements of desktop web pages for accessing and retrieving local files, on mobile devices, many businesses prefer not to require users to actively trigger access to specific files each time, as this significantly degrades the user experience. WebView, which inherits the desktop sandbox mechanism, cannot directly and dynamically access images, videos, and other file data stored locally on the user's phone. Therefore, business models that rely on local file data such as photos, videos, and audio files cannot be implemented on the web page. Summary of the Invention

[0003] In view of this, this application provides a method, apparatus and storage medium for accessing local files based on WebView, enabling mobile web pages to securely access and obtain local files on mobile devices without triggering any events, thereby improving the user experience.

[0004] The first aspect of this invention provides a method for accessing local files based on WebView, comprising:

[0005] Get the local path of the local file of the webpage to be returned;

[0006] Convert the local path into a local file protocol address;

[0007] The local path is mapped one-to-one with the local file protocol address;

[0008] Return the local file protocol address to the webpage;

[0009] The webpage requests at least one local file protocol address of the local file;

[0010] WebView intercepts every request for the local file initiated by the webpage. If an association between the local file and the webpage's URL exists in the first mapping library, the local file is retrieved based on the local file's protocol address, and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol.

[0011] The first mapping library is a mapping between a URL and the local files that are allowed to be uploaded to that URL.

[0012] In some embodiments, the method for accessing local files based on WebView further includes: encrypting the local file protocol address converted from the local path to obtain an encrypted local file protocol address, wherein the encryption uses a random encryption algorithm; recording the encryption algorithm used for the local file protocol address, or recording the encryption algorithm used for the local file.

[0013] In some embodiments, the first mapping library includes a first sub-mapping library and a second sub-mapping library;

[0014] The first sub-mapping library is a one-to-one mapping relationship between the local file and the encrypted local file protocol address, or the first sub-mapping library is a one-to-one mapping relationship between the local file and the method of obtaining the encrypted local file protocol address;

[0015] The second sub-mapping library represents a one-to-one or one-to-many mapping relationship between the URL and the local file.

[0016] In some embodiments, for each request to the local file initiated by the webpage, it is determined whether there is a mapping relationship between the URL corresponding to the webpage and the local file in the second sub-mapping library;

[0017] If the URL corresponding to the webpage and the local file have a mapping relationship in the second sub-mapping library...

[0018] Determine whether a mapping relationship exists between the local file and the encrypted local file protocol address in the first sub-mapping library.

[0019] Otherwise, the webpage is loaded based on the WebView network protocol;

[0020] If the local file and the encrypted local file protocol address have a mapping relationship in the first sub-mapping library.

[0021] The local file is obtained based on the encrypted local file protocol address, and the webpage is loaded.

[0022] Otherwise, the webpage is loaded based on the WebView network protocol;

[0023] or,

[0024] Determine whether a mapping relationship exists between the local file and the encrypted local file protocol address in the first sub-mapping library;

[0025] If there is a mapping relationship between the local file and the encrypted local file protocol address in the first sub-mapping library, determine whether there is a mapping relationship between the URL corresponding to the webpage and the local file in the second sub-mapping library; otherwise, load the webpage based on the WebView network protocol.

[0026] If the URL corresponding to the webpage and the local file have a mapping relationship in the second sub-mapping library, the local file is obtained based on the encrypted local file protocol address, and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol.

[0027] In some embodiments, if the webpage needs to obtain more than one local file, the WebView network I / O multithreading intercepts the requests initiated by the webpage for more than one local file, and performs multithreaded judgment and obtains the local file based on the judgment result to load the webpage, or loads the webpage based on the WebView network protocol.

[0028] In some embodiments, the method for converting the local path into the local file protocol address is as follows: adding a header indicating the protocol supported by WebView before the local path of the local file; the file protocol address retains the same filename suffix as the local file.

[0029] In some embodiments, the method for obtaining the local path of the local file of the webpage to be returned is as follows:

[0030] The local path of the local file of the requested webpage can be obtained through communication between JavaScript and native code, or through global variables.

[0031] In some embodiments, the method for returning the local file protocol address to the webpage is as follows:

[0032] The local file protocol address is notified to the webpage via a NativeJSI JavaScript callback, or set to the webpage's JavaScript global variable via the WebView's JavaScript API.

[0033] A second aspect of the present invention provides an apparatus for accessing local files based on WebView, comprising:

[0034] A local path acquisition unit is configured to acquire the local path of the local file of the webpage to be returned.

[0035] Address translation unit, the address translation unit is configured to translate the local path into a local file protocol address;

[0036] The local path is mapped one-to-one with the local file protocol address;

[0037] A webpage loading unit, configured to return the local file protocol address to the webpage; and configured to execute the webpage request at least one of the local file protocol addresses of the local file;

[0038] The judgment unit is configured to intercept every request for the local file initiated by the webpage in the WebView. If there is an association between the local file and the URL of the webpage in the first mapping library, the local file is obtained based on the local file protocol address and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol.

[0039] The first mapping library is a mapping of URLs to the local files that are allowed to be uploaded to the URLs.

[0040] A third aspect of the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements the steps of the method for accessing local files based on WebView as described in any of the preceding claims.

[0041] This invention constructs a logic for generating local file protocol addresses corresponding to local files, and based on this, sets a mapping relationship between different URLs and the local files allowed to be uploaded to different URLs. This supports data isolation at the WebView page level, enabling mobile web pages to securely access and obtain local files on mobile devices without triggering any issues, thus improving the user experience. Attached Figure Description

[0042] Figure 1 A flowchart illustrating a method for accessing local files based on WebView, as shown in an exemplary embodiment.

[0043] Figure 2 A flowchart illustrating the determination process of a method for accessing local files based on WebView, as shown in an exemplary embodiment.

[0044] Figure 3 A flowchart illustrating the determination process of a method for accessing local files based on WebView, as shown in another exemplary embodiment.

[0045] Figure 4 This is a schematic block diagram illustrating an apparatus for accessing local files based on WebView, as shown in an exemplary embodiment. Detailed Implementation

[0046] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.

[0047] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a,” “the,” and “the” used in this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.

[0048] It should be understood that although the terms first, second, third, etc., may be used in this application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this application, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to determination."

[0049] Currently, the industry mainly uses the following two methods to access local files based on WebView:

[0050] First, establish a local HTTP file proxy server for mobile devices, allowing all applications or WebView web pages to access the local directory of static resources via the HTTP protocol.

[0051] Secondly, it allows clients to access local files by intercepting WebView domain network requests. This is generally applicable to offline local file cache for files containing URL network resource addresses.

[0052] However, the first method lacks data security isolation design and is usually only suitable for the development and debugging stage of mobile applications. The second method is not suitable for web pages to actively access local files such as local photos, audio and video files on mobile phones, resulting in a poor user experience and also lacking data security isolation design.

[0053] In view of this, the present invention establishes a mapping relationship between specified web pages that can be uploaded without active triggering by setting a whitelist of network URLs and local file protocol addresses formed based on local file paths. By using network requests, it can securely access local files while avoiding browser sandbox restrictions and meeting file security requirements.

[0054] Figure 1 This is a flowchart illustrating a method for accessing local files based on WebView, as shown in an exemplary embodiment of the present invention.

[0055] Please refer to Figure 1 A method for accessing local files based on WebView includes the following steps:

[0056] S101. Obtain the local path of the local file of the webpage to be returned.

[0057] When a business application requires access to local files, the webpage requests a list of local files related to the business logic to determine their local paths. In some implementations, the webpage can request the list of local files related to the business logic via JavaScript-to-Native communication, or by obtaining the requested list of local files and their local paths to the webpage to be returned through global variables.

[0058] Specifically, the list of local files and the local paths of the local files to be returned on the webpage can be obtained through JavaScript-to-Native communication, for example, using native_jsi.getFileList; or the list of local files and the local paths of the local files to be returned on the webpage can be obtained through global variables, for example, using global_local_files.

[0059] Of course, the above is only an example of how to obtain the local file list and the local path of the local file of the webpage to be returned. It is not intended to be the only way to trigger and obtain the local file list and the local path of the local file of the webpage to be returned when the business needs to obtain local files.

[0060] S102. Convert the local path into a local file protocol address. The local path and the local file protocol address are mapped one-to-one.

[0061] Taking the local file list and local path of the local file of the webpage to be returned obtained by S101 through the JavaScript to Native communication method as an example, after receiving the local file list and local path of the local file of the webpage to be returned returned by Native or server-side business, the local path of the local file of all the webpage to be returned is converted into local file protocol address in the form of a list.

[0062] For example, if the local file to be returned for the webpage is a JPG image located in mifile, with the local path address myfile.local.com / 274F6C49B3E31A0C.jpg, by adding a header indicating the protocol supported by WebView before the local file path, the converted local file protocol address can be: mkfile: / / myfile.local.com / 274F6C49B3E31A0C.jpg, http: / / myfile.local.com / 274F6C49B3E31A0C.jpg, etc.

[0063] For example, if the local file to be returned to the webpage is a food image in MP4 format located in mifile, and the local path of the image is myfile.local.com / 234DJCNU4290CL23.mp4, by adding a header indicating the protocol supported by WebView before the local file path, the converted local file protocol address can be: mkfile: / / myfile.local.com / 234DJCNU4290CL23.mp4, http: / / myfile.local.com / 234DJCNU4290CL23.mp4, etc.

[0064] To ensure that WebView can easily read and determine the appropriate plugin to use in subsequent steps, the file protocol address retains the same filename suffix as the local file, such as common local file suffixes like jpg, png, mp4, mov, and mp3. When the local file is returned to WebView as a binary stream, WebView can determine the plugin to be invoked for reading and displaying by the suffix after the dot in the file protocol address, avoiding unnecessary redundant steps in file reading and webpage display.

[0065] The local file protocol address formed by the above method is mapped one-to-one with the local path of the local file, so that when a local file is retrieved through the local file protocol address in the future, the corresponding unique local file is obtained.

[0066] Understandably, to improve data security and protect user privacy, an encryption process can be set up during the generation of local file protocol addresses. The encrypted local file protocol address is then used for identification in the first mapping library, which is the whitelist, after being intercepted by WebView in step S105.

[0067] The following is a feasible method for generating local file protocol addresses that include an encryption process.

[0068] Another mapping library is set up to record the random encryption method used by local files, so that in step S105, WebView intercepts every request for the local file initiated by the webpage, and when it determines that there is an association between the local file and the URL of the webpage in the first mapping library, it establishes an association between the random encryption method used by the local file and the URL of the webpage.

[0069] The local protocol address is converted into an encrypted local protocol address using a random encryption method. Since there is a one-to-one mapping between the local path and the local file protocol address, and this mapping is passed along with the random encryption method, the local file and the encrypted local protocol address are also in a one-to-one mapping relationship. This ensures that when retrieving the local file using the encrypted local file protocol address, the corresponding unique local file is obtained.

[0070] For example, the encryption method can be chosen from a wide range of encryption algorithms to increase the randomness of the encryption process and make the encrypted local protocol address more unpredictable. Examples include hash-based encryption algorithms such as MD, SHA, and HMAC; symmetric encryption algorithms such as DES and AES; and asymmetric encryption algorithms such as DSA, ECC, and RSA. More specifically, MD can preferably be MD2, MD4, or MD5; SHA can preferably be SHA-0, SHA-1, SHA-2, SHA-224, SHA-256, SHA-384, or SHA-512; HMAC can preferably be HMACMD2, HMACMD4, HMACMD5, HMACSHA1, HMACSHA256, HMACSHA384, HMACSHA512, or HMACSHA224; and ECC using elliptic curves can preferably be ECC, ECDSA, or ECDHE, with parameter systems including secp192k1, secp224k1, secp256k1, secp384r1, or secp521r1.

[0071] By randomly selecting any local file protocol address from a rich library of encryption algorithms and converting it into an encrypted local file protocol address, it becomes extremely difficult to obtain the decrypted local file protocol path and the local file itself without knowing the mapping relationship of the random encryption method used. In other words, the use of both random encryption and the encryption method itself constructs a double encryption process, ensuring that even if someone constructs a local file protocol address using the local file address, they cannot read the local file through a webpage without knowing the mapping relationship of the random encryption method used. This double encryption method further guarantees the security of user data.

[0072] S103. Return the local file protocol address to the webpage.

[0073] In some embodiments corresponding to S101, the local file protocol address is returned to the webpage: either through JavaScript-to-Native communication or through global variables.

[0074] Specifically, communication between JavaScript and Native can be achieved through methods such as using JavaScript callbacks to notify the webpage via native_jsi.getFileList; or through global variables, such as the WebView's API evaluateJavaScript setting to global JavaScript variables on the webpage, or even more specifically, global_local_files.

[0075] Similarly, the above is merely an implementation example of returning the local file protocol address to the webpage, and is not intended to limit the only two ways to return the local file protocol address to the webpage.

[0076] Of course, in an embodiment that adds an encryption process, this step involves returning the encrypted local file protocol address to the webpage.

[0077] S104, The webpage requests at least one local file protocol address of the local file.

[0078] After obtaining the list of local file protocol addresses, the webpage sends a request to the local file protocol address corresponding to the local file to be read, based on the business needs of the webpage. Similarly, in the embodiment with added encryption, this step involves sending a request to the encrypted local file protocol address corresponding to the local file to be read.

[0079] It is understandable that when a webpage requests the local file protocol address of a local file, it uses a predefined protocol to request the file data resource.

[0080] S105. WebView intercepts each request for the local file initiated by the webpage. If an association between the local file and the webpage's URL exists in the first mapping library, the local file is retrieved based on the local file's protocol address, and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol. The first mapping library is a mapping between URLs and the allowed local files to be uploaded corresponding to those URLs.

[0081] By setting up a first mapping library that maps different URLs to the corresponding allowed local files for those URLs, WebView supports webpage-level data isolation, ensuring complete isolation of file access capabilities between different URLs. For secure URLs, if a user has a history of authorizing access to specific local files at those URLs, subsequent browsing of those URLs over extended periods and with any number of visits will allow access to the user's local files based on the business requirements of those URLs without requiring user intervention or other redundant operations, thus improving user experience. For insecure URLs or unfamiliar URLs where no access to any local files has been authorized, this embodiment can effectively intercept access based on WebView, preventing the reading of the user's local files on the mobile terminal, ensuring high security and strong privacy.

[0082] In some embodiments, WebView intercepts network requests initiated by web pages, passes the resource URI address to the device as a whitelist of the first mapping library, and performs identification and judgment. If the resource URI address matches the whitelist, the mapped local file path of the mobile phone is obtained, the local file data is read, and the local file is returned to WebView in binary stream. If the resource URI address does not match the whitelist, the web page is loaded using the default network protocol format of WebView.

[0083] By setting an interception mechanism, allowing binary streams as the return method for local files has high transmission efficiency. Compared with other transmission mechanisms such as strings, it avoids redundant information and improves transmission speed.

[0084] If the webpage needs to obtain more than one local file, the WebView network I / O multithreading intercepts the webpage's requests for more than one local file, performs multithreaded judgment, and obtains the local file based on the judgment result to load the webpage, or loads the webpage based on the WebView network protocol.

[0085] This embodiment loads local file resources through the network layer, making full use of WebView's multi-threaded network I / O processing capabilities to avoid blocking the JavaScript execution thread when local file data is passed to JavaScript.

[0086] The following is a feasible method for intercepting and judging the encryption process in WebView and obtaining local files based on the judgment result.

[0087] Understandably, in the local file retrieval method that includes an encryption process, after WebView intercepts the network request, it first identifies the encrypted local file protocol address, which is the address of an encrypted proxy domain name.

[0088] To establish a mapping relationship between the random encryption algorithm and the encrypted file, the first mapping library is further configured to include a first sub-mapping library and a second sub-mapping library.

[0089] Specifically, the first sub-mapping library is a one-to-one mapping relationship between the local file and the encrypted local file protocol address, or the first sub-mapping library is a one-to-one mapping relationship between the local file and the method of obtaining the encrypted local file protocol address.

[0090] Specifically, the second sub-mapping library represents a one-to-one or one-to-many mapping relationship between the URL and the local file.

[0091] Thus, through the first sub-mapping library, the process of generating the encrypted local protocol address by using a random encryption method to make the encrypted local protocol address more unpredictable, and the process of reading the encrypted local file protocol address, can form a complete cooperation.

[0092] In some embodiments, after WebView intercepts each request for the local file initiated by the webpage, it performs a subordinate judgment on each request for the local file initiated by the webpage.

[0093] Figure 2 This is a flowchart illustrating the determination process of a method for accessing local files based on WebView, as shown in an exemplary embodiment.

[0094] Please refer to Figure 2 The decision-making process for methods that access local files based on WebView includes:

[0095] S201, First judgment process: Determine whether there is a mapping relationship between the URL corresponding to the webpage and the local file in the second sub-mapping library.

[0096] S2011. If the URL corresponding to the webpage and the local file have a mapping relationship in the second sub-mapping library, then proceed to S202, the second judgment process.

[0097] S2012. If the URL corresponding to the webpage and the local file do not have a mapping relationship in the second sub-mapping library, then proceed to S2032.

[0098] S202, Second judgment process: Determine whether there is a mapping relationship between the local file and the encrypted local file protocol address in the first sub-mapping library.

[0099] S2021. If the local file and the encrypted local file protocol address have a mapping relationship in the first sub-mapping library, then proceed to S2031.

[0100] S2022 If there is no mapping relationship between the local file and the encrypted local file protocol address in the first sub-mapping library, then proceed to S2032.

[0101] S2031. Obtain the local file based on the encrypted local file protocol address, and load the webpage.

[0102] S2032. Load the webpage based on the WebView network protocol.

[0103] Figure 3 A flowchart illustrating the determination process of a method for accessing local files based on WebView, as shown in another exemplary embodiment.

[0104] Please refer to Figure 3 The decision-making process for methods that access local files based on WebView includes:

[0105] S301. First judgment process: Determine whether there is a mapping relationship between the local file and the encrypted local file protocol address in the first sub-mapping library.

[0106] S3011. If the local file and the encrypted local file protocol address have a mapping relationship in the first sub-mapping library, then proceed to S202, the second judgment process.

[0107] S3012. If there is no mapping relationship between the local file and the encrypted local file protocol address in the first sub-mapping library, load the webpage based on the WebView network protocol.

[0108] S302, Second Judgment Process: Determine whether there is a mapping relationship between the URL corresponding to the webpage and the local file in the second sub-mapping library.

[0109] S3021. If the URL corresponding to the webpage and the local file have a mapping relationship in the second sub-mapping library, obtain the local file based on the encrypted local file protocol address and load the webpage.

[0110] S3022. If the URL corresponding to the webpage and the local file do not have a mapping relationship in the second sub-mapping library, then the webpage is loaded based on the WebView network protocol.

[0111] By applying the above judgment steps to the two sub-mapping libraries, the system determines whether a random encryption algorithm corresponds to the local file based on whether a mapping relationship exists in the first sub-mapping library, thus preventing the path of the local file from being tampered with. Based on whether a mapping relationship exists in the second mapping library, the system determines which local files corresponding to the specified URL are whitelisted and can be obtained, while other local files cannot be obtained. This solves the problem of isolating local files developed for different URLs, improves local data security, and protects user privacy.

[0112] Understandably, after obtaining local files, the webpage reads images, videos, audio files, and completes the necessary upper-level business logic required by the webpage.

[0113] In summary, the exemplary embodiment supports WebView page-level data isolation by constructing logic to generate the local file protocol address corresponding to the local file, and based on this, setting a mapping relationship between different URLs and the allowed local files to be uploaded to those URLs. This allows mobile web pages to securely access and retrieve local files on the mobile device without triggering any malicious activity, thus improving the user experience. The exemplary embodiment also improves data security by setting the randomness of the encryption method for the local file's local path to the encrypted local protocol address, effectively preventing access from tampered URLs and ensuring user privacy. Furthermore, the exemplary embodiment loads local file resources through the network layer, fully utilizing WebView's multi-threaded network I / O processing capabilities and avoiding blocking the JavaScript execution thread when data is passed to JavaScript.

[0114] The preceding section provides a detailed description of the method for accessing local files based on WebView provided by the present invention. Corresponding to the aforementioned embodiments of the method for accessing local files based on WebView, the following section will describe the apparatus for the method for accessing local files based on WebView provided by the present invention.

[0115] Figure 4 This is a schematic block diagram of an apparatus for accessing local files based on WebView, as an exemplary embodiment.

[0116] Please refer to Figure 4 Devices that allow access to local files via WebView include:

[0117] Local path acquisition unit 401 is configured to acquire the local path of the local file of the webpage to be returned.

[0118] Address translation unit 402, configured to translate the local path into a local file protocol address; wherein, the local path and the local file protocol address are mapped one-to-one;

[0119] A webpage loading unit 403 is configured to return the local file protocol address to the webpage; and is configured to execute the webpage request for at least one of the local file protocol addresses of the local file.

[0120] The judgment unit 404 is configured to intercept every request for the local file initiated by the webpage through WebView. If there is an association between the local file and the URL of the webpage in the first mapping library, the local file is obtained based on the local file protocol address and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol.

[0121] The first mapping library 405 is a mapping of URLs to the local files that are allowed to be uploaded to the URLs.

[0122] The apparatus of this embodiment can be used to perform... Figure 1 The technical solutions of the method embodiments shown are similar in principle and in effect, and will not be described again here.

[0123] Similar to the corresponding WebView-based method for accessing local files, the exemplary embodiment constructs logic for generating the local file protocol address corresponding to the local file, and based on this, sets a mapping relationship between different URLs and the allowed local files to be uploaded to those URLs. This supports WebView page-level data isolation, enabling mobile web pages to securely access and retrieve local files on mobile devices without triggering any malicious activity, thus improving user experience. The exemplary embodiment sets the randomness of the encryption method for the local path of the local file to the encrypted local protocol address, enhancing data security, effectively preventing access from tampered URLs, and ensuring user privacy. The exemplary embodiment loads local file resources through the network layer, fully utilizing WebView's multi-threaded network I / O processing capabilities and avoiding blocking the JavaScript execution thread when data is passed to JavaScript.

[0124] This application also provides a computer-readable storage medium having a computer program stored thereon, wherein the program, when executed by a processor, implements the steps of any of the information display methods provided in this application.

[0125] Specifically, computer-readable media suitable for storing computer program instructions and data include all forms of non-volatile memory, media, and memory devices, such as semiconductor memory devices (e.g., EPROM, EEPROM, and flash memory devices), magnetic disks (e.g., internal hard disks or removable disks), magneto-optical disks, and CD-ROM and DVD-ROM disks.

[0126] The above description is merely a preferred embodiment of this application and is not intended to limit this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A method for accessing local files based on WebView, characterized in that, The method comprises: obtaining a local path of a local file to be returned to a webpage; converting the local path into a local file protocol address, encrypting the local file protocol address converted from the local path, and obtaining an encrypted local file protocol address; wherein the local path and the local file protocol address are in one-to-one mapping; returning the local file protocol address to the webpage; the webpage requesting the local file protocol address of at least one local file; the WebView intercepting each request for the local file initiated by the webpage, and if an association exists between the local file and the webpage in a first mapping library, obtaining the local file based on the local file protocol address and loading the webpage; otherwise, loading the webpage based on the WebView network protocol; wherein the first mapping library is a mapping of a webpage and a local file allowed to be uploaded corresponding to the webpage, and the first mapping library comprises a first sub-mapping library and a second sub-mapping library; if the webpage and the local file have a mapping relationship in the second sub-mapping library, it is determined whether the local file and the encrypted local file protocol address have a mapping relationship in the first sub-mapping library, otherwise the webpage is loaded based on the WebView network protocol; if the local file and the encrypted local file protocol address have a mapping relationship in the first sub-mapping library, the local file is obtained based on the encrypted local file protocol address and the webpage is loaded, otherwise the webpage is loaded based on the WebView network protocol.

2. The method for accessing local files based on WebView according to claim 1, wherein, The method for accessing a local file based on a WebView further comprises: recording an encryption algorithm corresponding to the local file protocol address, or recording an encryption algorithm corresponding to the local file.

3. The method for accessing a local file based on a WebView according to claim 1, wherein: the first sub-mapping library is a one-to-one mapping relationship between the local file and the encrypted local file protocol address, or the first sub-mapping library is a one-to-one mapping relationship between the local file and an obtaining method of the encrypted local file protocol address; the second sub-mapping library is a one-to-one or one-to-many mapping relationship between the webpage and the local file.

4. The method for accessing local files based on WebView according to claim 1, wherein, If the webpage needs to obtain more than one local file, the WebView network IO multithreading is used to intercept the request for more than one local file initiated by the webpage, and the multithreading is executed to determine and obtain the local file based on the determination result to load the webpage, or the webpage is loaded based on the WebView network protocol.

5. The method for accessing local files based on WebView according to claim 1, wherein, The method for converting the local path into the local file protocol address comprises: adding a header representing a protocol supported by the WebView in front of the local path of the local file; and the file protocol address retains the same file name suffix as the local file.

6. The method for accessing local files based on WebView according to claim 1, wherein, The method for obtaining the local path of the local file to be returned to the webpage comprises: The local path of the local file requested by the webpage is obtained through JavaScript-to-Native communication or through a global variable.

7. The method for accessing local files based on WebView according to claim 1, wherein, The method for returning the local file protocol address to the webpage is: The local file protocol address is notified to the webpage through JavaScript callback of NativeJSI or is set to the webpage JavaScript global variable through an API for executing JavaScript of the WebView.

8. An apparatus for accessing local files based on a WebView, the apparatus comprising: The method comprises: a local path obtaining unit configured to obtain a local path of a local file of a webpage to be returned; an address converting unit configured to convert the local path into a local file protocol address, encrypt the local file protocol address converted from the local path to obtain an encrypted local file protocol address, and wherein the local path and the local file protocol address are in one-to-one mapping; a webpage loading unit configured to return the local file protocol address to the webpage and to execute the webpage to request the local file protocol address of at least one local file; a judging unit configured to execute WebView to intercept each request of the local file initiated by the webpage, and if the local file and the URL of the webpage exist in association in a first mapping library, to obtain the local file based on the local file protocol address and load the webpage; otherwise, to load the webpage based on a WebView network protocol; the first mapping library is a mapping of a URL and a local file allowed to be uploaded corresponding to the URL, and the first mapping library comprises a first sub-mapping library and a second sub-mapping library; if the URL corresponding to the webpage and the local file exist in mapping relationship in the second sub-mapping library, it is determined whether the local file and the encrypted local file protocol address exist in mapping relationship in the first sub-mapping library; otherwise, the webpage is loaded based on the WebView network protocol; if the local file and the encrypted local file protocol address exist in mapping relationship in the first sub-mapping library, the local file is obtained based on the encrypted local file protocol address and the webpage is loaded; otherwise, the webpage is loaded based on the WebView network protocol.

9. A computer readable storage medium having stored thereon a computer program, characterized in that The program is executed by the processor to implement the steps of the method for accessing a local file based on a WebView according to any one of claims 1-7.

Citation Information

Patent Citations

  • Method and device for controlling URL access on basis of IOS system

    CN105808990A

  • WebView-based data injection method and device, equipment and storage medium

    CN112948727A