如何覆盖WKWebView的loadRequest

时间:2015-08-13 09:11:56

标签: ios swift wkwebview

我想创建一个自定义WKWebView类,因为我想设置一个自定义HTTP标头。但是我得到了这个错误:

  

使用selector' loadRequest重写方法:'具有不兼容的类型'(NSURLRequest) - > WKNavigation'?

import WebKit

class CustomWkWebView: WKWebView {
    override func loadRequest(request: NSURLRequest) -> WKNavigation? {
        var mutable_request = request.mutableCopy() as! NSMutableURLRequest
        mutable_request.setValue("SHOTWORKS_IOS_APPLICATION", forKey: "App-MyParameter")
        return super.loadRequest(mutable_request.copy() as! NSURLRequest)
    }
}

它写在' wkwebview'以这种方式

@availability(iOS, introduced=8.0)
class WKWebView : UIView {

    /*! @abstract A copy of the configuration with which the web view was
     initialized. */
    @NSCopying var configuration: WKWebViewConfiguration { get }

    /*! @abstract The web view's navigation delegate. */
    weak var navigationDelegate: WKNavigationDelegate?

    /*! @abstract The web view's user interface delegate. */
    weak var UIDelegate: WKUIDelegate?

    /*! @abstract The web view's back-forward list. */
    var backForwardList: WKBackForwardList { get }

    /*! @abstract Returns a web view initialized with a specified frame and
     configuration.
     @param frame The frame for the new web view.
     @param configuration The configuration for the new web view.
     @result An initialized web view, or nil if the object could not be
     initialized.
     @discussion This is a designated initializer. You can use
     @link -initWithFrame: @/link to initialize an instance with the default
     configuration. The initializer copies the specified configuration, so
     mutating the configuration after invoking the initializer has no effect
     on the web view.
     */
    init(frame: CGRect, configuration: WKWebViewConfiguration)

    /*! @abstract Navigates to a requested URL.
     @param request The request specifying the URL to which to navigate.
     @result A new navigation for the given request.
     */
    func loadRequest(request: NSURLRequest) -> WKNavigation?

    /*! @abstract Sets the webpage contents and base URL.
     @param string The string to use as the contents of the webpage.
     @param baseURL A URL that is used to resolve relative URLs within the document.
     @result A new navigation.
     */
    func loadHTMLString(string: String, baseURL: NSURL?) -> WKNavigation?

    /*! @abstract Navigates to an item from the back-forward list and sets it
     as the current item.
     @param item The item to which to navigate. Must be one of the items in the
     web view's back-forward list.
     @result A new navigation to the requested item, or nil if it is already
     the current item or is not part of the web view's back-forward list.
     @seealso backForwardList
     */
    func goToBackForwardListItem(item: WKBackForwardListItem) -> WKNavigation?

    /*! @abstract The page title.
     @discussion @link WKWebView @/link is key-value observing (KVO) compliant
     for this property.
     */
    var title: String? { get }

    /*! @abstract The active URL.
     @discussion This is the URL that should be reflected in the user
     interface.
     @link WKWebView @/link is key-value observing (KVO) compliant for this
     property.
     */
    @NSCopying var URL: NSURL? { get }

    /*! @abstract A Boolean value indicating whether the view is currently
     loading content.
     @discussion @link WKWebView @/link is key-value observing (KVO) compliant
     for this property.
     */
    var loading: Bool { get }

    /*! @abstract An estimate of what fraction of the current navigation has been completed.
     @discussion This value ranges from 0.0 to 1.0 based on the total number of
     bytes expected to be received, including the main document and all of its
     potential subresources. After a navigation completes, the value remains at 1.0
     until a new navigation starts, at which point it is reset to 0.0.
     @link WKWebView @/link is key-value observing (KVO) compliant for this
     property.
     */
    var estimatedProgress: Double { get }

    /*! @abstract A Boolean value indicating whether all resources on the page
     have been loaded over securely encrypted connections.
     @discussion @link WKWebView @/link is key-value observing (KVO) compliant
     for this property.
     */
    var hasOnlySecureContent: Bool { get }

    /*! @abstract A Boolean value indicating whether there is a back item in
     the back-forward list that can be navigated to.
     @discussion @link WKWebView @/link is key-value observing (KVO) compliant
     for this property.
     @seealso backForwardList.
     */
    var canGoBack: Bool { get }

    /*! @abstract A Boolean value indicating whether there is a forward item in
     the back-forward list that can be navigated to.
     @discussion @link WKWebView @/link is key-value observing (KVO) compliant
     for this property.
     @seealso backForwardList.
     */
    var canGoForward: Bool { get }

    /*! @abstract Navigates to the back item in the back-forward list.
     @result A new navigation to the requested item, or nil if there is no back
     item in the back-forward list.
     */
    func goBack() -> WKNavigation?

    /*! @abstract Navigates to the forward item in the back-forward list.
     @result A new navigation to the requested item, or nil if there is no
     forward item in the back-forward list.
     */
    func goForward() -> WKNavigation?

    /*! @abstract Reloads the current page.
     @result A new navigation representing the reload.
     */
    func reload() -> WKNavigation?

    /*! @abstract Reloads the current page, performing end-to-end revalidation
     using cache-validating conditionals if possible.
     @result A new navigation representing the reload.
     */
    func reloadFromOrigin() -> WKNavigation?

    /*! @abstract Stops loading all resources on the current page.
     */
    func stopLoading()

    /* @abstract Evaluates the given JavaScript string.
     @param javaScriptString The JavaScript string to evaluate.
     @param completionHandler A block to invoke when script evaluation completes or fails.
     @discussion The completionHandler is passed the result of the script evaluation or an error.
    */
    func evaluateJavaScript(javaScriptString: String, completionHandler: ((AnyObject!, NSError!) -> Void)?)

    /*! @abstract A Boolean value indicating whether horizontal swipe gestures
     will trigger back-forward list navigations.
     @discussion The default value is NO.
     */
    var allowsBackForwardNavigationGestures: Bool

    /*! @abstract The scroll view associated with the web view.
     */
    var scrollView: UIScrollView { get }
}

如何编写方法与我的代码相同。

我只是通过建议写的。

我不知道为什么我无法覆盖。

我该怎么办?

捕获后是这个错误。

enter image description here

0 个答案:

没有答案