0

I am facing a problem with UIWebView. I am trying to load an HTML string which will load other downloaded files(js files, css, html and xml files) to load some contents in custom player inside UIWebview.Same thing is working in android so I don't think there is issue in files. But in iOS it showing a white page. I have tried all suggestions in other similar questions in stack overflow.

I have already checked if the files are properly being downloaded or not. Everything seems fine. I can see the files in directories and open it. I have also put NSLogs inside webViewDidFinishLoad method to check and its also getting executed.I can neither find any solutions on this nor identify the problem.

self.webContentView.delegate = self;
NSString* html = [player getHTMLString];
NSString* baseURL = [player getBaseURLPath];
[self.webContentView loadHTMLString:html baseURL: [NSURL URLWithString: baseURL]];

html string is build from a template stored in resources. baseURL is not nil or incorrect. html string -

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content='width=device-width'>
    <title>{{lessonTitle}}</title>
    <script type="text/javascript" language="javascript" src="file://{{icplayerPath}}/javascript/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" language="javascript" src="file://{{icplayerPath}}/icplayer/icplayer.nocache.js"></script>
    <script type="text/x-mathjax-config">
     MathJax.Hub.Config({
            messageStyle: "none",
            TeX: {imageFont: null, extensions: ["mhchem.js", "AMSsymbols.js", "AMSmath.js"]},
            extensions: ["tex2jax.js", "forminput.js"],
            skipStartupTypeset: true,
            jax: ["input/TeX","output/HTML-CSS"],
            playerObject: "player"
    });
    </script>
    <script type="text/javascript" src="file://{{icplayerPath}}/javascript/MathJax/MathJax.js"></script>

    <script language="javascript">
    var player1;
    var scorm;

    function icOnAppLoaded(){
        // Load assessment
        {{#createBook}}
        player1 = icCreateBook('_icplayer', {{withCover}});
        {{/createBook}}
        {{^createBook}}
        player1 = icCreatePlayer('_icplayer');
        {{/createBook}}
        {{#lessonPages}}
        if (player1.hasOwnProperty('setPages'))
        {
            player1.setPages('{{lessonPages}}');
        }
        {{/lessonPages}}
        {{#lessonStateString}}
        stateString = "{{{lessonStateString}}}";
        player1.setState(stateString);
        {{/lessonStateString}}
        player1.load('file://{{lessonMainXmlPath}}');
        player1.onPageLoaded(function(){
            if (window.parent != null && window.parent.postMessage != null) {
                var height = $('#_icplayer').css('height');
                var width = $('#_icplayer').css('width');
                var message = "resize: width=" + width + ";height=" + height;
                var viewport = document.querySelector("meta[name=viewport]");
                viewport.setAttribute('content', 'width=' + parseInt(width));
                $('#lesson').width(width);
                $('#lesson').css('margin', '0 auto');
                window.parent.postMessage(message, '*');
        var pageNo = player1.getPlayerServices().getCurrentPageIndex();
        var theUrl = "mLibro://pageLoaded/"+pageNo;
        window.location = theUrl;
        alert(theUrl);
        }
        });
    }

    function mLibroIOS_getScore(){
        if (player1.hasOwnProperty('forceScoreUpdate')) {
            player1.forceScoreUpdate()
        }
        var pu = new PlayerUtils(player1);
        var pres = pu.getPresentation();
        var scoreNew = pu.getPresentationScore(pres);
        return JSON.stringify(scoreNew);
    }
    </script>
</head>
<!-- max-width:600px -->
<body style="padding:0px;margin:0px">
    <div id="lesson">
        <div id="_icplayer" style="padding:0px;margin:0px;float:left"></div>
    </div>
    <div style="clear: both"></div>
</body>

</html>
RR_Pradhan
  • 78
  • 12
  • `[self.webContentView loadHTMLString:string baseURL:nil];` have you tried this ? by passing nil in baseURL. – Nirav Kotecha Apr 15 '19 at 07:07
  • Yes, I have tried that also but the result is same. Does this issue have anything to do with UIWebView depreciation. – RR_Pradhan Apr 15 '19 at 07:10
  • can you give me example that what's your html string coming and base url ? so i can test in my webview. if its possible then. – Nirav Kotecha Apr 15 '19 at 07:12
  • html string is getting built from a mustache template to load js files from downloaded path. baseurl is the path of those files.(e.g - file:///Users/ealpha4tech/Library/Developer/CoreSimulator/Devices/B23074B1-3092-4334-A0CC-ED62FEA00F01/data/Containers/Data/Application/1B3EC041-DEB7-4BB7-BDE8-249745FE61B6/Documents/ealpha-live-new.appspot) – RR_Pradhan Apr 15 '19 at 07:16
  • add your html string and other methods code – karthikeyan Apr 15 '19 at 07:17
  • I have edited the question.Its the template used. – RR_Pradhan Apr 15 '19 at 07:21
  • @RR_Pradhan You wanted to load js, html etc files in your webview .. Thats it???? – dahiya_boy Apr 15 '19 at 07:24
  • check this.. https://stackoverflow.com/questions/6420925/load-resources-from-relative-path-using-local-html-in-uiwebview – Nirav Kotecha Apr 15 '19 at 07:25
  • Yes.The js files are for a custom player. – RR_Pradhan Apr 15 '19 at 07:26
  • 1
    @RR_Pradhan Then your question should be **How to load local js file in webview**. Please dont make ambiguous question. First clean your post with correct words and then Try this -> https://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources – dahiya_boy Apr 15 '19 at 07:26
  • I can not load my files to my project resources.As the files downloaded from server will be different from different users and contents.Its a mobile Learning management system so content will be different. – RR_Pradhan Apr 15 '19 at 07:31
  • @RR_Pradhan See problem in this way .. you downloaded file from server.. now your file is in local directory .. now all you need to get the file location and load it into webview. – dahiya_boy Apr 15 '19 at 07:32
  • Yes. I am able to get the location but when loading its not loading. – RR_Pradhan Apr 15 '19 at 07:36
  • Have you tried using `WKWebView` instead of almost deprecated `UIWebView`? – Cy-4AH Apr 15 '19 at 09:06
  • I tried but could not migrate properly. Its a bit complicated for this code. There arre no proper migration documents.Like what would be the method equivalent of shouldStartLoadWithRequest method – RR_Pradhan Apr 15 '19 at 09:15
  • @dahiya_boy, Do you think there might be some problem with UI component in storyboard ?If webViewDidFinishLoad is executed then it is finishing loading but for some reason it might not be able to render the data? Do i need to change anything in UIWebView or the UIView it is part of ? – RR_Pradhan Apr 15 '19 at 12:21
  • @RR_Pradhan As Cy said `UIWebView` is deprecated. But its still expected to be work. There is no UI issue. Probably you are doing something else wrong.. like getting path and loading it. and sure your js is correct. – dahiya_boy Apr 15 '19 at 12:45
  • @dahiya_boy It should be correct as the same files are used in android also and they works perfectly fine.I can see the requests going to shouldStartLoadWithRequest then webViewDidFinishLoad is called. But the WebView remains white. – RR_Pradhan Apr 15 '19 at 13:46
  • @RR_Pradhan your file is unable to load thats the issue.. if you can share the file then I can try & will update you status.. thats all I can do. – dahiya_boy Apr 15 '19 at 15:46
  • @dahiya_boy .. Hi, Sorry for the late reply... I can not share the files as there are so many of them and you need some token to use the links of those files... I tested many things and found that my html string is loaded fine(I am able to see changes like any message inside body) but the js files are not getting load or execute in UIWebView. – RR_Pradhan Apr 22 '19 at 06:24
  • Issue is still there. I can load any websites, html strings but not the js files. Is there any changes happened from Apple that I need to implement in my code or js files. PS : I came to know before me joining this codes and files were working as expected.There were some issue in server and we moved to new server and after that we faced this issue but only in iOS.Rest is working as expected. – RR_Pradhan Apr 22 '19 at 06:27
  • I know that your JS is not loading properly (thats what I meant earlier). Its JS issue not an iOS. Make sure you are using `WKWebview`. Check your JS in safari either its working or not? – dahiya_boy Apr 22 '19 at 06:35
  • @dahiya_boy, I have tried the answer by Nick weaver in https://stackoverflow.com/questions/5733883/loading-javascript-into-a-uiwebview-from-resources to test js files. I can execute js code inside html but is the code is in a separate js file then it is not loaded(same as my js files). – RR_Pradhan Apr 22 '19 at 11:57
  • It does not matter if the js files are in resources or in other directory UIWebView is not able to load those.Its same for my js files as well as any dummy js files written for test. – RR_Pradhan Apr 22 '19 at 12:00

0 Answers0