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.
Related
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)
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.
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
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.)
I can't seem to get an online GIF image to load into an NSImageView. Please see my code below and offer some comments to remedy this.
#import <Foundation/Foundation.h>
#interface ImageFromUrl : NSObject
#property (strong) NSString *urlString;
#property (strong) NSURL *url;
#property (strong) NSImage *radarGif;
#property (weak) IBOutlet NSImageView *radarGifView;
- (void)displayUrlImage:(id)sender;
#end
and
#import "ImageFromUrl.h"
#implementation ImageFromUrl
#synthesize urlString, url, radarGif, radarGifView;
- (void)displayUrlImage:(id)sender {
urlString = #"http://icons.wunderground.com/data/640x480/2xradarb5.gif";
url = [NSURL URLWithString:urlString];
radarGif = [[NSImage alloc] initWithContentsOfURL:url];
[radarGifView setImage:radarGif];
}
#end
I am receiving no errors when I run the application and the image of the GIF is not showing in the window.
I put this code in a project, and it worked fine, so the code works. How did you connect the outlet to the image view (do you have an object in IB that's an instance of this class)? How are you calling the method (and from where)?