Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode Button to Launch URL

Tags:

xcode

button

I've created a simple app that has a button in the bottom toolbar and I can't seem to get the button to work. I've tried to attach an action to it that will open a URL when pressed but it did not work. So I've removed that action and I'm hoping someone can help. I've posted the compressed xcode project at this URL https://www.box.com/s/5d45ce1df7d9dd0fe205

Any help is appreciated.

like image 503
user1532706 Avatar asked Jul 17 '12 18:07

user1532706


People also ask

How do I open a Link in SwiftUI?

SwiftUI gives us a dedicated Link view that looks like a button but opens a URL in Safari when pressed. It's easy enough to use – just give it a title for the button, plus a destination URL to show, like this: Link("Learn SwiftUI", destination: URL(string: "https://www.hackingwithswift.com/quick-start/swiftui")!)


1 Answers

Something like this would work. This is an example of a text-only button:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 100, 40)];
[button setBackgroundColor:[UIColor clearColor]];
[button setTitle:@"Google" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openGoogleURL) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

And the button's selector:

-(void)openGoogleURL
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
}
like image 156
lobianco Avatar answered Nov 15 '22 09:11

lobianco