Linking menubar entry to web view url

Discussion in 'Mac Programming' started by furycd001, Jun 2, 2014.

  1. furycd001 macrumors member

    furycd001

    Joined:
    Mar 9, 2011
    Location:
    Belfast
    #1
    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/"]];
    }
     
  2. Red Menace macrumors 6502

    Red Menace

    Joined:
    May 29, 2011
    Location:
    Littleton, Colorado, USA
    #2
    Use WebView class methods, for example:
    Code:
    [myWebView setMainFrameURL:@"http://www.google.com"];
     
  3. furycd001 thread starter macrumors member

    furycd001

    Joined:
    Mar 9, 2011
    Location:
    Belfast
    #3
    Thank you for your reply. I will give that a try and see if it works...
     
  4. furycd001 thread starter macrumors member

    furycd001

    Joined:
    Mar 9, 2011
    Location:
    Belfast
    #4
    Ok so I tried that but I get an error message now.

    Code:
    sending 'void' to parameter of incompatible type 'NSURL *'
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Post your code. Copy and paste the actual code into a reply; don't retype it.
     
  6. furycd001 thread starter macrumors member

    furycd001

    Joined:
    Mar 9, 2011
    Location:
    Belfast
    #6
    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
    
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    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.
     
  8. furycd001 thread starter macrumors member

    furycd001

    Joined:
    Mar 9, 2011
    Location:
    Belfast
    #8
    I'm very new to coding so thank you for the help, it is much appreciated :) I will try that code out now.
     

Share This Page