iOS Objective-C LocalStore in WKWebView

雨燕双飞 提交于 2019-12-12 04:54:26

问题


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:


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();
                }
         }];
    }
}];


来源:https://stackoverflow.com/questions/46082813/ios-objective-c-localstore-in-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!