WKUIDelegate.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. #import <Foundation/Foundation.h>
  27. #import <WebKit/WKPreviewActionItem.h>
  28. NS_ASSUME_NONNULL_BEGIN
  29. @class WKFrameInfo;
  30. @class WKNavigationAction;
  31. @class WKOpenPanelParameters;
  32. @class WKPreviewElementInfo;
  33. @class WKSecurityOrigin;
  34. @class WKWebView;
  35. @class WKWebViewConfiguration;
  36. @class WKWindowFeatures;
  37. #if TARGET_OS_IOS
  38. @class WKContextMenuElementInfo;
  39. @class UIContextMenuConfiguration;
  40. @protocol UIContextMenuInteractionCommitAnimating;
  41. #endif
  42. /*! A class conforming to the WKUIDelegate protocol provides methods for
  43. presenting native UI on behalf of a webpage.
  44. */
  45. @protocol WKUIDelegate <NSObject>
  46. @optional
  47. /*! @abstract Creates a new web view.
  48. @param webView The web view invoking the delegate method.
  49. @param configuration The configuration to use when creating the new web
  50. view. This configuration is a copy of webView.configuration.
  51. @param navigationAction The navigation action causing the new web view to
  52. be created.
  53. @param windowFeatures Window features requested by the webpage.
  54. @result A new web view or nil.
  55. @discussion The web view returned must be created with the specified configuration. WebKit will load the request in the returned web view.
  56. If you do not implement this method, the web view will cancel the navigation.
  57. */
  58. - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures;
  59. /*! @abstract Notifies your app that the DOM window object's close() method completed successfully.
  60. @param webView The web view invoking the delegate method.
  61. @discussion Your app should remove the web view from the view hierarchy and update
  62. the UI as needed, such as by closing the containing browser tab or window.
  63. */
  64. - (void)webViewDidClose:(WKWebView *)webView API_AVAILABLE(macos(10.11), ios(9.0));
  65. /*! @abstract Displays a JavaScript alert panel.
  66. @param webView The web view invoking the delegate method.
  67. @param message The message to display.
  68. @param frame Information about the frame whose JavaScript initiated this
  69. call.
  70. @param completionHandler The completion handler to call after the alert
  71. panel has been dismissed.
  72. @discussion For user security, your app should call attention to the fact
  73. that a specific website controls the content in this panel. A simple forumla
  74. for identifying the controlling website is frame.request.URL.host.
  75. The panel should have a single OK button.
  76. If you do not implement this method, the web view will behave as if the user selected the OK button.
  77. */
  78. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler;
  79. /*! @abstract Displays a JavaScript confirm panel.
  80. @param webView The web view invoking the delegate method.
  81. @param message The message to display.
  82. @param frame Information about the frame whose JavaScript initiated this call.
  83. @param completionHandler The completion handler to call after the confirm
  84. panel has been dismissed. Pass YES if the user chose OK, NO if the user
  85. chose Cancel.
  86. @discussion For user security, your app should call attention to the fact
  87. that a specific website controls the content in this panel. A simple forumla
  88. for identifying the controlling website is frame.request.URL.host.
  89. The panel should have two buttons, such as OK and Cancel.
  90. If you do not implement this method, the web view will behave as if the user selected the Cancel button.
  91. */
  92. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler;
  93. /*! @abstract Displays a JavaScript text input panel.
  94. @param webView The web view invoking the delegate method.
  95. @param prompt The prompt to display.
  96. @param defaultText The initial text to display in the text entry field.
  97. @param frame Information about the frame whose JavaScript initiated this call.
  98. @param completionHandler The completion handler to call after the text
  99. input panel has been dismissed. Pass the entered text if the user chose
  100. OK, otherwise nil.
  101. @discussion For user security, your app should call attention to the fact
  102. that a specific website controls the content in this panel. A simple forumla
  103. for identifying the controlling website is frame.request.URL.host.
  104. The panel should have two buttons, such as OK and Cancel, and a field in
  105. which to enter text.
  106. If you do not implement this method, the web view will behave as if the user selected the Cancel button.
  107. */
  108. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * _Nullable result))completionHandler;
  109. #if TARGET_OS_IPHONE
  110. /*! @abstract Allows your app to determine whether or not the given element should show a preview.
  111. @param webView The web view invoking the delegate method.
  112. @param elementInfo The elementInfo for the element the user has started touching.
  113. @discussion To disable previews entirely for the given element, return NO. Returning NO will prevent
  114. webView:previewingViewControllerForElement:defaultActions: and webView:commitPreviewingViewController:
  115. from being invoked.
  116. This method will only be invoked for elements that have default preview in WebKit, which is
  117. limited to links. In the future, it could be invoked for additional elements.
  118. */
  119. - (BOOL)webView:(WKWebView *)webView shouldPreviewElement:(WKPreviewElementInfo *)elementInfo API_DEPRECATED_WITH_REPLACEMENT("webView:contextMenuConfigurationForElement:completionHandler:", ios(10.0, 13.0));
  120. /*! @abstract Allows your app to provide a custom view controller to show when the given element is peeked.
  121. @param webView The web view invoking the delegate method.
  122. @param elementInfo The elementInfo for the element the user is peeking.
  123. @param defaultActions An array of the actions that WebKit would use as previewActionItems for this element by
  124. default. These actions would be used if allowsLinkPreview is YES but these delegate methods have not been
  125. implemented, or if this delegate method returns nil.
  126. @discussion Returning a view controller will result in that view controller being displayed as a peek preview.
  127. To use the defaultActions, your app is responsible for returning whichever of those actions it wants in your
  128. view controller's implementation of -previewActionItems.
  129. Returning nil will result in WebKit's default preview behavior. webView:commitPreviewingViewController: will only be invoked
  130. if a non-nil view controller was returned.
  131. */
  132. - (nullable UIViewController *)webView:(WKWebView *)webView previewingViewControllerForElement:(WKPreviewElementInfo *)elementInfo defaultActions:(NSArray<id <WKPreviewActionItem>> *)previewActions API_DEPRECATED_WITH_REPLACEMENT("webView:contextMenuConfigurationForElement:completionHandler:", ios(10.0, 13.0));
  133. /*! @abstract Allows your app to pop to the view controller it created.
  134. @param webView The web view invoking the delegate method.
  135. @param previewingViewController The view controller that is being popped.
  136. */
  137. - (void)webView:(WKWebView *)webView commitPreviewingViewController:(UIViewController *)previewingViewController API_DEPRECATED_WITH_REPLACEMENT("webView:contextMenuForElement:willCommitWithAnimator:", ios(10.0, 13.0));
  138. #endif // TARGET_OS_IPHONE
  139. #if TARGET_OS_IOS
  140. /**
  141. * @abstract Called when a context menu interaction begins.
  142. *
  143. * @param webView The web view invoking the delegate method.
  144. * @param elementInfo The elementInfo for the element the user is touching.
  145. * @param completionHandler A completion handler to call once a it has been decided whether or not to show a context menu.
  146. * Pass a valid UIContextMenuConfiguration to show a context menu, or pass nil to not show a context menu.
  147. */
  148. - (void)webView:(WKWebView *)webView contextMenuConfigurationForElement:(WKContextMenuElementInfo *)elementInfo completionHandler:(void (^)(UIContextMenuConfiguration * _Nullable configuration))completionHandler API_AVAILABLE(ios(13.0));
  149. /**
  150. * @abstract Called when the context menu will be presented.
  151. *
  152. * @param webView The web view invoking the delegate method.
  153. * @param elementInfo The elementInfo for the element the user is touching.
  154. */
  155. - (void)webView:(WKWebView *)webView contextMenuWillPresentForElement:(WKContextMenuElementInfo *)elementInfo API_AVAILABLE(ios(13.0));
  156. /**
  157. * @abstract Called when the context menu configured by the UIContextMenuConfiguration from
  158. * webView:contextMenuConfigurationForElement:completionHandler: is committed. That is, when
  159. * the user has selected the view provided in the UIContextMenuContentPreviewProvider.
  160. *
  161. * @param webView The web view invoking the delegate method.
  162. * @param elementInfo The elementInfo for the element the user is touching.
  163. * @param animator The animator to use for the commit animation.
  164. */
  165. - (void)webView:(WKWebView *)webView contextMenuForElement:(WKContextMenuElementInfo *)elementInfo willCommitWithAnimator:(id <UIContextMenuInteractionCommitAnimating>)animator API_AVAILABLE(ios(13.0));
  166. /**
  167. * @abstract Called when the context menu ends, either by being dismissed or when a menu action is taken.
  168. *
  169. * @param webView The web view invoking the delegate method.
  170. * @param elementInfo The elementInfo for the element the user is touching.
  171. */
  172. - (void)webView:(WKWebView *)webView contextMenuDidEndForElement:(WKContextMenuElementInfo *)elementInfo API_AVAILABLE(ios(13.0));
  173. #endif // TARGET_OS_IOS
  174. #if !TARGET_OS_IPHONE
  175. /*! @abstract Displays a file upload panel.
  176. @param webView The web view invoking the delegate method.
  177. @param parameters Parameters describing the file upload control.
  178. @param frame Information about the frame whose file upload control initiated this call.
  179. @param completionHandler The completion handler to call after open panel has been dismissed. Pass the selected URLs if the user chose OK, otherwise nil.
  180. If you do not implement this method, the web view will behave as if the user selected the Cancel button.
  181. */
  182. - (void)webView:(WKWebView *)webView runOpenPanelWithParameters:(WKOpenPanelParameters *)parameters initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSArray<NSURL *> * _Nullable URLs))completionHandler API_AVAILABLE(macos(10.12));
  183. #endif
  184. @end
  185. NS_ASSUME_NONNULL_END