Note that there are some explanatory texts on larger screens.

plurals
  1. PORead JSON file from documents folder iOS with javascript
    primarykey
    data
    text
    <p>In my app I need to read a JSON file stored in Documents folder of my iPhone. I saw that exist a method to bridge javascript and objective C. How I can do that? I understand that i should use this method in objective C <code>- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;</code>, but what I should write in javascript? I found this: <a href="http://blog.techno-barje.fr/post/2010/10/06/UIWebView-secrets-part3-How-to-properly-call-ObjectiveC-from-Javascript/" rel="nofollow">call objective C method from javascript</a>, but I didn't understand how it works. In my app I've to do this:</p> <ol> <li>Reach an offer from a mobile site (I made some little mobile site)</li> <li>I store the mobile site local in the Documents folder of my iPhone</li> <li>I generate a JSON file to keep the name, the path and the expire date for the offer. I stored this JSON in Documents folder of my iPhone</li> <li>My app has an "archive" view controller, in which I load a mobile site in which I will to list all offer stored in the device by reading the JSON created</li> </ol> <p>I made the native app and it works, now I've to implement the method to read the JSON file from Documents folder can anyone help me to do this operation? How I can read the JSON from javascript, parse it and show the result in an html dynamic list?</p> <p>Thank you</p> <p>CODE UPDATE: for now I wrote this code:</p> <p><strong>Objective C snippet</strong></p> <pre><code>- (void)parseJson { // Carico il documento JSON dalla cartella documents NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"data.json"]; BOOL fileExist = [[NSFileManager defaultManager] fileExistsAtPath:filePath]; if (fileExist) { NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL]; NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *dictContent = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; if (dictContent) { NSDictionary *sites = [dictContent objectForKey:@"sites"]; NSArray *site = [sites objectForKey:@"site"]; NSMutableArray *names = [[NSMutableArray alloc]init]; NSMutableArray *srcs = [[NSMutableArray alloc]init]; for (int i = 0; i &lt; [site count]; i++) { NSDictionary *dictData = [site objectAtIndex:i]; [names addObject:[dictData objectForKey:@"name"]]; [srcs addObject:[dictData objectForKey:@"src"]]; }; NSString *javascriptString = [NSString stringWithFormat:@"dataFromObjC([%@, %@])", names, srcs]; [self.webView stringByEvaluatingJavaScriptFromString:javascriptString]; } } else { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Promo" message:@"Non hai ancora salvato nessuna promozione!" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alert show]; } // Faccio il parse del documento JSON // Passo le informazioni parsate al javascript } </code></pre> <p>In javascript I made a function named <code>dataFromObjC(names, srcs)</code>. Below you will find the html code to see what I made:</p> <p><strong>Html code</strong></p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html lang="it"&gt; &lt;head&gt; &lt;meta charset="utf-8" /&gt; &lt;meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"/&gt; &lt;title&gt;Lista coupon&lt;/title&gt; &lt;script src="../js/jquery-1.9.1.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../js/memoria.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function dataFromObjC(names, srcs) { alert(names); alert(srcs); } &lt;/script&gt; &lt;style type="text/css"&gt; body { background-color: #000000; width: 100%; height: 100%; padding: 0; margin: 0; } ul { list-style-type: none; padding: 5px; } li { color: #FFFFFF; font-family: sans-serif; padding-bottom: 5px; } p { color: #FFFFFF; font-family: sans-serif; padding: 5px; text-align: center; } a { text-decoration: none; color: #FFFFFF; } &lt;/style&gt; &lt;/head&gt; &lt;body onload="loadJson();"&gt; &lt;div id="header"&gt; &lt;/div&gt; &lt;div id="content"&gt; &lt;p&gt;Di seguito trovi tutte le promozioni salvate&lt;/p&gt; &lt;div id="list"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="footer"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I hope this will be useful to solve it. I used the <code>stringByEvaluatingJavaScriptFromString</code>, because I found on here a post to read data from objective C to javascript.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload