2011-01-31 36 views
0

我的應用程序正在創建一個字符串的HTML文件,並使用loadData將其注入到我的UIWebview。從不同的baseUrl在iPhone/iPad的HTML文件中的JQuery UIWebview

我的資產(圖像和js文件)從遠程服務器下載到我的設備中,並且我將它們的baseUrl放入loadData方法(文檔/圖片/資產)中。

在這個目錄我也有2個文件的jquery。

我的問題是uiwebview無法使用jquery,我不知道爲什麼。我的HTML發現其他資產在相同的目錄(圖像),但它似乎無法找到jQuery。

當我嘗試在常規的Safari瀏覽器上使用它,它的工作原理。當將js代碼插入html文件時,它可以工作。但使用本地文件不工作在我的iPad上。

這是我在應用程序代碼:

NSArray *docDir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *imagePath = [NSString stringWithFormat:@"%@/pics", [docDir objectAtIndex:0]]; 
NSString *imagePath = [NSString stringWithFormat:@"%@/pics", [docDir objectAtIndex:0]]; 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@"/" withString:@"//"]; 
imagePath = [imagePath stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 

NSData *htmlDataPortrait = [_stringFromData dataUsingEncoding:NSUnicodeStringEncoding allowLossyConversion:YES]; 

[_portraitWebview loadData:htmlDataPortrait MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString: [NSString stringWithFormat:@"file:/%@//",imagePath]]]; 

,這是我想要在我的HTML文件中使用的代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<head> 

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 

    <style type="text/css"> 

     body { 

      margin-left: 0px; 

      margin-top: 0px; 

      margin-right: 1024px; 

      margin-bottom: 606px; 

      background-repeat: no-repeat; 

      background-color: transparent; 

     } 

    </style> 

    <script type="text/javascript" src="assets/jquery.min.js"></script> 

    <script type="text/javascript" src="assets/func.js"></script> 

    <script> 

     $('body').ready(function(){ 

      reOrderArtWithImgOpt('1'); 

      $('div#fullpage').css('visibility', 'visible'); 

      imgNum = $('img[width!=0]').length; 

      imgIndex = 0; 

      $('img').load(function(){ 

       ++imgIndex; 

       if (imgIndex == imgNum) { 

        setTimeout("setEventDone()", 50); 

       } 

      }); 

     }); 

     function setEventDone(){ 

      window.location.href = 'myapp://loaded'; 

     } 

    </script> 

<script type="text/javascript"> 

    touchMove = function(event){ 

     // Prevent scrolling on this element 

     event.preventDefault(); 

    } 

</script> 

「資產/ ...」目錄位於第e文件/圖片目錄。

代碼不進入:

$('body').ready(function(){ 

感謝。

回答

1

今天我有這個問題。你需要確保JS文件被複制到bundle中。默認情況下,XCode會嘗試編譯JS,而不是將它們包含爲應用程序的資源。

另請參見 jQuery not loading on the iPhone

相關問題