Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hyperlinks within "about" text for iphone application

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.

like image 918
Bwooce Avatar asked Jan 11 '09 14:01

Bwooce


4 Answers

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];
like image 108
frankodwyer Avatar answered Sep 28 '22 13:09

frankodwyer


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];
}
like image 23
Paxic Avatar answered Sep 28 '22 13:09

Paxic


UIWebView is probably the wrong way to do this. It's VERY much overkill for something like this. You're should check out a Github project called LRLinkableLabel.

It will auto-detect any URLs that are inside the .text property.

You can use it like so:

LRLinkableLabel *label = [[LRLinkableLabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 20.0)];
label.delegate = self;
label.text = @"Check out http://dundermifflin.com to find some great paper deals!";

Then just make sure self implements this method:

- (void) linkableLabel:(LRLinkableLabel *)label clickedButton:(UIButton *)button forURL:(NSURL *)url {
    [[UIApplication sharedApplication] openURL:url];
}

You can also use the linkColor and textColor properties to configure the appearance of the label. From this point you can use it just like any other UILabel.

Remember to set the delegate to nil when you're all done to make sure everything is all cleaned up.

Hope this helps.

like image 21
Jake Marsh Avatar answered Sep 28 '22 13:09

Jake Marsh


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.

Documentation:

  • UIApplication openURL: (deprecated)
  • UIApplication openURL:options:completionHandler: (iOS 10+)
  • iPhone URL Scheme Reference
like image 20
Ryan Townshend Avatar answered Sep 28 '22 12:09

Ryan Townshend