简体   繁体   中英

How can I display a website (e.g. apple) in a cocoa app using WebKit?

Ok guys, so far I've coded the whole thing by following other people's answers however when I build and run my project the website or html files I specify in the code don't display on the screen; no more than a white screen is displayed.

This is what I've done so far on my header file:

//MY HEADER FILE(.h):

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

@interface Project_XAppDelegate: NSObject <NSApplicationDelegate> {
NSWindow *window;
IBOutlet WebView *webView;
}

@property (assign) IBOutlet NSWindow *window;
@property (nonatomic, retain) IBOutlet WebView *webView;

@end

Implementation:

//MY IMPLEMENTATION FILE(.m):

#import "Project_XAppDelegate.h"
#import "WebKit/Webkit.h"

@implementation Project_XAppDelegate

@synthesize window;
@synthesize webView;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}

- (void)awakeFromNib {
    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/Users/*****/Desktop/Project X/HTML/mehmar.html"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]];
}

@end

Your htmlPath looks wrong to me. Not too familiar with OSX development, but once you get the bundle path shouldn't you just append "index.html" to it instead of the complete path to your file on disk.

Try [resourcesPath stringByAppendingString:@"index.html"] ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM