Thursday, 15 September 2011

How to use Javascript from document directory in UIWebView in iOS? -



How to use Javascript from document directory in UIWebView in iOS? -

in application , have alter font webpage loaded online.

first store customjava.js files in online hosting , utilize file alter font in ios app.

here of codes.

- (void)webviewdidfinishload:(uiwebview *)webview { nsstring *js = [nsstring stringwithformat:@"var script = document.createelement('script');" "script.type = 'text/javascript';" "script.src = 'http://www.gustohei.com/syjs/customjava.js';"]; js = [nsstring stringwithformat:@"%@ document.getelementsbytagname('head')[0].appendchild(script);", js]; [self.webviewofbrowser stringbyevaluatingjavascriptfromstring:js]; uiapplication *app = [uiapplication sharedapplication]; app.networkactivityindicatorvisible = no; }

it's fine , alter font correctly.

in case , want maintain js file in document directory , want utilize file document directory . don't want utilize online.

so wrote next code.

- (void)webviewdidfinishload:(uiwebview *)webview { nsstring *jspath = [[[nsbundle mainbundle] resourcepath] stringbyappendingpathcomponent:@"customjava.js"]; nsstring *js = [nsstring stringwithformat:@"var script = document.createelement('script');" "script.type = 'text/javascript';" "script.src ='%@'",jspath]; js = [nsstring stringwithformat:@"%@ document.getelementsbytagname('head')[0].appendchild(script);", js]; [self.webviewofbrowser stringbyevaluatingjavascriptfromstring:js]; uiapplication *app = [uiapplication sharedapplication]; app.networkactivityindicatorvisible = no; }

however it's doesn't work. how that?

in xcode’s project navigator (left column) click on bluish project icon. select “build phases” tab in main window open “compile sources” section find javascript file in list , remove it. open “copy bundle resources” section add javascript file list

then load js file using next code

[[nsbundle mainbundle] pathforresource:@"script.js" oftype:nil];

update: xcode 4.5 creates warning when add together javascript file project without removing “compile sources” section:

warning: no rule process file 'filepath' of type sourcecode.javascript architecture armv7

javascript ios cocoa-touch uiwebview

No comments:

Post a Comment