2009-11-09 7 views

Répondre

0

Si vous utilisez un UIWebView, vous avez le balisage html à votre disposition. Ainsi, vous pouvez faire quelque chose comme ceci:

NSString *html = @"This is a very nice <font color=\"green\">#thing</font>"; 
[myWebView loadHTMLString:html baseURL:[NSURL URLWithString: @"http://localhost"]]; 
+0

Mais comme le texte arrive est aléatoire, que comment le détecter à l'exécution ?? – crystal

+0

Je suppose que vous feriez une recherche et remplacer sur le texte pour "détecter à l'exécution" – wkw

2

Vous devez utiliser UIWebView et faire les cliquables articles ancres HTML standard, puis surveiller le chargement de lien dans la méthode UIWebViewDelegate shouldStartLoadWithRequest

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSURL* u = [request URL]; 

    if([[u scheme] isEqualToString:@"showlicenses"]) { 
     NSString *path = [[NSBundle mainBundle] 
      pathForResource:@"credits" ofType:@"html" inDirectory:@"help"]; 

     LicensesWebviewController *vc = [[LicensesWebviewController alloc] initWithURL:[NSURL fileURLWithPath:path]]; 
     vc.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
     [self presentModalViewController:vc animated:YES]; 
     [vc release]; 

     return NO; // DO NOT attempt to load URL 
    } 

    return YES; // if you want to allow the URL to load 
} 

Vous » Vous devez définir le délégué sur votre UIWebView.

myWebView.delegate = self;

Donc quand un lien dans ma page HTML avec le format: "showLicenses: // blahblah" est tapé, je pousse un nouveau contrôleur de vue.

Vous pouvez utiliser n'importe quel type de liens que vous voulez, il vous suffit d'examiner et de piéger ceux que vous voulez gérer en interne. par exemple, "myscheme: /// faire/quelque chose/avec/ce/lien"

+0

La méthode initwithURL ne trouve pas ... – crystal

+0

Eh bien ... ce code fournit un exemple, mais [[LicensesWebviewController alloc] initWithURL: [ NSURL fileURLWithPath: path]] est un ViewController dans l'une de mes applications. vous devez écrire votre propre, ou faire ce que vous voulez avec le lien. – wkw

Questions connexes