How to launch a web page on opening of the application - cocoa

Hello everyone i need some help. I'm trying to make an application for mac in Xcode 4.0 and i want it to display a webpage on launch i have already added the webkit framework and have got some code but its not working. the code is below
The app delegate.m file
#import "AppDelegate.h"
#import <WebKit/WebKit.h>
#implementation AppDelegate
#synthesize window = _window;
#synthesize WebView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[WebView mainFrame] LoadRequest:requestObj];
}
#end
and the app delegate.h file
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (assign) IBOutlet WebView *WebView;
#interface myAppDelegate : NSObject <NSApplicationDelegate> {
WebView *myWebView;
}
#property (retain, nonatomic) IBOutlet WebView *myWebView;
#end
If anyone can help please respond asap thanks guys!

hmm, i'm assuming that code isn't compiling? (it shouldn't be! :) ). the .h should look something like:
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
WebView *_myWebView; // not needed if you're using the latest xcode
}
#property (assign) IBOutlet NSWindow *window;
#property (retain, nonatomic) IBOutlet WebView *myWebView;
#end
and the .m:
#import "AppDelegate.h"
#import <WebKit/WebKit.h>
#implementation AppDelegate
#synthesize window = _window;
#synthesize myWebView = _myWebView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlAddress = #"http://www.google.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[self.myWebView mainFrame] loadRequest:requestObj]; // note the lower case 'l' in 'loadRequest'
}
#end
this assumes you've connected the webview in the .xib with the myWebView property.

Related

When I call this method, it returns 0

Logic is: read data from a view (3 textfields) then when a button is pressed, sum data which stored in the model and print it out.
<--Header file-->
#import <Cocoa/Cocoa.h>
#class QuoteAttributes;
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTextField *amountOfHardware;
#property (weak) IBOutlet NSTextField *amountOfPrinter;
#property (weak) IBOutlet NSTextField *amountOfSoftware;
#property (strong) QuoteAttributes *quickQuote;
- (IBAction)calculate:(id)sender;
- (void) setValueTotheForm;
#end
See the following Code:
#import "AppDelegate.h"
#import "QuoteAttributes.h"
#implementation AppDelegate
#synthesize amountOfHardware;
#synthesize amountOfPrinter;
#synthesize amountOfSoftware;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
}
- (void) setValueTotheForm{
[self.quickQuote setNumHardware:[self.amountOfSoftware intValue]];
[self.quickQuote setNumPrinter:[self.amountOfPrinter intValue]];
[self.quickQuote setNumSoftware:[self.amountOfSoftware intValue]];
int totalQuote = [self.quickQuote numHardware] + [self.quickQuote numPrinter] + [self.quickQuote numSoftware];
NSLog(#"%d", totalQuote);
}
- (IBAction)calculate:(id)sender {
[self setValueTotheForm];
}
#end
However, when I call this method, it prints 0
should initiate the object first :
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
QuoteAttributes *qa = [[QuoteAttributes alloc] init];
[self setQuickQuote: qa];
}
then the abovementioned code will work.

View Switching: Unrecognised selector sent to instance

I made some example that changes view by clicking button.
When clicking the "fist" button in view, I keep getting the error:
[__NSArrayM changeLogView:]: unrecognized selector sent to instance 0x100539e30
Here is the simplified code:
DSWAppDelegate.h
#import <Cocoa/Cocoa.h>
#class DSWHomeViewController;
#interface DSWAppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSButton *logBtn;
#property (weak) IBOutlet NSTabView *tabView;
#end
DSWAppDelegate.m
#import "DSWAppDelegate.h"
#import "DSWLogViewController.h"
#implementation DSWAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
[self.tabView setTabViewType:NSNoTabsNoBorder];
NSTabViewItem *item = nil;
DSWLogViewController *logvc = [[DSWLogViewController alloc initWithNibName:#"DSWLogViewController" bundle:[NSBundle mainBundle]];
item = [[NSTabViewItem alloc] initWithIdentifier:#"log"];
[item setView:logvc.view];
[self.tabView insertTabViewItem:item atIndex:0];
}
#end
DSWLogViewController.h
#import <Cocoa/Cocoa.h>
#interface DSWLogViewController : NSViewController
#property (weak) IBOutlet NSButton *firstBtn;
#property (weak) IBOutlet NSButton *secondBtn;
#property (weak) IBOutlet NSBox *box;
- (IBAction)changeLogView:(id)sender;
- (IBAction)testSelector:(id)sender;
#end
DSWLogViewController.m
#import "DSWLogViewController.h"
#implementation DSWLogViewController
- (IBAction)changeLogView:(id)sender
{
NSLog(#"changeLogView : %#", sender);
}
- (IBAction)testSelector:(id)sender
{
NSLog(#"testSelector : %#", sender);
}
#end
And I checked class name of file owner.
Something is sending a changeLogView: message to a mutable array instead of your view controller. Chances are, whatever you've connected the IBAction to is pointing to the wrong object - an array of some sort, rather than your view controller.

How to start a page with web view in the Xcode?

I see here that many using this code but do working with me. Well. (DESKTOP APP)
Simple webview auto laucher URL
I will go to talk step by step
1) Create a project
2) Create a window and a web view
3) Put the identifier how prevelwindow (web view) and windows (window)
4) In My .H
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
WebView *prevelwindow;
}
#property (strong) IBOutlet NSWindow *window;
#property (strong) IBOutlet WebView *prevelwindow;
#end
5) In My .M
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize window;
#synthesize prevelwindow;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSString *urlString = #"http://www.google.com.br";
[[prevelwindow mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
#end
6) Well then I compile it and it is not working.
First off you must add the (Targets->Build Phases->Link Binary).
Then you can #import it in your .h AppDelegate file and declare a new WebView:
#import "WebKit/WebKit.h"
#interface Check_AccountzAppDelegate : NSObject <NSApplicationDelegate> {
WebView *MyWebView;
}
#property (retain, nonatomic) IBOutlet WebView *MyWebView;
Now you can load a new Request (.m AppDelegate file):
#synthesize MyWebView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
[[MyWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
}
And finally add a WebView, and connect it to your MyWebView IBOutlet in your .nib file:
New WebView:
Connect the IBOutlet:

Webview in Cocoa doesn't load

It Keeps throwing error: Receiver type webFrame for instance message is a forward declaration on the line "[[webView mainFrame ] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];"
my .h file
#interface AttendanceWizardAppDelegate : NSObject <NSApplicationDelegate>
{
#private WebView *webView;
}
#property (weak) IBOutlet WebView *webView;
#property (assign) IBOutlet NSWindow *window;
#end
My .m file
#import "AttendanceWizardAppDelegate.h"
#implementation AttendanceWizardAppDelegate
#synthesize Username = _Username;
#synthesize Password = _Password;
#synthesize webView = _webView;
#synthesize webber = _webber;
#synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlStr = #"www.google.com";
[[webView mainFrame ] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
}
#end
You simply need to add the import of WebKit headers in your header file:
#import <WebKit/WebKit.h>
Your code can also be simplified, by not defining a instance variable for the properties you declare:
Header file (.h):
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface AttendanceWizardAppDelegate : NSObject <NSApplicationDelegate>
// No need for a iVar declaration
#property (weak) IBOutlet WebView *webView;
#property (assign) IBOutlet NSWindow *window;
#end
Implementation file (.m):
#import "AttendanceWizardAppDelegate.h"
#implementation AttendanceWizardAppDelegate
// Simplified synthesize declarations (no reference to the iVar)
#synthesize Username; // I suggest you change the name of this variable ; the convention is to start the name of your property with a lower case letter, to not confuse it with a class name
#synthesize Password;
#synthesize webView;
#synthesize webber;
#synthesize window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSString *urlString = #"www.google.com";
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; // Note the use of self.webView, to use the getter you created by declaring the property
}
#end

No declaration of property ... found in interface - But there is

I have the following error: No declaration of "window" found in interface.
Though when I look there is one... There's probably something stupid I missed, but can't find it.
PlanetoidsAppDelegate.h
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
#property (assign) IBOutlet NSWindow* window;
#property (nonatomic, retain) IBOutlet WebView* webView;
#end
PlanetoidsAppDelegate.m
#import "PlanetoidsAppDelegate.h"
#implementation PlanetoidsAppDelegate
#synthesize window; //<-- error here
#synthesize webView; //<-- error here (well, the same one for webView that is...)
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}
- (void)awakeFromNib {
NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
NSString *htmlPath = [resourcesPath stringByAppendingString:#"/Planetoids/Planetoids_Game.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; //<-- error: webView undeclared, what makes sense considdering the other errors :P
}
#end
Can anyone here see the error?
Your interface is WebViewExampleAppDelegate but your implementation is PlanetoidsAppDelegate. These must match.
#interface WebViewExampleAppDelegate : NSObject in .h
#implementation PlanetoidsAppDelegate in .m
Two completely different classes.You need to be implementing WebViewExampleAppDelegate.m and synthesizing those methods in that class.
Also, for this:
#interface WebViewExampleAppDelegate : NSObject {
NSWindow *window;
IBOutlet WebView *webView;
}
try
#interface WebViewExampleAppDelegate : NSObject {
UIWindow *window;
IBOutlet WebView *webView;
}

Resources