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

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:

Related

Cocoa NSTextField

i have a problem with accessing value from the NSTExtField in different class here is the code:
AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (assign) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSTextField *numberOfPhrases;
#end
AppDelegate.m
#import "AppDelegate.h"
#implementation AppDelegate
#synthesize numberOfPhrases;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSLog(#"%#",[numberOfPhrases stringValue]);
}
TestClass.h
#interface TestClass : NSObject
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass
- (IBAction)doSomething:(id)sender {
NSLog(#"%#",[numberOfPhrases stringValue]); ?????????
}
You obviously can't access the text field value in another class without a link to it.
To access the text field's value you either need to have one more IBOutlet to it in this class or an IBOutlet to AppDelegate so that you can access its property.
TestClass.h
#interface TestClass : NSObject
{
IBOutlet NSTextField *numberOfPhrases; // connect it to the new referencing outlet of text field by dragging a NSObject object in your xib and setting its class to "TestClass"
}
- (IBAction)doSomething:(id)sender;
#end
OR an another option is to have a IBOutlet of AppDelegate in TestClass (because if you only create a new instance of AppDelegate and not its IBOutlet then a different instance of text field will be created and you will not be able to access the value of your text field)
TestClass.h
#interface TestClass : NSObject
{
IBOutlet AppDelegate *appDel; // connect in the xib
}
- (IBAction)doSomething:(id)sender;
#end
TestClass.m
#implementation TestClass : NSObject
- (IBAction)doSomething:(id)sender
{
[[appDel numberOfPhrases]stringValue]; //get the string value in text field
}
#end
The only thing you're missing is the addition to your TestClass.m file:
#import "TestClass.h"
#import "AppDelegate.h"
#implementation TestClass
- (IBAction)doSomething:(id)sender {
AppDelegate *theInstance = [[AppDelegate alloc] init];
[theInstance numberOfPhrases];
}
#end
You need to include the class header of AppDelegate.h in TestClass.m, then you simply call an instance through [[AppDelegate alloc] init]; You'll need to link your NSTextField to the Sent Actions in Interface Builder do:Something -> TestClass and Referencing Outlets numberOfPhrases -> AppDelegate.
Output:
2014-01-21 23:32:56.499 test[6236:303] Wonders Never Cease

How to launch a web page on opening of the application

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.

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;
}

Xcode Storyboard - Transfer of data

I'm new to storyboarding so the answer to this may be simplistic,
I've created a viewController in which a UITextField is present. My test is to transfer the data (text) from that text field into a viewController that is pushed onto the screen.
The coding that i have is as follows:
ViewController1.h -
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : UIViewController
#property (nonatomic, retain) IBOutlet UITextField *inputText;
#end
ViewController1.m
#import "ViewController1.h"
#implementation ViewController
#synthesize inputText;
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showTextController"]) {
ViewController2 *vc2 = [segue destinationViewController];
vc2.selectedText.text = self.inputText.text;
}
}
#end
ViewController2.h -
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
#property (nonatomic, retain) IBOutlet UILabel *selectedText;
#end
ViewController2.m
#import "ViewController2.h"
#implementation ViewController2
#synthesize selectedText;
#end
The segue between viewController1 and 2 in storyboard is referred to as 'showTextController'.
Is this the correct coding for something so simple? Do i need to be using 'ViewDidLoad' method along with the prepareForSegue:sender method?
It looks correct to me. In fact, this is simpler than we have had in the past since the storyboard takes care instantiating our view controller objects. One thing to note is that if you're using ARC then you shouldn't be retaining your UILabel.
Hope this helps.

Resources