How to load specific url in Webview programmatically? - objective-c

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)

Related

Segmented control to change pages in UIWebView

I've been going through example after example trying to compare my project with others to see what I'm doing incorrectly however I have yet to find the answer. In short what I'm attempting to do for class is have a three button segmented control that will change the url that a UIWebView displays.
However, when the app is ran it crashes on launch with the following console output
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x7f969ab2dd80> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key appleWebView.'
*** First throw call stack:
The following is my code:
ViewController.M
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIScrollView *theScroller;
#property (weak, nonatomic) IBOutlet UISegmentedControl *appleSite1;
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.theScroller.contentSize = CGSizeMake(280, 1000.0);
}
- (IBAction)appleSite1:(UIButton *)sender
{
NSURL *url = [[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.google.com"]];
[self.myWebView loadRequest:[NSURLRequest requestWithURL:url]];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
//{
// IBOutlet UIWebView myWebView;
//}
//
#end
I'm only testing with the one site (ebay) until I can figure out exactly what I'm missing. Obviously I'm a student and am learning so please bear with me.
Thanks in advance.
I don't know if you already solve your problem...
The crash is maybe caused by Interface Builder which is linking your UIWebView to a property (IBOutlet) named appleWebView. I think that you renamed appleWebView to myWebView.
To fixe this open the controller's xib (or storyboard), select the UIWebView, in the right panel open the connections inspector and remove the link to appleWebView.
Hope it helps!

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.

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

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

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.

Receiver type 'WebFrame' for instance message is a forward declaration

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.