Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 6    Views: 88

AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
04/26/16 04:07 AM (8 years ago)

Is there a way to save a variable to read in javascript ?

I wish to save a string variable from a plugin, then read that string variable in an html file. Currently this is ios, but I am looking to do the same in android too. I have done a lot of googling, this looked the most promising https://shubhankarbhattacharya.wordpress.com/2013/06/07/passstore-in-localstorage-an-arraynsarray-of-objectsnsdictionary-to-javascript-from-objective-c/ but I cannot figure this out. My thoughts were to try an use html5 local storage to save from the plugin. In testing I can read a variable into html (tested by saving a variable from another html file), I seem to be unable to save a similar variable from a plugin. Any kind of local storage is all I need, which can be temporary whilst the app is running. One string variable - sounds so simple in theory...
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
04/26/16 09:45 AM (8 years ago)
Alternatively, if i could call the local html file directly in a webview I could pass parameters to it in the url. But that doesn't seem to work, the code goes straight through the call to my webview as if it wasn't there....
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
04/26/16 02:18 PM (8 years ago)
So your looking for a javascript objective c bridge? http://stackoverflow.com/questions/9473582/ios-javascript-bridge http://www.steamclock.com/blog/2013/05/apple-objective-c-javascript-bridge/ Use setters and getters to get at the variable. If you need persistent storage of the variable use NSUserDefaults.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
04/27/16 10:29 AM (8 years ago)
I appreciate the thought, I'm not sure that I'm looking for a javascript bridge, but I don't know enought to be sure. All I need is to save a variable from one of my plugins to local storage and then read that into an html file that is in the project. At the end of a long day yesterday I had something work (once), based on the above link and also http://stackoverflow.com/questions/3641968/passing-objective-c-variable-to-javascript-in-ios. Now I cannot repeat it, which is very frustrating. With my other fumblings I may have enabled something to allow local storage but I don't know what!
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
04/27/16 10:49 AM (8 years ago)
I should add that my javascript reads in null.
 
CMCOFFEE
Android Fan
Profile
Posts: 2017
Reg: Jan 04, 2013
Amarillo, Texas
26,670
like
04/28/16 06:02 PM (8 years ago)
Why are you using JavaScript? You can achieve better results using swift and objective c.
 
AlanMac
Aspiring developer
Profile
Posts: 2612
Reg: Mar 05, 2012
Esher, UK
37,120
like
04/29/16 01:25 AM (8 years ago)
Good question Chris. The app I am working on is in fact a hybrid. It started here http://www.buzztouch.com/forum/thread.php?tid=46AF5A9B752874E26691E56&currentPage=1 Bottom line, with the limited skills that I have, javascript seems to be the best option for some of the bits, with an eventual Android/ios solution in mind and I do have everything else working. My only issue is creating the local storage variable in objective C and reading it from html. I amy even be creating it successfully but not looking for it in the right place!
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.