3 Replies
      Latest reply: Feb 19, 2017 2:00 PM by eskimo RSS
      javid94 Level 1 Level 1 (0 points)

        I want to refer json2html.js & jquery.json2html.js into my Swift 3.0 project. While I try invoking the json2html.transform using the JavaScriptCore class (in swift), it's returning undefined. Please refer to below code screenshot.

         

        func loadJavascript()
            {
                let jsSource = jsfile1+jsfile2
            
                let data = try!String(contentsOfFile:Bundle.main.path(forResource:"data", ofType: "json")!)
                let transform = try!String(contentsOfFile:Bundle.main.path(forResource:"transform", ofType: "json")!)
                let context = JSContext()
                let fun = context?.evaluateScript(jsSource)
            
                let Func = fun?.objectForKeyedSubscript("json2html.transform")
                let Call = (Func?.call(withArguments: [data,transform]))
            
                /
                webView.loadHTMLString((Call?.toString())!, baseURL: nil)
            
            }
        

         

        • jsfile1 & jsfile2 are the 2 JS file contents (JS and jQuery) for JSON2HTML converted to String
        • I've saved the code for data and transform in 2 separate files(which work well without Swift). Those 2 files are data and transform (I have saved transform data inside JSON-anyhow will be converted to String)

        Normal JS functions work fine while I tried it in the same way. Could anyone please help me out?

         

        Also, is there any pre-built JSON2HTML library/framework built for Swift that could be integrated via CocoaPods into swift.

        Awaiting for reply! Thanks a lot in advance.

        • Re: Using JSON2HTML library in Swift
          eskimo Apple Staff Apple Staff (7,845 points)

          While I try invoking the json2html.transform using the JavaScriptCore class (in swift), it's returning undefined.

          It sounds like you need to debug this at the JavaScript level.  I’ve never done that myself but AFAIK it’s possible with modern versions of the Safari Web Inspector.  A good place to start is WWDC 2014 Session 512 [TTT][Web Inspector and Modern JavaScript].

          Share and Enjoy

          Quinn “The Eskimo!”
          Apple Developer Relations, Developer Technical Support, Core OS/Hardware
          let myEmail = "eskimo" + "1" + "@apple.com"

            • Re: Using JSON2HTML library in Swift
              javid94 Level 1 Level 1 (0 points)

              Thanks for your reply Eskimo. Unfortunately it's not paying back.

               

              I got to refer 2 js files (json2html.js & jquery.json2html.js) and invoke a JS function from them passing JSON data as parameters.

              I'm using evaluateScript & objectForKeyedSubscript and all I get is undefined (in all the ways I tried). The same parameters & JS functions work fine while I tried outside without using Swift & XCode. I don't know what's the issue!

                • Re: Using JSON2HTML library in Swift
                  eskimo Apple Staff Apple Staff (7,845 points)

                  My suggestion was that you use the web inspector to step through your JavaScript code to see where it’s going wrong.  Did you try that?  What did you see?

                  Share and Enjoy

                  Quinn “The Eskimo!”
                  Apple Developer Relations, Developer Technical Support, Core OS/Hardware
                  let myEmail = "eskimo" + "1" + "@apple.com"