New iOS Dev
New iOS Dev

Reputation: 2057

Can we use HTML code in SWIFT?

I have some attractive graph which is made in HTML and javascript.

So my questions are:

  1. Can I use this work in my swift app,
  2. Can we use javascript and html code to make graph in app?using webview
  3. Can we pass values from swift/objective c code to javascript/html code and vice-versa?

If yes , How can we pass variable value to html/javascript and how to to accept this value in HTML

Upvotes: 2

Views: 1651

Answers (2)

Duyen-Hoa
Duyen-Hoa

Reputation: 15804

  1. YES. Create a webview then load this html page.
  2. YES. (same as answer 1)
  3. YES .

    • To send to html page: call [webView stringByEvaluatingJavaScriptFromString:function];
    • To send to swift/objective-c : handle shouldStartLoadWithRequest of your UIwebview.

    -(BOOL) webView : (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType : (UIWebViewNavigationType)navigationType {

    if ([[[request URL] absoluteString] hasPrefix:@"yourPrefixe:"]) {
        //do your works
        //then cancel current request
        return NO;
    }
    return YES;
    

    }

Upvotes: 0

Daij-Djan
Daij-Djan

Reputation: 50129

  • html text can be embedded into labels as NSAttributedString instances
  • HTML content that has scripts and stuff can be put into a web view container: UIWebView or WKWebView

Upvotes: 2

Related Questions