Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do you have control over that <code>token.html</code> source? It would be best if it could return JSON or XML, rather than HTML. It greatly simplifies the parsing process. </p> <p>But you could easily load the page into a <code>NSData</code> via:</p> <pre><code>NSError *error = nil; NSURL *url = [NSURL URLWithString:@"http://nonlinearmind.no-ip.org:8888/token.html"]; NSData *htmlData = [NSData dataWithContentsOfURL:url options:0 error:&amp;error]; if (error != nil) { NSLog(@"%s unable to load URL '%@'; error = %@", __FUNCTION__, url.absoluteString, error); } </code></pre> <p>Or into a <code>NSString</code> via:</p> <pre><code>NSError *error = nil; NSURL *url = [NSURL URLWithString:@"http://nonlinearmind.no-ip.org:8888/token.html"]; NSString *htmlString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&amp;error]; if (error != nil) { NSLog(@"%s unable to load URL '%@'; error = %@", __FUNCTION__, url.absoluteString, error); } </code></pre> <p>You can parse it from there.</p> <p>For example, to get all of the text between the <code>&lt;h1&gt;</code> open and close tags from the <code>NSString</code> called <code>htmlString</code>, you could:</p> <pre><code>NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"&lt;h1&gt;(.*?)&lt;/h1&gt;" options:NSRegularExpressionCaseInsensitive error:&amp;error]; if (error != nil) { NSLog(@"%s regex error: %@", __FUNCTION__, error); } NSMutableArray *matches = [NSMutableArray array]; [regex enumerateMatchesInString:htmlString options:0 range:NSMakeRange(0, [htmlString length]) usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) { // the string including html tags is [htmlString substringWithRange:result.range] NSRange matchRange = [result rangeAtIndex:1]; [matches addObject:[htmlString substringWithRange:matchRange]]; }]; NSString *first = matches[0]; NSString *second = matches[1]; // etc. NSLog(@"first=%@", first); NSLog(@"second=%@", second); </code></pre> <hr> <p>I know you haven't said that you could do JSON, but if you could, the iOS implementation is incredibly easy. So, let's assume you had a URL that returned a JSON result like:</p> <pre><code>{ "username" : "myusername", "session" : "mysessionidentifier", "token" : "somecryptictoken2340923923402309234" } </code></pre> <p>You could then parse that with:</p> <pre><code>NSURL *url = [NSURL URLWithString:@"http://nonlinearmind.no-ip.org:8888/token.json"]; NSError *error; NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&amp;error]; if (error != nil) { NSLog(@"%s data read error %@", __FUNCTION__, error); return; } NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data options:0 error:&amp;error]; if (error != nil) { NSLog(@"%s JSON parse error %@", __FUNCTION__, error); return; } NSLog(@"username = %@", results[@"username"]); NSLog(@"session = %@", results[@"session"]); NSLog(@"token = %@", results[@"token"]); </code></pre>
    singulars
    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. VO
      singulars
      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