iOS Objective-C LocalStore in WKWebView

1.1k views Asked by At

I found this SO that gives an explanation of how to set local storage using javascript.

I'm trying to translate it from Swift to Objective-C. For some reason it's throwing a fit at me like I'm missing a bracket but I can't seem to figure it out. Any ideas how to correctly translate that Swift to iOS?

Here's what I have so far:

- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation {

    [webView evaluateJavaScript:@"localStorage.getItem(\"key\")" completionHandler:^(id result, NSError *error) {
        if (error == nil) {
            [webView evaluateJavaScript:@"localStorage.setItem(\"key\", \"value\")" completionHandler:^(id result, NSError *error) {
                    if (error == nil) {
                        webView.reload();
                    }
            }
             }];
        }
    }];
}
1

There are 1 answers

0
Anonymous On BEST ANSWER

Indeed, you have an extra bracket in your code.

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    [webView evaluateJavaScript:@"localStorage.getItem(\"key\")" completionHandler:^(id result, NSError *error) {
    if (error == nil) {
        [webView evaluateJavaScript:@"localStorage.setItem(\"key\", \"value\")" completionHandler:^(id result, NSError *error) {
                if (error == nil) {
                    webView.reload();
                }
         }];
    }
}];