开发者

ios get page source from web?

I am working on a a开发者_JS百科pp and try to display part of a web page.

My idea is first, get page source then parse it!

I already found a useful HTML parser for it but still struggling in how to get page source?

All I found is about UIWebview. Something like:

uiwebview = [[UIWebView loadRequest:[NSURLRequest requestWithURL:url]]; 


Use

NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"] encoding:NSUTF8StringEncoding error:nil];


The disadvantage to using +stringWithContentsOfURL: in the main thread is that your UI will block while the request is active. This becomes especially problematic when the user is on a network with high latency, when the server is slow to respond, or if the request ends up timing out. In the last case, the user may see the UI block for a very long time.

The +stringWithContentsOfURL: method also lacks a way to provide you with error information in the event the server does not return a 200 status.

To perform the request asynchronously without blocking the UI, use NSURLConnection and grab the data in the delegate:

- ( void )connection: (NSURLConnection *)connection didReceiveData: (NSData *)data
{
    // receivedData is an NSMutableData object
    [ receivedData appendData: data ];
}

And then kick off parsing when the connection finishes:

- ( void )connectionDidFinishLoading: (NSURLConnection *)connection
{
    [ self parseHTMLData: receivedData ];
}

The URL Loading System Programming Guide will get you started.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜