user1532706
user1532706

Reputation: 51

Xcode Button to Launch URL

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.

Upvotes: 5

Views: 21718

Answers (2)

changtung
changtung

Reputation: 1624

Use another viewcontroller with webView embeeded. This solution force to stay in your application during watching url content.

  1. Create another view controller on storyboard.
  2. place webview on it. ( name it webView )
  3. connect web view with controller class through assistant editor ( assuming that You connected new UIView class to view controller )
  4. in viewDidLoad of your new class insert:

     [super viewDidLoad];
     NSString *fullURL = @"http://google.pl";
    
     NSURL *url = [NSURL URLWithString:fullURL];
    
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:requestObj];
    

Upvotes: -1

lobianco
lobianco

Reputation: 6276

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"]];
}

Upvotes: 25

Related Questions