BluGeni
BluGeni

Reputation: 3454

Open url with variable syntax

I have a variable agencyWebsite and a label that should open the website when clicked with this method:

- (void)website1LblTapped {
    NSURL *url = [NSURL URLWithString:self.agencyWebsite];
    [[UIApplication sharedApplication] openURL:url];
}

I get a warning in the compiler saying:

Incompatible pointer types sending UILabel* to parameter of type NSString*

And the app crashes when the link is clicked. Any suggestions?

Edit: Here is what I am doing to make the label clickable

UITapGestureRecognizer* website1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(website1LblTapped)];
    // if labelView is not set userInteractionEnabled, you must do so
    [self.agencyWebsite setUserInteractionEnabled:YES];
    [self.agencyWebsite addGestureRecognizer:website1LblGesture];

What i used to get it working

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://%@", self.agencyWebsite.text]];

Upvotes: 0

Views: 95

Answers (1)

rdurand
rdurand

Reputation: 7410

If agencyWebsite is of type UILabel*, you need to access its text property instead of passing the object itself to URLWithString:.

- (void)website1LblTapped {

    NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
    [[UIApplication sharedApplication] openURL:url];
}

Calling self.agencyWebsite will return your UILabel* object, whereas self.agencyWebsite.text will return a NSString* object containing the text from the label.

Upvotes: 1

Related Questions