tkelly2000
tkelly2000

Reputation: 41

Internal URL with a Fragment ID

I have a large .htm file in the main bundle I wish to load into a webview. No problem until I append a fragment id to the end of the URL. I want the webview to open at an anchor id #c on a button press.

BTW: If I create a URL http://www.mydomain.com/database.htm#c and load this from the Internet. All all is good.

I have tried several approaches. Both have failed. I am guessing the server side does the parsing of the URL on the internet, but the iPhone does not have that capability. Does anyone have a suggestion? Please be explicit with examples if possible. I am a noob and on my own.

Stumped

 NSString *path         = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
 NSString *pathWithFrag = [NSString stringWithFormat:@"%@%@%",path, @"#c"];
 NSURL *baseURL         = [ NSURL fileURLWithPath:pathWithFrag];
 [spinner2 loadRequest:[NSURLRequest requestWithURL:baseURL]];
 [super viewDidLoad];   

OR

 NSString *path  = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
 NSURL *baseURL  = [ NSURL fileURLWithPath:path];
 NSURL *newURL   = [baseURL URLByAppendingPathComponent:@"#c"];
 [spinner2 loadRequest:[NSURLRequest requestWithURL:newURL]];
 [super viewDidLoad];

Upvotes: 4

Views: 528

Answers (2)

Jeff Ames
Jeff Ames

Reputation: 2044

It looks like it may an issue with NSURL url-escaping the "#" character: http://9mmedia.com/blog/?p=299

Following the method on that blog post, something like this might work:

NSString *path = [[NSBundle mainBundle] pathForResource:@"database1.htm" ofType:nil];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSURL *fullURL = [NSURL URLWithString:@"#c" relativeToURL:baseURL];
[spinner2 loadRequest:[NSURLRequest requestWithURL:fullURL]];
[super viewDidLoad];

Upvotes: 3

Jeff Ames
Jeff Ames

Reputation: 2044

The extra % in the pathWithFrag format may be causing problems: try @"%@%@", not @"%@%@%"

Upvotes: 0

Related Questions