Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

furycd001

macrumors member
Original poster
Mar 9, 2011
65
0
Belfast
HI all

I have created an app with a web view & a custom menubar entry. How can I link the two together so that whenever the menubar entry is clicked it loads a pre defined url inside the web view. I have already made an attempt but the code I have used loads the url in my default browser (safari) when the menubar entry is clicked it. All I really need to do is link the menubar to the webkit. Also the webkit is set to preload a url upon the app opening. Here is the code I have used below and your help will be much appreciated...

APPAppDelegate.h
Code:
-(IBAction)openLink:(id)sender;

APPAppDelegate.m
Code:
-(IBAction)openLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.google.com/"]];
}
 
Ok so I tried that but I get an error message now.

Code:
sending 'void' to parameter of incompatible type 'NSURL *'
 
APPAppDelegate.h
Code:
//
//  APPAppDelegate.h
//  Browser
//
//  Created by furycd001 on 01/05/2014.
//  Copyright (c) 2014 furycd001. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>


@interface APPAppDelegate : NSObject <NSApplicationDelegate>

{
    WebView *myWebView;
}

@property

(retain, nonatomic) IBOutlet WebView *myWebView;


@property (assign) IBOutlet NSWindow *window;

@property (assign) IBOutlet WebView *webView;

-(IBAction)openLink:(id)sender;

@end

APPAppDelegate.m
Code:
//
//  APPAppDelegate.m
//  Browser
//
//  Created by furycd001 on 01/05/2014.
//  Copyright (c) 2014 furycd001. All rights reserved.
//

#import "APPAppDelegate.h"

@implementation APPAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{

    NSRect frame=[NSScreen mainScreen].frame ;
    [self.window setFrame:frame display:YES animate:YES];
    
    NSURL *url = [NSURL URLWithString:@"http://www.example.com"];
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
    [[[self webView] mainFrame] loadRequest:urlRequest];
    [self.window setContentView:self.webView];
    
}

-(IBAction)openLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[myWebView setMainFrameURL:@"http://www.google.com/"]];
}

-(IBAction)openLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://www.google.co.uk/"]];
}


@end
 
The problem is this:
Code:
-(IBAction)openLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[myWebView setMainFrameURL:@"http://www.google.com/"]];
}
Red Menace meant "replace", not "add". Like so:
Code:
-(IBAction)openLink:(id)sender
{
    [myWebView setMainFrameURL:@"http://www.google.com/"];
}
The above code is an approximation. More may be required. I haven't tested this directly/


The use of NSWorkspace and openURL: is entirely wrong. That will open a URL in whatever app is appropriate to the URL scheme.

Since setMainFrame: returns nothing (type void), you can't just paste that code fragment into place, because it doesn't return a URL.

Read the class reference docs for NSWorkspace and WebView.


EDIT
You also have two methods with identical names. They need different names, or they are indistinguishable.
 
The problem is this:
Code:
-(IBAction)openLink:(id)sender
{
    [[NSWorkspace sharedWorkspace] openURL:[myWebView setMainFrameURL:@"http://www.google.com/"]];
}
Red Menace meant "replace", not "add". Like so:
Code:
-(IBAction)openLink:(id)sender
{
    [myWebView setMainFrameURL:@"http://www.google.com/"];
}
The above code is an approximation. More may be required. I haven't tested this directly/


The use of NSWorkspace and openURL: is entirely wrong. That will open a URL in whatever app is appropriate to the URL scheme.

Since setMainFrame: returns nothing (type void), you can't just paste that code fragment into place, because it doesn't return a URL.

Read the class reference docs for NSWorkspace and WebView.

I'm very new to coding so thank you for the help, it is much appreciated :) I will try that code out now.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.