UniWebView.Navigation.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // UniWebView.Navigation.cs
  3. // Created by Wang Wei (@onevcat) on 2025-04-19.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. public partial class UniWebView {
  18. /// <summary>
  19. /// The url of current loaded web page.
  20. /// </summary>
  21. public string Url => UniWebViewInterface.GetUrl(listener.Name);
  22. /// <summary>
  23. /// Loads a url in current web view.
  24. /// </summary>
  25. /// <param name="url">The url to be loaded. This url should start with `http://` or `https://` scheme. You could even load a non-ascii url text if it is valid.</param>
  26. /// <param name="skipEncoding">
  27. /// Whether UniWebView should skip encoding the url or not. If set to `false`, UniWebView will try to encode the url parameter before
  28. /// loading it. Otherwise, your original url string will be used as the url if it is valid. Default is `false`.
  29. /// </param>
  30. /// <param name="readAccessURL">
  31. /// The URL to allow read access to. This parameter is only used when loading from the filesystem in iOS, and passed
  32. /// to `loadFileURL:allowingReadAccessToURL:` method of WebKit. By default, the parent folder of the `url` parameter will be read accessible.
  33. /// </param>
  34. public void Load(string url, bool skipEncoding = false, string readAccessURL = null) {
  35. UniWebViewInterface.Load(listener.Name, url, skipEncoding, readAccessURL);
  36. }
  37. /// <summary>
  38. /// Loads an HTML string in current web view.
  39. /// </summary>
  40. /// <param name="htmlString">The HTML string to use as the contents of the webpage.</param>
  41. /// <param name="baseUrl">The url to use as the page's base url.</param>
  42. /// <param name="skipEncoding">
  43. /// Whether UniWebView should skip encoding the baseUrl or not. If set to `false`, UniWebView will try to encode the baseUrl parameter before
  44. /// using it. Otherwise, your original url string will be used as the baseUrl if it is valid. Default is `false`.
  45. /// </param>
  46. public void LoadHTMLString(string htmlString, string baseUrl, bool skipEncoding = false) {
  47. UniWebViewInterface.LoadHTMLString(listener.Name, htmlString, baseUrl, skipEncoding);
  48. }
  49. /// <summary>
  50. /// Reloads the current page.
  51. /// </summary>
  52. public void Reload() {
  53. UniWebViewInterface.Reload(listener.Name);
  54. }
  55. /// <summary>
  56. /// Stops loading all resources on the current page.
  57. /// </summary>
  58. public void Stop() {
  59. UniWebViewInterface.Stop(listener.Name);
  60. }
  61. /// <summary>
  62. /// Gets whether there is a back page in the back-forward list that can be navigated to.
  63. /// </summary>
  64. public bool CanGoBack => UniWebViewInterface.CanGoBack(listener.Name);
  65. /// <summary>
  66. /// Gets whether there is a forward page in the back-forward list that can be navigated to.
  67. /// </summary>
  68. public bool CanGoForward => UniWebViewInterface.CanGoForward(listener.Name);
  69. /// <summary>
  70. /// Navigates to the back item in the back-forward list.
  71. /// </summary>
  72. public void GoBack() {
  73. UniWebViewInterface.GoBack(listener.Name);
  74. }
  75. /// <summary>
  76. /// Navigates to the forward item in the back-forward list.
  77. /// </summary>
  78. public void GoForward() {
  79. UniWebViewInterface.GoForward(listener.Name);
  80. }
  81. /// <summary>
  82. /// Gets a copy of the back-forward list, which is the navigation history for the web view.
  83. /// </summary>
  84. /// <remarks>
  85. /// <para>
  86. /// The back-forward list represents the browsing history in the current web view. It contains information
  87. /// about visited (both back and forward) pages and allows access to any entry in the history by index. This is a
  88. /// snapshot of the history at the time of calling - it won't update automatically with new navigation. Call
  89. /// `CopyBackForwardList` again to get the latest history if necessary.
  90. /// </para>
  91. /// </remarks>
  92. /// <returns>
  93. /// A <see cref="UniWebViewBackForwardList"/> object containing a snapshot of the navigation history.
  94. /// The list provides a read-only record of all web pages visited in this web view.
  95. /// </returns>
  96. public UniWebViewBackForwardList CopyBackForwardList() {
  97. var json = UniWebViewInterface.CopyBackForwardList(listener.Name);
  98. return new UniWebViewBackForwardList(json);
  99. }
  100. /// <summary>
  101. /// Navigates to the specified index in the back-forward list.
  102. ///
  103. /// The index is a zero-based index of the item in the back-forward list. If the index is out of range, this method
  104. /// does nothing.
  105. /// </summary>
  106. /// <param name="index">The zero-based index of the item in the back-forward list.</param>
  107. public void GoToIndexInBackForwardList(int index) {
  108. UniWebViewInterface.GoToIndexInBackForwardList(listener.Name, index);
  109. }
  110. }