1

I'm making an iphone app...can I get the source from it in objective-c?

Thanks! Elijah

2 Answers 2

1

You can get anything from the web view that JavaScript can return using stringByEvaluatingJavaScriptFromString:. Try something along the lines of

document.lastChild.outerHTML

The document will likely have two child nodes; the first will be the DOCTYPE, and the second the <html> element.

Sign up to request clarification or add additional context in comments.

Comments

0

If you need to get the contents of the current URL in your web view you could have something like this in a UIViewController. I didn't compile this code, so there may be some syntax errors, but this should be a solid starting point for your problem.

@interface aViewController : UIViewController <UIWebViewDelegate>
    UIWebView *webView;
    NSString *htmlSource;
@end
@implementation aViewController 

-(void) viewDidLoad {
    CGRect webFrame = [[UIScreen mainScreen] applicationFrame];

    self.webView = [[UIWebView alloc] initWithFrame:webFrame];
    self.webView.delegate = self;

    NSURL *aURL = [NSURL URLWithString:@"http://www.myurl.com"];
    NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
    //load the index.html file into the web view.
    [self.webView loadRequest:aRequest];
}    
//This is a delegate method that is sent after a web view finishes loading content.
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"finished loading");
    self.htmlSource = [[NSString alloc] initWithData:[[webView request] HTTPBody] encoding:NSASCIIStringEncoding];
}

@end

4 Comments

Two requests to the same URL doubles your chances of an error response, adds the potential for inconsistent responses, and increases your use of the radio. Caching might mitigate these issues, but why depend on that when you already have the data?
What are you referring to by two requests to the same URL?
Ahh, I see what you mean. I've updated it to retrieve the source based on the HTTPBody of the UIWebView's NSURLRequest.
The request property is the request that the web view sent to the server to obtain the content, not the response from the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.