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

后端 未结 6 1780
深忆病人
深忆病人 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:17

    You can execute JavaScript in your UIWebView from Obj-C. Simply call [webView stringByEvaluatingJavaScriptFromString:@"myJavaScript"];.

    I could imagine a setup like this:

    Webpage

    
       
          
       
       
       
    
    

    Objective-C

    NSString *myParameter = @"myParameter";
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"callmeFromObjC('%@')", myParameter]];
    

提交回复
热议问题