Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The second question is actually easier to answer. Look at the <a href="https://developer.apple.com/documentation/foundation/nsstring/1497360-stringwithcontentsofurl?language=objc" rel="nofollow noreferrer"><code>stringWithContentsOfURL:encoding:error:</code></a> method of NSString - it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example:</p> <pre><code>NSString *googleString = @"http://www.google.com"; NSURL *googleURL = [NSURL URLWithString:googleString]; NSError *error; NSString *googlePage = [NSString stringWithContentsOfURL:googleURL encoding:NSASCIIStringEncoding error:&amp;error]; </code></pre> <p>After running this code, <code>googlePage</code> will contain the HTML for www.google.com, and <code>error</code> will contain any errors encountered in the fetch. (You should check the contents of <code>error</code> after the fetch.)</p> <p>Going the other way (from a UIWebView) is a bit trickier, but is basically the same concept. You'll have to pull the <a href="https://developer.apple.com/documentation/uikit/uiwebview/1617972-request?language=objc" rel="nofollow noreferrer">request</a> from the view, then do the fetch as before:</p> <pre><code>NSURL *requestURL = [[yourWebView request] URL]; NSError *error; NSString *page = [NSString stringWithContentsOfURL:requestURL encoding:NSASCIIStringEncoding error:&amp;error]; </code></pre> <p><strong>EDIT:</strong> Both these methods take a performance hit, however, since they do the request twice. You can get around this by grabbing the content from a currently-loaded UIWebView using its <code>stringByEvaluatingJavascriptFromString:</code> method, as such:</p> <pre><code>NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString: @"document.body.innerHTML"]; </code></pre> <p>This will grab the current HTML contents of the view using the Document Object Model, parse the JavaScript, then give it to you as an NSString* of HTML.</p> <p>Another way is to do your request programmatically first, then load the UIWebView from what you requested. Let's say you take the second example above, where you have <code>NSString *page</code> as the result of a call to <code>stringWithContentsOfURL:encoding:error:</code>. You can then push that string into the web view using <code>loadHTMLString:baseURL:</code>, assuming you also held on to the NSURL you requested:</p> <pre><code>[yourWebView loadHTMLString:page baseURL:requestURL]; </code></pre> <p>I'm not sure, however, if this will run JavaScript found in the page you load (the method name, <code>loadHTMLString</code>, is somewhat ambiguous, and the docs don't say much about it).</p> <p>For more info:</p> <ul> <li><a href="https://developer.apple.com/documentation/uikit/uiwebview?language=objc" rel="nofollow noreferrer">UIWebView</a> class reference</li> <li><a href="https://developer.apple.com/documentation/foundation/nsstring?language=objc" rel="nofollow noreferrer">NSString</a> class reference</li> <li><a href="https://developer.apple.com/documentation/foundation/nsurl?language=objc" rel="nofollow noreferrer">NSURL</a> class reference</li> </ul>
 

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