Can i make a html file in Documents directory which accesses jquery and cordova in the app bundle?

孤者浪人 提交于 2019-12-25 17:07:29

问题


I am building a html file which is stored in

[[PROJ UserDirectory] stringByAppendingPathComponent:@"tmp_Form_File.html"]

and inside of the file "tmp_FOrm_File.html" it is being dynamically built. Body shows up correctly but the JS will not execute.

I was trying to do:

NSString * cordova_path = [[NSBundle mainBundle] oathForResource:@"cordova-2.2.0" ofType:@"js" inDirectory:@"html"]
NSString = [[@"<script type=\"text/javascript\" src=\"" stringByAppendingString:cordova_path] stringByAppendingString:@"\"></script>"];

and similarly for jquery.

My issue is that i dont think that when I load the htmlfile into a webView that it gets the data accordingly.

I tested the strings, and they look sound:

<script type="text/javascript" src="/var/mobile/Applications/[DATA]/PROJ.app/html/cordova-2.2.0.js"></script><script type="text/javascript" src="/var/mobile/Applications/[DATA]/PROJ.app/html/jquery-1.7.2.min.js"></script><script type="text/javascript">$(function(){ alert("test"); });</script>

1) Am i doing it wrong? if So, how do i resolve this?

2) Would it be better to through ios copy the html directory in the bundle to Documents? If so, how?

Edit: Temporarily removed the jquery references, jquery and cordova. just used pure javascript, and it will alert correctly. So that means there is something wrong with the src being in the Bundle.


回答1:


This is happening because your references are not working. When you want your HTML to be rendered in webview with some relative paths inside it, you have add it like using the option "Create folder references for added folder".

If this html pages are not going to fetch and store any data, then keep it in bundle, otherwise copy them to Library or document's folder in Appdelegate.

Hope this helps!




回答2:


I couldnt reference it atm, but i resolved it this way: - I copied the contents of [BUNDLE]/html to Documents....

    NSString * sourcePath = [[[NSBundle mainBundle] pathForResource:@"cordova-2.2.0" ofType:@"js" inDirectory:@"html"] stringByDeletingLastPathComponent];
    NSString * documentDirectory = [AssetInspectorFileManager UsersDirectory];
  • adjusted the src of the javascript.

    < script type=\"text/javascript\" src=\"cordova-2.2.0.js\">

    < script type=\"text/javascript\" src=\"jquery-1.7.2.min.js\">

  • Closed a missing parenthese

    " $(\"div.container\").css(\"z-index\",1;"

became

 "   $(\"div.container\").css(\"z-index\",1);"
  • Coomenting is not good to do for this, as when you write data out such as:

    $(this).click(function(){//[THIS IS A COMMENT] alert("hi");});

So you have to adjust for that sort of issue.



来源:https://stackoverflow.com/questions/13824436/can-i-make-a-html-file-in-documents-directory-which-accesses-jquery-and-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!