2011-09-25 5 views
0

les gars. Je veux charger seulement une partie de la page Web, mais je ne peux pas le faire.Ouvrir une partie de la page html dans UIWebView

L'homme du forum russe a dit, que je peux utiliser PHP, mais je ne peux pas le créer dans mon application iOS. Il envoie ce 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); ?> 

Merci beaucoup.

Désolé pour mon mauvais anglais.

+1

Qu'est-ce que vous essayez d'accomplir? – zaph

Répondre

1

Voici un exemple travaillé pour vous. Il utilise All See Interactive pour effectuer la requête HTTP.

Dans le fichier d'en-tête que j'ai:

#import <UIKit/UIKit.h> 
#import "ASIHTTPRequest.h" 

@interface testViewController : UIViewController <UIWebViewDelegate>{ 
    UIWebView *webView; 
} 
@end 

Et dans le fichier de mise en œuvre je:

// 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); 
} 
0

Cela ne charge pas une partie d'une page Web, il charge tout le code HTML et renvoie une partie de celui-ci.

$code = file_get_contents($url); lit le code HTML complet de l'URL.

1

Quelle partie vous souhaitez charger sur UIWebView? Si vous savez quel contenu doit être affiché sur webView, chargez-le avec la méthode loadHTMLString. par exemple:

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]; 
Questions connexes