IOS add Wkwebview causes the controller to not release the issue

Source: Internet
Author: User

In Wkwebview and JavaScript interactions, the method of injecting MessageHandler into the messagehandler,app is often injected into the native

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];    config.userContentController = [WKUserContentController new];    //注入handler    [config.userContentController addScriptMessageHandler:self name:@"HandlerName"];

Here we find a strong reference to self when injecting handler into JS, resulting in a memory leak

Workaround add a new class Weakscriptmessagedelegate

@interface WeakScriptMessageDelegate : NSObject<WKScriptMessageHandler>@property (nonatomic, weak) id<WKScriptMessageHandler> scriptDelegate;- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate;@end@implementation WeakScriptMessageDelegate- (instancetype)initWithDelegate:(id<WKScriptMessageHandler>)scriptDelegate {    self = [super init];    if (self) {        _scriptDelegate = scriptDelegate;    }    return self;}- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {    [self.scriptDelegate userContentController:userContentController didReceiveScriptMessage:message];}@end

With the following line of code added, Viewcontroller calls the Dealloc method, at which point Viewcontroller is released normally. But Weakscriptmessagedelegate is not released and needs to be released in Dealloc Weakscriptmessagedelegate.

    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];    config.userContentController = [WKUserContentController new];    //注入handler    [config.userContentController addScriptMessageHandler:[[WeakScriptMessageDelegate alloc] initWithDelegate:self] name:@"HandlerName"];

Release Weakscriptmessagedelegate implemented in the Dealloc method

    [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"HandlerName"];

IOS add Wkwebview causes the controller to not release the issue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.