WKWebView.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * Copyright (C) 2014 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #import <WebKit/WKFoundation.h>
  26. #if TARGET_OS_IPHONE
  27. #import <UIKit/UIKit.h>
  28. #else
  29. #import <AppKit/AppKit.h>
  30. #endif
  31. NS_ASSUME_NONNULL_BEGIN
  32. @class WKBackForwardList;
  33. @class WKBackForwardListItem;
  34. @class WKNavigation;
  35. @class WKSnapshotConfiguration;
  36. @class WKWebViewConfiguration;
  37. @protocol WKNavigationDelegate;
  38. @protocol WKUIDelegate;
  39. /*!
  40. A WKWebView object displays interactive Web content.
  41. @helperclass @link WKWebViewConfiguration @/link
  42. Used to configure @link WKWebView @/link instances.
  43. */
  44. #if TARGET_OS_IPHONE
  45. WK_EXTERN API_AVAILABLE(macos(10.10), ios(8.0))
  46. @interface WKWebView : UIView
  47. #else
  48. WK_EXTERN API_AVAILABLE(macos(10.10), ios(8.0))
  49. @interface WKWebView : NSView
  50. #endif
  51. /*! @abstract A copy of the configuration with which the web view was
  52. initialized. */
  53. @property (nonatomic, readonly, copy) WKWebViewConfiguration *configuration;
  54. /*! @abstract The web view's navigation delegate. */
  55. @property (nullable, nonatomic, weak) id <WKNavigationDelegate> navigationDelegate;
  56. /*! @abstract The web view's user interface delegate. */
  57. @property (nullable, nonatomic, weak) id <WKUIDelegate> UIDelegate;
  58. /*! @abstract The web view's back-forward list. */
  59. @property (nonatomic, readonly, strong) WKBackForwardList *backForwardList;
  60. /*! @abstract Returns a web view initialized with a specified frame and
  61. configuration.
  62. @param frame The frame for the new web view.
  63. @param configuration The configuration for the new web view.
  64. @result An initialized web view, or nil if the object could not be
  65. initialized.
  66. @discussion This is a designated initializer. You can use
  67. @link -initWithFrame: @/link to initialize an instance with the default
  68. configuration. The initializer copies the specified configuration, so
  69. mutating the configuration after invoking the initializer has no effect
  70. on the web view.
  71. */
  72. - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
  73. - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
  74. /*! @abstract Navigates to a requested URL.
  75. @param request The request specifying the URL to which to navigate.
  76. @result A new navigation for the given request.
  77. */
  78. - (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;
  79. /*! @abstract Navigates to the requested file URL on the filesystem.
  80. @param URL The file URL to which to navigate.
  81. @param readAccessURL The URL to allow read access to.
  82. @discussion If readAccessURL references a single file, only that file may be loaded by WebKit.
  83. If readAccessURL references a directory, files inside that file may be loaded by WebKit.
  84. @result A new navigation for the given file URL.
  85. */
  86. - (nullable WKNavigation *)loadFileURL:(NSURL *)URL allowingReadAccessToURL:(NSURL *)readAccessURL API_AVAILABLE(macos(10.11), ios(9.0));
  87. /*! @abstract Sets the webpage contents and base URL.
  88. @param string The string to use as the contents of the webpage.
  89. @param baseURL A URL that is used to resolve relative URLs within the document.
  90. @result A new navigation.
  91. */
  92. - (nullable WKNavigation *)loadHTMLString:(NSString *)string baseURL:(nullable NSURL *)baseURL;
  93. /*! @abstract Sets the webpage contents and base URL.
  94. @param data The data to use as the contents of the webpage.
  95. @param MIMEType The MIME type of the data.
  96. @param characterEncodingName The data's character encoding name.
  97. @param baseURL A URL that is used to resolve relative URLs within the document.
  98. @result A new navigation.
  99. */
  100. - (nullable WKNavigation *)loadData:(NSData *)data MIMEType:(NSString *)MIMEType characterEncodingName:(NSString *)characterEncodingName baseURL:(NSURL *)baseURL API_AVAILABLE(macos(10.11), ios(9.0));
  101. /*! @abstract Navigates to an item from the back-forward list and sets it
  102. as the current item.
  103. @param item The item to which to navigate. Must be one of the items in the
  104. web view's back-forward list.
  105. @result A new navigation to the requested item, or nil if it is already
  106. the current item or is not part of the web view's back-forward list.
  107. @seealso backForwardList
  108. */
  109. - (nullable WKNavigation *)goToBackForwardListItem:(WKBackForwardListItem *)item;
  110. /*! @abstract The page title.
  111. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  112. for this property.
  113. */
  114. @property (nullable, nonatomic, readonly, copy) NSString *title;
  115. /*! @abstract The active URL.
  116. @discussion This is the URL that should be reflected in the user
  117. interface.
  118. @link WKWebView @/link is key-value observing (KVO) compliant for this
  119. property.
  120. */
  121. @property (nullable, nonatomic, readonly, copy) NSURL *URL;
  122. /*! @abstract A Boolean value indicating whether the view is currently
  123. loading content.
  124. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  125. for this property.
  126. */
  127. @property (nonatomic, readonly, getter=isLoading) BOOL loading;
  128. /*! @abstract An estimate of what fraction of the current navigation has been completed.
  129. @discussion This value ranges from 0.0 to 1.0 based on the total number of
  130. bytes expected to be received, including the main document and all of its
  131. potential subresources. After a navigation completes, the value remains at 1.0
  132. until a new navigation starts, at which point it is reset to 0.0.
  133. @link WKWebView @/link is key-value observing (KVO) compliant for this
  134. property.
  135. */
  136. @property (nonatomic, readonly) double estimatedProgress;
  137. /*! @abstract A Boolean value indicating whether all resources on the page
  138. have been loaded over securely encrypted connections.
  139. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  140. for this property.
  141. */
  142. @property (nonatomic, readonly) BOOL hasOnlySecureContent;
  143. /*! @abstract A SecTrustRef for the currently committed navigation.
  144. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  145. for this property.
  146. */
  147. @property (nonatomic, readonly, nullable) SecTrustRef serverTrust API_AVAILABLE(macos(10.12), ios(10.0));
  148. /*! @abstract A Boolean value indicating whether there is a back item in
  149. the back-forward list that can be navigated to.
  150. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  151. for this property.
  152. @seealso backForwardList.
  153. */
  154. @property (nonatomic, readonly) BOOL canGoBack;
  155. /*! @abstract A Boolean value indicating whether there is a forward item in
  156. the back-forward list that can be navigated to.
  157. @discussion @link WKWebView @/link is key-value observing (KVO) compliant
  158. for this property.
  159. @seealso backForwardList.
  160. */
  161. @property (nonatomic, readonly) BOOL canGoForward;
  162. /*! @abstract Navigates to the back item in the back-forward list.
  163. @result A new navigation to the requested item, or nil if there is no back
  164. item in the back-forward list.
  165. */
  166. - (nullable WKNavigation *)goBack;
  167. /*! @abstract Navigates to the forward item in the back-forward list.
  168. @result A new navigation to the requested item, or nil if there is no
  169. forward item in the back-forward list.
  170. */
  171. - (nullable WKNavigation *)goForward;
  172. /*! @abstract Reloads the current page.
  173. @result A new navigation representing the reload.
  174. */
  175. - (nullable WKNavigation *)reload;
  176. /*! @abstract Reloads the current page, performing end-to-end revalidation
  177. using cache-validating conditionals if possible.
  178. @result A new navigation representing the reload.
  179. */
  180. - (nullable WKNavigation *)reloadFromOrigin;
  181. /*! @abstract Stops loading all resources on the current page.
  182. */
  183. - (void)stopLoading;
  184. /* @abstract Evaluates the given JavaScript string.
  185. @param javaScriptString The JavaScript string to evaluate.
  186. @param completionHandler A block to invoke when script evaluation completes or fails.
  187. @discussion The completionHandler is passed the result of the script evaluation or an error.
  188. */
  189. - (void)evaluateJavaScript:(NSString *)javaScriptString completionHandler:(void (^ _Nullable)(_Nullable id, NSError * _Nullable error))completionHandler;
  190. /*! @abstract Get a snapshot for the visible viewport of WKWebView.
  191. @param snapshotConfiguration An object that specifies how the snapshot is configured.
  192. @param completionHandler A block to invoke when the snapshot is ready.
  193. @discussion If the WKSnapshotConfiguration is nil, the method will snapshot the bounds of the
  194. WKWebView and create an image that is the width of the bounds of the WKWebView and scaled to the
  195. device scale. The completionHandler is passed the image of the viewport contents or an error.
  196. */
  197. #if TARGET_OS_IPHONE
  198. - (void)takeSnapshotWithConfiguration:(nullable WKSnapshotConfiguration *)snapshotConfiguration completionHandler:(void (^)(UIImage * _Nullable snapshotImage, NSError * _Nullable error))completionHandler API_AVAILABLE(ios(11.0));
  199. #else
  200. - (void)takeSnapshotWithConfiguration:(nullable WKSnapshotConfiguration *)snapshotConfiguration completionHandler:(void (^)(NSImage * _Nullable snapshotImage, NSError * _Nullable error))completionHandler API_AVAILABLE(macos(10.13));
  201. #endif
  202. /*! @abstract A Boolean value indicating whether horizontal swipe gestures
  203. will trigger back-forward list navigations.
  204. @discussion The default value is NO.
  205. */
  206. @property (nonatomic) BOOL allowsBackForwardNavigationGestures;
  207. /*! @abstract The custom user agent string or nil if no custom user agent string has been set.
  208. */
  209. @property (nullable, nonatomic, copy) NSString *customUserAgent API_AVAILABLE(macos(10.11), ios(9.0));
  210. /*! @abstract A Boolean value indicating whether link preview is allowed for any
  211. links inside this WKWebView.
  212. @discussion The default value is YES on Mac and iOS.
  213. */
  214. @property (nonatomic) BOOL allowsLinkPreview API_AVAILABLE(macos(10.11), ios(9.0));
  215. #if TARGET_OS_IPHONE
  216. /*! @abstract The scroll view associated with the web view.
  217. */
  218. @property (nonatomic, readonly, strong) UIScrollView *scrollView;
  219. #endif
  220. #if !TARGET_OS_IPHONE
  221. /* @abstract A Boolean value indicating whether magnify gestures will
  222. change the web view's magnification.
  223. @discussion It is possible to set the magnification property even if
  224. allowsMagnification is set to NO.
  225. The default value is NO.
  226. */
  227. @property (nonatomic) BOOL allowsMagnification;
  228. /* @abstract The factor by which the viewport of the page is currently scaled.
  229. @discussion The default value is 1.0.
  230. */
  231. @property (nonatomic) CGFloat magnification;
  232. /* @abstract Scales the page content by a specified factor and centers the
  233. result on a specified point.
  234. * @param magnification The factor by which to scale the content.
  235. * @param point The point (in view space) on which to center magnification.
  236. */
  237. - (void)setMagnification:(CGFloat)magnification centeredAtPoint:(CGPoint)point;
  238. #endif
  239. /* @abstract Checks whether or not WKWebViews handle the given URL scheme by default.
  240. @param scheme The URL scheme to check.
  241. */
  242. + (BOOL)handlesURLScheme:(NSString *)urlScheme API_AVAILABLE(macos(10.13), ios(11.0));
  243. @end
  244. #if !TARGET_OS_IPHONE
  245. @interface WKWebView (WKIBActions) <NSUserInterfaceValidations>
  246. /*! @abstract Action method that navigates to the back item in the
  247. back-forward list.
  248. @param sender The object that sent this message.
  249. */
  250. - (IBAction)goBack:(nullable id)sender;
  251. /*! @abstract Action method that navigates to the forward item in the
  252. back-forward list.
  253. @param sender The object that sent this message.
  254. */
  255. - (IBAction)goForward:(nullable id)sender;
  256. /*! @abstract Action method that reloads the current page.
  257. @param sender The object that sent this message.
  258. */
  259. - (IBAction)reload:(nullable id)sender;
  260. /*! @abstract Action method that reloads the current page, performing
  261. end-to-end revalidation using cache-validating conditionals if possible.
  262. @param sender The object that sent this message.
  263. */
  264. - (IBAction)reloadFromOrigin:(nullable id)sender;
  265. /*! @abstract Action method that stops loading all resources on the current
  266. page.
  267. @param sender The object that sent this message.
  268. */
  269. - (IBAction)stopLoading:(nullable id)sender;
  270. @end
  271. API_AVAILABLE(macos(10.15))
  272. @interface WKWebView (WKNSTextFinderClient) <NSTextFinderClient>
  273. @end
  274. #endif
  275. @interface WKWebView (WKDeprecated)
  276. @property (nonatomic, readonly, copy) NSArray *certificateChain API_DEPRECATED_WITH_REPLACEMENT("serverTrust", macos(10.11, 10.12), ios(9.0, 10.0));
  277. @end
  278. NS_ASSUME_NONNULL_END