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.
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 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 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.
I'm learning ObjC and cocoa dev and have come across a real 'stumper'. Having exhausted Google, I respectfully adorn my desperation hat and present to you:
A class and a view controller:
The class 'Content Window' imports a viewcontroller instance and places it in a window:
ContentWindow.h
#import <Foundation/Foundation.h>
#import <WebKit/WebView.h>
#import "ContentViewController.h"
#interface ContentWindow : NSWindow{
ContentViewController* viewController;
}
#property IBOutlet ContentViewController* viewController;
-(NSWindow *) newWindow;
#end
ContentWindow.m
#import "ContentWindow.h"
#implementation ContentWindow
#synthesize viewController;
-(NSWindow *) newWindow{
//Builds the window as 'window' and displays it successfully here
//... [code redacted for brevity]
// Build view
viewController = [[ContentViewController alloc] initWithNibName:#"ContentViewController" bundle:nil];
[window setContentView: viewController.view];
NSString *urlString = #"http://www.google.com";
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
[viewController.title setStringValue:#"my title"];
}
#end
I am attempting to do two things with the interface:
[viewController.title setStringValue:#"my title"];
This successfully sets the view element 'title' to "my title".
[[viewController.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
This, however, throws the error:
Receiver type 'WebFrame' for instance message is a forward declaration.
and underlines in red the section of the line:
viewController.webView mainFrame
My view controller is as follows:
ContentViewController.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#interface ContentViewController : NSViewController {
IBOutlet NSTextField *title;
IBOutlet WebView *webView;
}
#property IBOutlet WebView *webView;
#property IBOutlet NSTextField *title;
#end
ContentViewController.m
#import "ContentViewController.h"
#interface ContentViewController ()
#end
#implementation ContentViewController
#synthesize title, webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
#end
Finally to use this class, I am instantiating a content window from my AppDelegate class with
contentWindow = [[ContentWindow new] newWindow];
Having imported ContentWindow.h into AppDelegate.h and having set:
__strong NSWindow * contentWindow
as an AppDelegate synthesised instance variable.
I have linked both items in IB (definitely!) I have also added Webkit foundation to my project, which was suggested in another thread.
I can't for the life of me understand what is going on. I know that the logical answer is to put down Xcode and pick up the 'Learn Xcode and Objective c' book (with a bookmark about half way through where I was arrogant enough to think I'd learned enough to try something out), but before I do that, on the off-chance:
Could anyone help?
Thanks as always, AtFPt.
Usually this error message means, that the type of a class is not know (since declared by #class).
Make sure, that your code can see a declaration of WebFrame.
If so, maybe you add it later and XCode works with older meta data. In this case, a clean before build usually helps.
So, I've been looking at the documentation and Googling, but I can't figure out how to call a method when my WebView (OSX, not iOS), changes... All the examples seem to be for iOS. My code looks like this:
SNAFAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface SNAFAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *browser;
- (IBAction)forwards:(id)sender;
#end
SNAFAppDelegate.m
#import "SNAFAppDelegate.h"
#implementation SNAFAppDelegate
#synthesize window = _window;
#synthesize browser = _b;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[[_b mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://google.com"]]];
}
#end
SNAFBrowser.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebView.h>
#import <WebKit/WebFrame.h>
#import <WebKit/WebEditingDelegate.h>
#import <WebKit/WebKit.h>
#interface SNAFBrowser : WebView
#property (assign) IBOutlet WebView *browser;
#end
SNAFBrowser.m
#import "SNAFBrowser.h"
#implementation SNAFBrowser
#synthesize browser = _b;
- (void)webViewDidChange:(NSNotification *)notification
{
NSLog(#"Hello World");
}
#end
Then, in my MainMenu.xib file, I linked the Outlet "browser" from the AppDelegate to the WebView and set the WebView's custom class to SNAFBrowser.
Essentially, I just want to know when the webview loads another page.
I'm probably doing something ridiculously stupid, so any help is appreciated.
Implement the WebFrameLoadDelegate protocol, and see which methods are suitable to implement, for example webView:didFinishLoadForFrame:.
Example:
#interface SNAFBrowser : WebView <WebFrameLoadDelegate>
...
#end
#implementation SNAFBrowser
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
NSLog(#"Something got loaded!");
}
...
#end