How to load URL on launch in a WebView (OSX project)? - objective-c

I am just starting to develop mac apps and I want that the WebView a URL when the app start.Here's my code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
//other instance variables
}
#property
(retain, nonatomic) IBOutlet WebView *myWebView;
//other properties and methods
#end
AppDelegate.m:
#import "AppDelegate.h"
#import <WebKit/WebKit.h>
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlText = #"http://google.com";
[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
return;
// Insert code here to initialize your application
}
#end
How to load URL on launch in a WebView (OSX project)?
I think that the code work but when I try to connect the code with the WebView in Interface Builder,I can't find the "web view" in the list of outlets.
Thanks
I'have update my code following the last post but is still doesn't work.
Thanks again for you replies.

You need to add WebKit framework.
#import <WebKit/WebKit.h>

Hard to be sure what the issue is here, so a guess...
Which way are you dragging the connection in IB?
To connect your outlet you want to drag from the outlet shown in the Inspector to the web view:
If you drag in the other way, from the web view to the App Delegate in the outline you are trying to connect an action.
You also have an issue in your code, your instance variable:
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
WebView *myWebView;
//other instance variables
}
will not be used by your property:
#property (retain, nonatomic) IBOutlet WebView *myWebView;
as your property is automatically synthesised and so will create an instance variable _myWebView. You should see a compiler warning to this effect.
This in turn means the statement:
[[myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
will not do what you expect, as myWebView will be nil and not refer to your WebView. You should refer to the property as self.myWebView:
[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
With those changes you should see Google in your web view.
HTH

Related

How to load specific url in Webview programmatically?

I am trying to make a basic search tool in cocoa. I am trying to take the string from a textfield, edit it to make a link that searches the string in google, and then feed that into the WebView. However, Xcode is not letting me use the variable I made for the Web View. I'm getting errors along the lines of "Unknown type name 'WebView'" and "Bad Receiver type '*int'". Any help would be appreciated. Here's my code for
NewWindowController.h:
#import <Cocoa/Cocoa.h>
#interface NewWindowController : NSWindowController
#property (weak) IBOutlet NSComboBox *searchselector;
- (IBAction)onclickGO:(id)sender;
#property (weak) IBOutlet WebView *webv;
#end
NewWindowController.m:
#import "NewWindowController.h"
#interface NewWindowController ()
#end
#implementation NewWindowController
#synthesize searchselector, webv;
- (void)windowDidLoad {
[super windowDidLoad];
NSLog(#"Window did load");
}
- (IBAction)onclickGO:(id)sender {
NSInteger *indexofsearch = [searchselector indexOfSelectedItem];
NSLog(#"%d",indexofsearch);
//test for web view to load page
[[webv mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: #"https://google.com"]]];
}
#end
WebView is part of WebKit.framework, which is not included in the default projects.
Add #import WebKit; to the top of whatever file you're referencing the WebView object in.
Add WebKit.framework to "Link Binary with Libraries" (in Build Phases)

Objective-c: Open URL in WebView (Mac)

I'm having a problem with opening a URL in WebView. Haven't programed with objective-c in awhile and it looks like what I'm coding has depreciated. The application opens, loads the URL but crashes with this error
Thread 1:EXC_BAD_ACCESS (code=1, address=0x20)
I originally used this to help me program in the past: [How to load URL on launch in a WebView (OSX project)?
Here's the code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
}
#property
(retain, nonatomic) IBOutlet WebView *myWebView;
#end
AppDelegate.m
#import "AppDelegate.h"
#import <WebKit/WebKit.h>
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlText = #"http://google.com";
[[self.myWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
return;
// Insert code here to initialize your application
}
#end
I receive a warning in AppDelegate.m before I build
Autosynthesized property 'myWebView' will use synthesized instance
variable '_myWebView', not existing instance variable 'myWebView'
How can I fix this?
For second part of your question, that is for opening a URL in webview there is a fork of phoneGap(http://phonegap.com) called Macgap available. you can check that out here, http://github.com/maccman/macgap
Delete your declaration of WebView *myWebView; insider your curly brackets, that's not necessary.

Working with two windows

I am new to programming specially objective c.
I have a two windows in MainMenu.xib.
1st window (with webview) is launching when i run my application.
I am able to launch 2nd window (with a text field and save button) from top menu.
I have specified a default URL as "www.google.com" for first launch.
Now i want to configure URL by putting it in textfield on 2nd window. Whenever i click "SAVE" 1st window should refresh with newly given URL.
How is it possible and how i can connect action button "SAVE" that it impact on 1st window.
Here is my code
AppDelegate.h contains
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet WebView *webview;
#property (assign) IBOutlet NSTextField *urlString;
- (IBAction)save:(id)sender;
#end
**And my AppDelegate.m contains**
#implementation AppDelegate
#synthesize webview;
#synthesize urlString;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlString = #"http://www.google.com";
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
-(IBAction)save{
[[webview mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString.stringValue]]];
}
#end
Thanks
You need to open the Save Menu and Ctrl+Drag to your class.
Set the action to your save or create a new one.
(Either call your method from IBAction method created by XCode or put your code there.)

Xcode 4.5.2 - OS X Application - Link webview to code, load hand-coded homepage

I am using Xcode 4.5.2.
I created a new project (OS X > Application > Cocoa Application).
Under linked Frameworks and Libraries I added Webkit.framework.
Under MainMenu.xib I added a Web View object to the Window
Under MainMenu.xib I added 2 Push Buttons and named them Back and Forward.
I control clicked on the buttons and linked them to the Web View and selected goBack: and goForward:
Under MainMenu.xib I added a Text Field object
I control clicked on the text field and linked it to the Web View and selected takeStringURLFrom:
All of that worked. I can enter a web address into the text field and press return and use the back and forward buttons.
I want to hand code a default website (http://www.google.com/) to load and I'm having trouble figuring out what to do from here?
What do I link my web view to (when I control click it)?
What do I need to add to my AppDelgate.h and AppDelgate.m files to load Google into the web view when the app opens?
I did some research and got this far. I was able to find a working example but I am trying to understand how it works step-by-step and am stuck on this step.
If you want your delegate to load the Google homepage after the application launches, you can simply add the following to your applicationDidFinishLaunching::
NSURL* url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[[self.webView mainFrame] loadRequest:request];
This assumes you have the following in your AppDelegate.h:
#property (weak) IBOutlet WebView *webView;
which you placed by CTRL-dragging from the WebView control in Interface Builder into your AppDelegate.h.
Final code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet WebView *webView;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSURL* url = [NSURL URLWithString:#"http://google.com"];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[[self.webView mainFrame] loadRequest:request];
}
#end

OS X app WebView not loading through code

I have spent about a whole day of grief trying to solve this question, and this is my last resort. I have a web view in my OS X app. I made a web browser and it is working great with navigation arrows and all, but when I try to make a home page (google) It does not load on the WebView (called webber in the code). I made sure all the outlets are connected. here is my code for AppDelegate.h:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>{
NSWindow *window;
NSTextField *urlBox;
IBOutlet WebView *webber;
IBOutlet NSTextField *googleSearchField;
}
-(IBAction)search;
#property (assign) IBOutlet NSWindow *window;
#end
and here is the code for AppDelegate.m:
#import "AppDelegate.h"
#implementation AppDelegate
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController{
NSLog(#"Nib loaded");
NSString *urlText = #"http://www.google.com";
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];
}
-(IBAction)search
{
NSString *searchString = [googleSearchField stringValue];
NSString *searchURL = [NSString stringWithFormat:#"http://www.google.com/search?q=%#", searchString];
[[webber mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: searchURL]]];
}
#end
I would very much appreciate ANY form of help and I am very desperate. Please remember that I am only in 6th grade, and a step by step guide or just plain code is what I am looking for. Thank you for your time! download the Xcode project here: http://www.cadenfarley.com/cobra/Download.html Scroll down until you see "Xcode project here"
Your windowControllerDidLoadNib: method is never called because you don't have an NSWindowController anywhere. Rename that method to - (void)awakeFromNib and it should work.