2015-03-13 85 views
0

我的應用程序中有一個.html文件。我想通過編碼來讀取文件路徑。我在支持文件下創建一個文件夾。在那個文件中我有一個.html文件。我需要該文件來解析。我的問題是如何獲得該文件路徑?如何從應用程序中的文件夾獲取文件路徑[iOS]

我需要index_SCORM.html來自Project_Management_Module文件夾的文件。

請幫幫我。

enter image description here

我從HTML文件此響應。但它不是在網頁視圖

 htmlFile == <!DOCTYPE html> 

    <html lang="en"> 
     <head> 
     <meta name='viewport' content='initial-scale = 1, minimum-scale = 1, maximum-scale = 1'/> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     <meta http-equiv="x-ua-compatible" content="IE=10"> 
     <title></title> 
     <style type="text/css">#initialLoading{background:url(assets/htmlimages/loader.gif) no-repeat center center;background-color:#ffffff;position:absolute;margin:auto;top:0;left:0;right:0;bottom:0;z-index:10010;}</style> 

     <script> 
     var deviceReady = false; 
     var initCalled = false ; 

     function onBodyLoad() 
     { 
      if(typeof window.device === 'undefined') 
      { 
      document.addEventListener("deviceready", onDeviceReady, false); 
      } 
      else 
      { 
       onDeviceReady(); 
      } 
     } 

     function onDeviceReady() 
     { 
      deviceReady = true ; 
      if(initCalled === true) 
      initializeCP(); 
     } 

     function initializeCP() 
     { 
      initCalled = true ; 
      if(cp.pg && deviceReady === false) 
      return; 

      function cpInit() 
      { 
              document.body.innerHTML = " <div class='cpMainContainer' id='cpDocument' style='left: 0px; top:0px;' > <div id='main_container' style='top:0px;position:absolute;width:100%;height:100%;'> <div id='projectBorder' style='top:0px;left:0px;width:100%;height:100%;position:absolute;display:block'></div> <div class='shadow' id='project_container' style='left: 0px; top:0px;width:100%;height:100%;position:absolute;overflow:hidden;' > <div id='project' class='cp-movie' style='width:100% ;height:100%;overflow:hidden;'>  <div id='project_main' class='cp-timeline cp-main'>   <div id='div_Slide' onclick='cp.handleClick(event)' style='top:0px; width:100% ;height:100% ;position:absolute;-webkit-tap-highlight-color: rgba(0,0,0,0);'></div>  </div>  <div id='autoplayDiv' style='display:block;text-align:center;position:absolute;left:0px;top:0px;'>   <img id='autoplayImage' src='' style='position:absolute;display:block;vertical-align:middle;'/>             <div id='playImage' tabindex='9999' role='button' aria-label='play' onkeydown='cp.CPPlayButtonHandle(event)' onClick='cp.movie.play()' style='position:absolute;display:block;vertical-align:middle;'></div>  </div> </div> <div id='toc' style='left:0px;position:absolute;-webkit-tap-highlight-color: rgba(0,0,0,0);'> </div> <div id='playbar' style='bottom:0px; position:fixed'> </div> <div id='cc' style='left:0px; position:fixed;visibility:hidden;pointer-events:none;' onclick='cp.handleCCClick(event)'>  <div id='ccText' style='left:0px;float:left;position:absolute;width:100%;height:100%;'>  <p style='margin-left:8px;margin-right:8px;margin-top:2px;'>  </p>  </div>  <div id='ccClose' style='background-image:url(./assets/htmlimages/ccClose.png);right:10px; position:absolute;cursor:pointer;width:13px;height:11px;' onclick='cp.showHideCC()'>  </div> </div> <div id='gestureIcon' class='gestureIcon'> </div> <div id='gestureHint' class='gestureHintDiv'>  <div id='gImage' class='gesturesHint'></div> </div> <div id='pwdv' style='display:block;text-align:center;position:absolute;width:100%;height:100%;left:0px;top:0px'></div> <div id='exdv' style='display:block;text-align:center;position:absolute;width:100%;height:100%;left:0px;top:0px'></div> </div> </div></div><div id='blockUserInteraction' class='blocker' style='width:100%;height:100%;'> <table style='width:100%;height:100%;text-align:center;vertical-align:middle' id='loading' class='loadingBackground'>  <tr style='width:100%;height:100%;text-align:center;vertical-align:middle'>   <td style='width:100%;height:100%;text-align:center;vertical-align:middle'>    <image id='preloaderImage'></image>    <div id='loadingString' class='loadingString'>Loading...</div>   </td>  </tr> </table></div> <div id='initialLoading'></div>"; 

      cp.DoCPInit(); 
      var lCpExit = window["DoCPExit"]; 
      window["DoCPExit"] = function() 
      { 
      if(cp.UnloadActivties) 
       cp.UnloadActivties(); 
      lCpExit(); 
     }; 
    } 

     cpInit(); 
     } 
    </script> 
    </head> 
    <body onload="onBodyLoad()"> 
     <div id='initialLoading'></div> 

     <script> 
      (function() 
      { 
      if(document.documentMode < 9) 
      { 
       document.body.innerHTML = ""; 

       document.write("The content you are trying to view is not supported in the current Document Mode of Internet Explorer. Change the Document Mode to Internet Explorer 9 Standards and try to view the content again.<br>To change the Document Mode, press F12, click Document Mode: <current mode>, and then select Internet Explorer 9 Standards."); 

       return; 
      } 

      window.addEventListener("load",function() 
      { 
       setTimeout(function() 
       {     
        var script = document.createElement('script'); 
        script.type = 'text/javascript'; 
        script.src = 'assets/js/CPXHRLoader.js'; 
        script.defer = 'defer'; 
        script.onload = function() 
        { 
         var lCSSLoaded = false; 
         var lJSLoaded = false; 

         function constructDIVs() 
         { 
          if(lCSSLoaded && lJSLoaded) 
          { 
           initializeCP(); 
          } 
         } 

    cpXHRJSLoader.css('assets/css/CPLibraryAll.css',function() { 
          lCSSLoaded = true; 
          constructDIVs(); 
         }); 

         var lJSFiles = [ 'assets/js/jquery-1.6.1.min.js','scormdriver.js','assets/js/CPM.js' ]; 

         cpXHRJSLoader.js(lJSFiles,function() 
         { 
          //console.log("js loaded"); 
          lJSLoaded = true; 
          constructDIVs(); 
         }); 
        } 

        document.getElementsByTagName('head')[0].appendChild(script); 

       },1); 

      },false); 

     })(); 

     </script> 

    <noscript style="text-align:center;font-size:24px;">Enable Javascript support in the browser.</noscript> 

    </body> 

    </html> 
+0

像這樣:'的NSString *路徑= [[一個NSBundle mainBundle] pathForResource:@ 「index_SCORM」 ofType:@ 「HTML」];'。 – gabbler 2015-03-13 09:11:51

+0

是的,我已經這樣做,但我沒有獲取Html文件中的數據 – 2015-03-13 09:12:29

回答

0

加載這應該工作:

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"index_SCORM" ofType:@"html"]; 
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil]; 
[yourWebView loadHTMLString:htmlString baseURL:nil]; 

還要檢查該資源被調用pathForResource:ofType:inDirectory發現。

+0

它不工作:( – 2015-03-13 09:19:19

+0

如果路徑不爲零,這應該工作。 – gabbler 2015-03-13 09:20:50

0

這裏是讓你的文件路徑的方式:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"index_SCORM" ofType:@"html"]; 
+0

沒有人,它不工作:( – 2015-03-13 09:19:08

+0

什麼是filePath的內容NSString? – Niko 2015-03-13 09:24:58

0
NSString *path = [[NSBundle mainBundle] pathForResource:@"index_SCORM" ofType:@"html"]; 

確保當您將文件添加到您的項目拷貝文件,如果需要被選中。

enter image description here

+0

沒有人,它不工作:( – 2015-03-13 09:19:04

+0

NSString * path = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@「index_SCORM」ofType:@「html」] encoding:NSUTF8StringEncoding error:nil]; NSLog(@ 「htmlFile ==%@」,path); [self.webView loadHTMLString:path baseURL:nil]; 我這樣寫,數據來自NSLog,但沒有在WebView中加載 – 2015-03-13 09:19:56

+0

所以問題出在webView中關於如何從文件中檢索數據 – 2015-03-13 09:20:49

相關問題