Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening web URL with NSButton - Mac OS

Tags:

objective-c

I need some help with specific copy and paste code to help me get this running. I want users to click on a NSButton and it will launch safari or their default web browser.

I added a NSButton on my form and then added the declaration and implementation for that but still isn't working. why is it not working? Thanks in Advance...

 --------.h file--------
//  Copyright (c) 2013 Company. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "HttpRequest.h"
#import "JSON.h"
#import "ChimpKit.h"
#define IS_REGISTERED @"IS_REGISTERED"
#define NORMAL_0 0
#define MAILCHIMP_1 1

@protocol RegistrationWindowDelegate

-(void) registrationDidFinished;

@end

@interface RegistrationWindowController : NSWindowController<HttpRequestDelegate,ChimpKitDelegate,NSWindowDelegate> {


    IBOutlet NSButton *pushApp1; //Newly added NSButton for opening Web URL

    IBOutlet NSWindow *registerWindow;
    IBOutlet NSWindow *privacyWindow;
    IBOutlet NSTextField *firstNameField, *lastNameField, *emailField, *verfilyEmailField;
    IBOutlet NSPopUpButton *countryPopUpBtn;
    IBOutlet NSButton *updateCheckBox, *promotionCheckBox;
    IBOutlet NSMenuItem *registerMenuItem;
    IBOutlet NSProgressIndicator *progressIndicator;
    IBOutlet NSButton *registerSoftware,*registerLater;
    id<RegistrationWindowDelegate> delegate;

}
@property(nonatomic,assign) id<RegistrationWindowDelegate> delegate;
- (id)initForRegistrationServer:(int) registrationServer withDelegate:(id<RegistrationWindowDelegate>) delegate;
+ (BOOL) isRegistered;
- (void) setRegistered:(BOOL)state;
- (void) openWindow:(id) sender;
- (BOOL)isValidEmail:(NSString *)email;
-(void) registerWithNormal;
- (void) registerWithMailChimpForApps;
- (NSArray *)addCountries;

@end

and this is my .m file (I tried with snippets of code I found on the web)

--------.m file--------

//  Copyright (c) 2013 Computer. All rights reserved.
//

#import "RegistrationWindowController.h"

@interface RegistrationWindowController ()

@end

int registrationServer;

@implementation RegistrationWindowController
@synthesize delegate;

- (IBAction)pushApp1:(id)sender {

///Found this snippet on the web, but doesnt work
   NSURL *myURL = [NSURL URLWithString:@"http://www.google.com"];
   [[NSWorkspace sharedWorkspace] openURL:myURL];


  ///// Also Found this snippet on the web, but doesnt work
  // NSString *s = [ NSString stringWithFormat:@"http://www.google.com"];
  // [[NSApplication sharedApplication] openURL:[NSURL URLWithString:s]];



    }
like image 713
DevCompany Avatar asked Jul 05 '13 22:07

DevCompany


3 Answers

This should work:

-(IBAction)pushApp1:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
}
like image 156
Dani Avatar answered Nov 17 '22 03:11

Dani


Code for Swift 3

@IBAction func pushApp1(_ sender: Any) {
    NSWorkspace.shared().open(URL(string: "http://stackoverflow.com")!)
}
like image 8
Adam Bardon Avatar answered Nov 17 '22 01:11

Adam Bardon


swift 5:

@IBAction func gotoSite(_ sender: Any) {

    if let url = URL(string: OFFICIAL_SITE_URL) {
        NSWorkspace.shared.open(url)
    }

}
like image 1
ingconti Avatar answered Nov 17 '22 03:11

ingconti