views:

3739

answers:

4

I'm on the final stretch of my first simple iPhone application. I'm building an "about" view, with credits/info/etc.

I am failing on the simplest thing: how can I embed hyperlinks into the text? I'm current using a UIView with a UILabel for the text.

I've looked on here, and the sample apps, but not got anywhere. Should I use a UIWebView?

Thx.

+8  A: 

Yep, use a UIWebView and put static HTML in it.

Like so:

[myWebView loadHTMLString:@"<html><head></head><body style=\"font-family: sans-serif;\"> .... </body></html>" baseURL:nil];
frankodwyer
+4  A: 

You can trigger safari by calling UIApplication openURL: method with the URL you wish to display. This will close your app, and then open safari ( or mail / youtube / etc ).

You will want to make your link somehow, perhaps in a button. That part is up to you.

If you want to embed html content into your view, then by all means use a UIWebView.

Links require iPhone dev center login.

UIApplication openURL:

iPhone URL Scheme Reference

Ryan Townshend
A: 

Thanks Frank and Ryan.

In addition to Frank's direction, I also needed to implement the UIWebViewDelegate, and link to it in the Interface Builder. The reason was that every link that was clicked would open in my application (with no navigation possible...). I only needed to implement this method that opens every URL with the appropriate app:

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return false;
    }
    return true;
}
Bwooce
+4  A: 

To launch the web browser at the specified url:

NSURL *target = [[NSURL alloc] initWithString:@"http://www.stackoverflow.com"];
[[UIApplication sharedApplication] openURL:target];

This code can be run anywhere. I sub-classed UILabel, added the touchedsEnded method and place it in there. (Don't forget to set labelname.userInteractionEnabled = YES;)

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSURL *target = [[NSURL alloc] initWithString:@"http://www.stackoverflow.com"];
    [[UIApplication sharedApplication] openURL:target];
}
Paxic