Communication between iOS's native app and webpage's javascript

后端 未结 6 1902
深忆病人
深忆病人 2021-02-01 11:10

I have a webpage loaded in a UIWebView, and a javascript function of the page needs to data from native iOs app, a NSString. How can a Js function access the data in native app

6条回答
  •  别跟我提以往
    2021-02-01 11:39

    [webView loadHTMLString:@"" 
          baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];
    
    NSString *result = [webView stringByEvaluatingJavaScriptFromString:@"function(parameter)"];
    

    Give feedback to iOS

    window.location = customprefix://function/parameter=value
    
    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        if ([[URL scheme] isEqualToString:@"customprefix"]) {
            // handle function name and paramters
        }
    }
    

    I also wrote a guide on how to call and handle different javascript functions from within iOS. http://www.dplusmpage.com/2012/07/20/execute-javascript-on-ios/

提交回复
热议问题