How to use WebView on xcode 4.5.1 - xcode

I'm new to programming and I need to embed a website in my application (which is blank; I only want to embed the website right now). I've been searching for it since 5:00 PM (It's now 9:30 PM) and I still didn't find anything about that.
What code do I need and in what file I need to write it? What do I have to link together?
I use Xcode 4.5.1 and I'm trying to do a Cocoa application for Mac OS X (not for iOS).
I'm sorry if some of my sentences are not clear, but English is not my primary language.
If you need more information in order to help me, just ask.

In your AppDelegate.h file, add this line below the #import <Cocoa/Cocoa.h> line:
#import <WebKit/WebKit.h>
and add this line below the #property (assign) IBOutlet NSWindow *window; line:
#property (assign) IBOutlet WebView *webView;
Select your MainMenu.xib file.
Open the window inside it, then drag a WebView from the Object Library browser into the window. Align and size it.
There should be an icon representing your AppController object to the left of your UI layout. Control-drag from it to your WebView inside your window. (Do not control-drag from your File's Owner icon!) Release the mouse button. A contextual menu should appear, containing the word webView. Select it.
Add the framework WebKit.framework to your project. Right-click on your Frameworks folder in the resource list on the left side of the Xcode window. Choose "Add files to "<your project name>"... and select the framework using this path: /System/Library/Frameworks/WebKit.framework.
Select your AppDelegate.m file.
In your -applicationDidFinishLaunching: method, replace the comment with this code:
// I provided Apple's URL, but this is where you provide your own instead.
NSURL *url = [NSURL URLWithString:#"http://www.apple.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[[self webView] mainFrame] loadRequest:urlRequest];
Build and run. When the window appears, you should see it load the web page you described in the URL.
A few final words:
I see that you're new here. What I've just done, in the context of Stack Overflow, is to give you a gift. You need to try a little harder looking for resources on the Web. I found two myself, but because they're a bit old (and the development tools look different enough), I embarked upon this answer. I want you to promise that you'll work harder to find answers for yourself. A great place to begin is by reading Apple's own very excellent documentation.

Did you find the Apple tutorial on this very subject:
WebView *webview = [[WebView alloc] init]; // or initialise using the modern-equivalent of InterfaceBuilder
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlText]]];

Related

How to add an outlet between a storyboard and an NSDocument

I've done iOS for a few years, but I'm new to OS X.
I'm using the document-based template in Xcode 6. I added a textfield to the view controller in the storyboard. How do I access it from the Document?
I added:
#property(nonatomic, weak)IBOutlet NSString* aString;
to Document.h. When I drag to it, IB seems to be forcing me to use bindings. OK with me, but I'm still very weak on bindings. The dialog box asks for a number of values, most of which I can guess. But, what does "Custom Class" mean, and what should I put there?
A little help here would certainly be appreciated.
Thanks
You don't. The IBOutlet for the textfield should be in the NSViewController subclass (ViewController in the template Xcode project).
Your next question will be "How do I access the NSDocument from my view controller?"; Mac App Storyboard - Access Document in NSViewController ;-)

What is the equivalent to UIViewController methodology for OSX?

I am new to OSX development.
Before storyboards on iOS, when you created a single view application, you would end with this structure:
AppDelegate.h
AppDelegate.h
ViewController.h
ViewController.m
ViewController.xib
The app would start on the delegate and call the xib and its classes, so the entry point to the app would be viewDidLoad inside ViewController.m
I have created an app for OSX. All I have is
AppDelegate.h
AppDelegate.m
MainMenu.xib
I would now to recreate something like the ViewController.h and ViewController.m and transfer control to something like a viewDidLoad, but I see this MainMenu.xib of cocoa has a window inside.
What do I do? Create a custom class for this window? I did not see any equivalent to viewDidLoad, viewDidAppear, etc. What is the equivalent for NSWindow or if this is now how it is done, please tell me.
The AppDelegate should have a applicationDidFinishLaunching which you can use as the entry point. the AppDelegate has a window associated with it by default. You can use AppDelegate to create the various features you would like to use. An alternative is to create a subclass of NSWindowController and have an associated xib for that which you can load when you want. Hope this helps.

Downgrading from Storyboard to XIB

So I understand that I can not use storyboard on anything less then 5.0. I have finished my App using storyboard only so am very basic to code.
I am going to re-create my app using XIB, I will create a new XIB file for each of my storyboards.
Want I would like help with is the code I need to cross-fade between each XIB. There will a button linking each XIB.
What do I need to add to ViewController.h and where do I put the code into ViewController.m
Thanks a lot for your help.
Bryce
This is what I use.
- (IBAction)changeViews:(id)sender
{
ModalViewControllerTwo *modalViewControllerTwo = [[ModalViewControllerTwo alloc] init];
modalViewControllerTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[rootViewController presentModalViewController:modalViewControllerTwo animated:YES];
}

iPad: Launch MainWindow.xib's primary view without using InterfaceBuilder confi

I get really confused using InterfaceBuilder to setup the main window xib file's primary controller view. Is there a good example or simple way to launch the ViewController I want to show by default in the Application Delegate instead of setting it up in InterfaceBuilder's MainWindow.xib file?
you can show your view by code as
FirstViewController *fvc = [[FirstViewController alloc] init];
[window addSubView:fvc];

Cocoa webView to display a web page

I am trying to develop an export aperture plugin, I have designed some views/windows and everything runs smoothly, but trying to now add a webView on one of the window, and cannot make it to display the linked url (hard typed in the code).
Is webView not supported because I'm running it within aperture? anyone having problem with this?
I'm not getting any errors, and used the webView object in the simplest way as possible.
The code to load the web page on webView:
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
Make sure you hooked up the outlet to the web view in IB (assuming that the web view is in a nib). Many, many Cocoa programmers forget that.
If it's not in a nib, please edit your question to include the code you used to create it.

Resources