Re: Using an anchor in NSURL file url
Re: Using an anchor in NSURL file url
- Subject: Re: Using an anchor in NSURL file url
- From: Jeff Evans <email@hidden>
- Date: Thu, 21 Jan 2016 18:23:02 -0800
Thanks -
Here's what I'm doing: Assume a directory called book and a file called chapter1, and an anchor called #page3
NSString *filePath = [[NSBundle mainBundle] pathForResource: chapter1 ofType: @"html" inDirectory: book];
//That file path gets converted to a base url:
NSURL *baseURL = [NSURL fileURLWithPath:filePath];
//I then create a combined url that includes the anchor, following a suggestion I saw elsewhere:
NSURL *fullURL = [NSURL URLWithString: #page3 relativeToURL:baseURL];
//Then I create a url request with that:
NSURLRequest *nsrequest = [NSURLRequest requestWithURL: fullURL];
//And pass it to the WKWebView:
[webView loadRequest:nsrequest];
//Which loads the chapter file very nicely, but ignoring the anchor.
//The full url appears to have the anchor prepended, plus a couple of dashes:
#page3-- [pathtobook is here]/book/chapter1.html
//Instead of what I'd expect,
[pathtobook is here]/book/chapter1.html#page3
So I don't quite understand that format. I expect I'm missing something basic here. The chapter does contain the anchor <a name="page3"> page 3></a>
Yours, Jeff
On Jan 21, 2016, at 5:32 PM, Quincey Morris wrote:
On Jan 21, 2016, at 17:17 , Jeff Evans <email@hidden> wrote:
>
> In OSX I'm using an NSURLRequest to load a file url in WKWebView. Works fine unless I try to add an anchor to the path, for example,
>
> [path]/filename.html#anchorname
>
> The problem appears to be that the # gets escaped to #.
Can you show what APIs you’re using for this? Are you appending the anchor to an existing string or URL? Are you explicitly creating a file URL, or a generic URL?
Looking at NSURL documentation, it looks like NSURL recognizes the anchor as a piece called “fragment”.
> I tried [NSURL URLWithString: anchorname relativeToURL: baseURL] but this produces a url with the form
This doesn’t seem likely to work. In this API, the first parameter is assumed to *be* a URL, albeit in relative form, not a URL piece, which is what you have.
If this email is spam, report it to www.OnlyMyEmail.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden