1

How to integrate JavaScript in objective c?
Here, is my JavaScript code:

<!DOCTYPE HTML>
<head>
    <meta  name="viewport"  content="initial-scale=1.0,  user-scalable=no"  />
    <script  type="text/javascript"  src="https://maps.micello.com/webmap/v0/micellomap.js"></script>
    <script  type="text/javascript">
        micello.maps.init("ZccEicxOED0xicnQFrnkK7uzXJJUog",mapInit);
        function  mapInit(strMapID)  {
            alert("Hello World!"+strMapID);
            var  mapControl = new micello.maps.MapControl('mapElement');
            var  mapDataObject = mapControl.getMapData();
            mapDataObject.loadCommunity(strMapID);
        }
    </script>
    <style  type="text/css">
        html, body { height: 100%; width: 100%; margin: 0; overflow: hidden; }
        #mapElement{ width:100%;  height:100%; }
        </style>
</head>
<body>
    <div  id="mapElement"></div>
</body>
</html>

I am using this JavaScript code in my Objective-C in WebView. Here, is my objective-c code:

- (void)viewDidLoad { 
    NSLog(@"strMapID = %@",strMapID);
    NSString *path;
    NSBundle *thisBundle = [NSBundle mainBundle];
    path = [thisBundle pathForResource:@"SomeHTML" ofType:@"html"];  
    NSURL *instructionsURL = [NSURL fileURLWithPath:path]; 
    [webView loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
}  

WebView delegate methods:

-(void)webViewDidStartLoad:(UIWebView *)webView {
    NSLog(@"start");
}
-(void)webViewDidFinishLoad:(UIWebView *)webView1 {
    NSLog(@"finish");

    [webView1 stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"mapInit(%@)",strMapID]];

    //[webView stringByEvaluatingJavaScriptFromString:@"mapInit()"];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    NSLog(@"Error for WEBVIEW: %@", [error description]);
}

But here, the JavaScript alert("Hello World!"+strMapID); is calling two times. first time it is showing correct MapID but second time it's showing undefined.
I don't know why alert is calling 2 times. Because of this it's giving me 500 error in my webview.

Error in My iPhone

Please guide me to solve my Problem.

Thanks in Advance.

  • Does this help : http://stackoverflow.com/questions/38198083/how-to-use-angularjs-javascript-and-objective-c-together ? This is not my expert area. – 0aslam0 Jul 26 '16 at 14:02
  • Thanks @0aslam0 but this link is not helpful for me. –  Jul 27 '16 at 04:50
  • I want to know that how to convert this html code into a javascript function. In this I want to pass parameter in mapInit(n) method through objective c. –  Jul 27 '16 at 05:30

1 Answers1

0

First we need to set UIWebView then UIWebView in Delegate Methods I call stringByEvaluatingJavaScriptFromString

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  CGFloat height = [[webview stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
  CGRect frame = webview.frame;
  frame.size.height = height;
  webview.frame = frame;
  //If you want to set font size and body of HTML
  int fontSize = 80;
  NSString *jsString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '%d%%'", fontSize];
  [webview stringByEvaluatingJavaScriptFromString:jsString];
  NSString *jsWebViewFontFamilyString = [[NSString alloc] initWithFormat:@"document.getElementsByTagName('body')[0].style.fontFamily = 'Helvetica'"];
  [webview stringByEvaluatingJavaScriptFromString:jsWebViewFontFamilyString];

  //For passing parameter from Objective c to JavaScript
  NSString *strMapId  = @"1367"; //Your ID
  NSString *jsCallBack = [NSString stringWithFormat:@"mapInit('%@')",strMapId];
  [webView stringByEvaluatingJavaScriptFromString:jsCallBack];
}

Passing Objective C variable to JavaScript

Then If you want to navigate or do anything

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
  switch (navigationType)
  {
    case UIWebViewNavigationTypeLinkClicked:
        //When User tapped a link. 
        break;
    case UIWebViewNavigationTypeOther: 
       //Some other action occurred. 
       break;
    case UIWebViewNavigationTypeFormSubmitted:
        //user submitted a form. 
       break;
    case UIWebViewNavigationTypeBackForward: 
       //User tapped the back or forward button. . 
       break;
    case UIWebViewNavigationTypeReload:
       //User tapped the reload button. 
      break;
     case UIWebViewNavigationTypeFormResubmitted
       //User resubmitted a form. . 
      break;
   }
 return YES; 
}
Community
  • 1
  • 1
user3182143
  • 9,459
  • 3
  • 32
  • 39
  • I am sorry @user3182143 but I want to implement My JavaScript code in one js file and I want to pass parameter `mapInit(n)` method in objective c. Are you understood my Question? –  Jul 27 '16 at 05:03
  • http://stackoverflow.com/questions/9826792/how-to-invoke-objective-c-method-from-javascript-and-send-back-data-to-javascrip – user3182143 Jul 27 '16 at 05:17
  • not understood. can u tell me how to use this link answer in my scenario? –  Jul 27 '16 at 05:36
  • N means what?why do you pass N? – user3182143 Jul 27 '16 at 05:54
  • See also http://stackoverflow.com/questions/28105156/how-to-pass-a-variable-from-javascript-to-objective-c-and-objective-c-to-javasct – user3182143 Jul 27 '16 at 05:59
  • Actually it's basically a indoor map script in which n means Map Id. I want to pass MapID dynamically. –  Jul 27 '16 at 06:04
  • 1
    My answer and your webview didFininshLoad answer is correct.I think you could have made mistaken in your java script code – user3182143 Jul 27 '16 at 07:55
  • Please see in my question. I added screenshot in my question. –  Jul 27 '16 at 09:27