beginner's webKit questions
beginner's webKit questions
- Subject: beginner's webKit questions
- From: Jay Swartzfeger <email@hidden>
- Date: Tue, 21 Feb 2006 14:05:59 -0700
Hi all,
I'm just dipping my toes into the Cocoa/Xcode pool and experiencing a
few challenges. I've searched the archives and came up empty-handed
so decided to post my first request (I apologize if these answer have
been covered previously).
Also, I apologize in advance if these questions are too rudimentary
for the list -- I have four cocoa/xcode/objective-c books on order
and waiting for them to arrive.
Basically, I started my first project by trying various 'build your
own browser' tutorials. I've been mostly successful but have run into
some issues:
1. connectURL: and default http:// protocol
In my urlString field, I'm forced to enter the full url, ie "http://
www.apple.com/" ; simply entering "www.apple.com" won't work. Is
there a way to search for a : so if a user doesn't enter "http://"
the url will still work?
2. page titles and full url
I used didReceiveTitle to display a page's title, but page titles
simply show "untitled". Also, I used [urlString setStringValue:url];
to display the full url but it still only displays the base URL I
enter in my urlString. I can't see anything I entered incorrectly...
any ideas?
3. default homepage:
I used NSString *urlText = [NSString stringWithString:@"http://
www.google.com"]; to display a default homepage, but it only
intermittently displays. Did I implement it correctly, or can I chalk
this up to a connectivity issue?
4. user feedback
Most pages load correctly, but they give no user feedback that any
data is loading -- the page will remain blank and suddenly 'pop' into
place. This isn't a big deal for smaller pages, but larger pages
appear to take longer to load than they actually do. Is there a
method I can implement to show page elements that are fetched before
the rest of them are fully displayed?
I've included the .h and .m project files below. I think part of my
issue is that I haven't been learning directly from one source (ie,
books aren't here yet), so I've slapped together a "frankenstein"
project with code from a few different tutorials that are probably
conflicting.
Thanks for any responses. I've stuck with RealBasic the last few
years because I was intimidated by Cocoa/Xcode, but it's much more
user friendly than I had anticipated. Thanks for any feedback!
.h
---
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import <WebKit/WebView.h>
@interface MyDocument : NSDocument
{
IBOutlet id urlString;
IBOutlet WebView *webView;
}
//- (void)setDefaultHomepage:(NSString *)homepage;
//- (NSString *)getDefaultHomepage;
- (IBAction)connectURL:(id)sender;
@end
---
.m
---
#import "MyDocument.h"
//static NSString *defaultHP =@"http://www.google.com";
@implementation MyDocument
- (id)init
{
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message
and return nil.
}
return self;
}
- (NSString *)windowNibName
{
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if
your document supports multiple NSWindowControllers, you should
remove this method and override -makeWindowControllers instead.
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
[webView setUIDelegate:self];
[webView setGroupName:@"MyDocument"];
NSString *urlText = [NSString stringWithString:@"http://
www.google.com"];
[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL
URLWithString:urlText]]];
// Add any code here that needs to be executed once the
windowController has loaded the document's window.
// The two webView mods were added for JavaScript window support
}
- (NSData *)dataRepresentationOfType:(NSString *)aType
{
// Insert code here to write your document from the given data.
You can also choose to override -fileWrapperRepresentationOfType: or -
writeToFile:ofType: instead.
// For applications targeted for Tiger or later systems, you
should use the new Tiger API -dataOfType:error:. In this case you
can also choose to override -writeToURL:ofType:error:, -
fileWrapperOfType:error:, or -
writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
return nil;
}
- (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType
{
// Insert code here to read your document from the given data.
You can also choose to override -
loadFileWrapperRepresentation:ofType: or -readFromFile:ofType: instead.
// For applications targeted for Tiger or later systems, you
should use the new Tiger API readFromData:ofType:error:. In this
case you can also choose to override -readFromURL:ofType:error: or -
readFromFileWrapper:ofType:error: instead.
return YES;
}
- (IBAction)connectURL:(id)sender{
[urlString setStringValue:[sender stringValue]];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:
[sender stringValue]]]];
}
- (id)webView
{
return webView;
}
- (WebView *)webView:(WebView *)sender
createWebViewWithRequest:
(NSURLRequest *)request
{
id myDocument = [
[NSDocumentController
sharedDocumentController]
openUntitledDocumentOfType:
@"DocumentType"
display:YES];
[[[myDocument webView] mainFrame]
loadRequest:request];
return [myDocument webView];
}
- (void)webViewShow:(WebView *)sender
{
id myDocument = [[NSDocumentController
sharedDocumentController]
documentForWindow:
[sender window]];
[myDocument showWindows];
}
//- (void)setDefaultHomepage:(NSString *)homepage
//{
//defaultHP = homepage;
//}
//
//- (NSString *)getDefaultHomepage
//{
//return defaultHP;
//}
//
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:
(WebFrame *)frame
{
// Only report feedback for the main frame.
if (frame == [sender mainFrame]){
NSString *url = [[[[frame provisionalDataSource] request] URL]
absoluteString];
[urlString setStringValue:url];
}
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title
forFrame:(WebFrame *)frame
{
if (frame == [sender mainFrame]){
[[sender window] setTitle:title];
}
}
- (id)webView:(WebView *)sender
identifierForInitialRequest:(NSURLRequest *)request fromDataSource:
(WebDataSource
*)dataSource
{
int resourceCount;
int resourceFailedCount;
int resourceCompletedCount;
// Return some object that can be used to identify this resource
return [NSNumber numberWithInt:resourceCount++];
}
-(NSURLRequest *)webView:(WebView *)sender
resource:(id)identifier
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
fromDataSource:(WebDataSource *)dataSource
{
// Update the status message
[self updateResourceStatus];
return request;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden