Adding Safari to a Cocoa Project

MAC OSX Programming Involving Anything from Objective-C Cocoa, Carbon to Anything working on the Apple Box!
Admin
Site Admin
Posts: 951
Joined: Thu Aug 23, 2007 10:35 am

Adding Safari to a Cocoa Project

Postby Admin » Sun Oct 14, 2012 12:03 am

Add the object to the XIB interface
Import the Webkit framework
Add the code and Ctrl Drag to App Delegate

Code: Select all

//
//  AppDelegate.h
//  MyFirstApplications
//
//  Created by user on 10/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
@private
   
WebView *MyWebView;
}

- (IBAction)doSomethingElse:(id)pId;

@property (assign) IBOutlet WebView *MyWebView;

@property (assign) IBOutlet NSWindow *window;

@end

Code: Select all

//
//  AppDelegate.m
//  MyFirstApplications
//
//  Created by user on 10/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
//@synthesize MyWebView;

- (IBAction)doSomethingElse:(id)pId; {
    NSLog(@"Hi there");
    int a = 5;
    a = a + 1;
    NSString *urlAddress = @"http://www.google.com";
   
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];
   
    //URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //[[WebView web] loadRequest:[NSURL URLWithString:@"google.com"]];
    [[MyWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
    //[MyWebView loadRequest:requestObj];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end

Admin
Site Admin
Posts: 951
Joined: Thu Aug 23, 2007 10:35 am

Re: Adding Safari to a Cocoa Project

Postby Admin » Sun Oct 14, 2012 1:21 am

Code: Select all

//
//  AppDelegate.h
//  MyFirstApplications
//
//  Created by user on 10/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate> {
@private
   
    WebView *MyWebView;
    NSButton *MyNavButton;
}

- (IBAction)doSomethingElse:(id)pId;

//This makes the Property show in AppDelegate
@property (assign) IBOutlet WebView *MyWebView;
@property (assign) IBOutlet NSButton *MyNavButton;
@property (assign) IBOutlet NSWindow *window;

@end

Code: Select all

//
//  AppDelegate.m
//  MyFirstApplications
//
//  Created by user on 10/13/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

@implementation AppDelegate

@synthesize window = _window;
//@synthesize MyWebView;

- (IBAction)doSomethingElse:(id)pId; {
    NSLog(@"Hi there");
    int a = 5;
    a = a + 1;
    NSString *urlAddress = @"http://www.google.com";
   
    //Create a URL object.
    NSURL *url = [NSURL URLWithString:urlAddress];
   
    //URL Requst Object
    //NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    //[[WebView web] loadRequest:[NSURL URLWithString:@"google.com"]];
    [[MyWebView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]];
    MyNavButton.title = @"test";
    //[MyWebView loadRequest:requestObj];
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

@end


Return to “XCode Cocoa Carbon”

Who is online

Users browsing this forum: No registered users and 1 guest