guys. I want to load only pa开发者_开发知识库rt of web page, but I can't do it.
Man from Russian forum told, that I can use PHP, but I can't create this in my iOS app. He send this code:
> <?php
> $url = "http://www.shesterenka.com";
> $unique_start = "<h1>";
> $unique_end = "</h1>";
> function weather($url, $unique_start, $unique_end) {
> $code = file_get_contents($url);
> preg_match('/'.preg_quote($unique_start,
> '/').'(.*)'.preg_quote($unique_end, '/').'/Us', $code, $match);
> return $match[1];
> }
> echo weather($url, $unique_start, $unique_end); ?>
Thank you very much.
Sorry for my bad English.
Here's a worked example for you. It uses All See Interactive to make the HTTP request.
In the header file I have:
#import <UIKit/UIKit.h>
#import "ASIHTTPRequest.h"
@interface testViewController : UIViewController <UIWebViewDelegate>{
UIWebView *webView;
}
@end
And in the implementation file I have:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
[webView setDelegate:self];
[webView setScalesPageToFit:YES];
NSURL *url = [NSURL URLWithString:@"http://www.shesterenka.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
self.view = webView;
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSString *unique_start = @"<h1>";
NSRange i = [responseString rangeOfString:unique_start];
NSString *unique_end = @"</h1>";
NSRange j = [responseString rangeOfString:unique_end];
if(i.location != NSNotFound && j.location != NSNotFound)
{
NSString *weather = [responseString substringFromIndex:i.location];
weather = [responseString substringToIndex:j.location];
[webView loadHTMLString:weather baseURL:nil];
} else {
NSLog(@"strings not found");
}
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"Error: %@",error);
}
What part you want to load on UIWebView? If you know what content needs to be displayed on webView then load with loadHTMLString method. for example:
NSString* content = @"Hello World";
NSString* beforeBody = @"<html><head></head><body>";
NSString* afterBody = @"</body></html>";
NSString* finalContent = [[beforeBody stringByAppendingString:content]
stringByAppendingString: afterBody];
[webView loadHTMLString:finalContent baseURL:nil];
That isn't loading part of a web page, it is loading the entire html and returning part of it.
$code = file_get_contents($url);
is reading the entire URL's html.
精彩评论