开发者

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 seve开发者_开发问答ral 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];


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];


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜