Jump to content

How to automatically add "http://" prefix when in a WebKit custom web browser?


9 posts in this topic

Recommended Posts

Guest Ramm

How do I automatically add the "http://" prefix to a web address in Cocoa/Xcode? I am working on a browser and would like to know, so that way I don't have to enter in "http://example.com" every time. Any ideas? I have checked out cocoadevcentral and some other sites to no avail. Here is pretty much my whole MyDocument.m Objective-C code:

 

//
//  MyDocument.m
//  Rammbrowse
//
//  Created by Rammy on 5/8/07.
//  Copyright RammSoft. All rights reserved.
//
#import "PageLoad.h"
#import "MyDocument.h"
static NSString *defaultHP =@"http://www.insanelymac.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"];
[urlString setStringValue:defaultHP];
[[webView mainFrame] loadRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:defaultHP]]];
[webView setFrameLoadDelegate:self];
}

- (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
{
// Report feedback only for the main frame.
if (frame == [sender mainFrame]){
	[[sender window] setTitle:title];
}
}


@end

 

 

Any ideas?

  • 3 weeks later...

Couldn't you just do something as simple as reading the contents of the address bar/box, and if it's missing http://, then append it to the beginning?

 

[b]This is pseudo code[/b]
if( ! AddressBox.StringValue().Contains("http://") ) then
 AddressBox.Text = "http://" + AddressBox.Text
end if

Guest Ramm

Well, based on the code you showed above, I don't think you are thinking "Cocoa." Isn't the code above Basic? I am coding in Objective-C. And also, I know what I have to do (read to see if it contains HTTP://, if it doesn't, add http://)

Ok, some logics for you Ramm.

 

The first thing you should do is to check the whole string to see if it contains the substring "http://"

 

if ([yourString rangeOfString:@"http://"] == 0)        // This function returns 0 if the substring is not found
{                                                                         // so if it IS equal to zero, (substring not found) we dont have the http://, then let's add it.
   NSString *httpPrefix = @"http://";                     // CREATE a variable of type string with the "http://" prefix
   NSString *correctURL = [httpPrefix stringByAppendingString:yourString];   // Combine both strings to get the final one
}                                                                       // and later you should take care of instruct webkit to use the variable "correctURL".

 

This code could have little mistakes, i did it without actually testing it, but i think you got the idea. :lol:

Hope it helps.-

×
×
  • Create New...