Re: Using an anchor in NSURL file url
Would it be possible to parse the anchor out from the url and then once the page is completely loaded in the webview use javascript's window.location to jump to the anchor's location within the page. I know that can work for UIWebView, not 100% sure for WKWebView. > On Jan 21, 2016, at 7:17 PM, Jeff Evanswrote: >> >> Colleagues, >> >> 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 most correct way would be to use NSURLComponents to append the > fragment: > > NSURL* fileURL = [NSURL fileURLWithPath:somePathString]; // or, even > better, an API that gives file URLs directly, skipping the path string > NSURLComponents* components = [NSURLComponents componentsWithURL:fileURL > resolvingAgainstBaseURL:YES]; > components.fragment = @"anchorname"; > NSURL* urlWithFragment = components.URL; > > > If you can't use NSURLComponents because you're targeting releases of OS X > before 10.9, you should just use string operations: > > NSURL* fileURL = // ⦠as above ⦠> NSString* urlString = fileURL.absoluteString; > urlString = [urlString stringByAppending:@"#anchorname"]; > NSURL* urlWithFragment = [NSURL URLWithString:urlString]; > > Regards, > Ken > > > ___ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/diederik%40tenhorses.com > > This email sent to diede...@tenhorses.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Using an anchor in NSURL file url
> On Jan 21, 2016, at 6:23 PM, Jeff Evanswrote: > > //The full url appears to have the anchor prepended, plus a couple of > dashes: > > #page3-- [pathtobook is here]/book/chapter1.html The .description property of a URL assembled with -relativeToURL is weird looking like that, but it’s not the actual URL. To see the actual URL, use the .absoluteString property instead. Or convert the URL to an absolute URL by getting its .absoluteURL property. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Using an anchor in NSURL file url
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 page 3> Yours, Jeff On Jan 21, 2016, at 5:32 PM, Quincey Morris wrote: On Jan 21, 2016, at 17:17 , Jeff Evanswrote: > > 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 %23. 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 (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Using an anchor in NSURL file url
On Jan 21, 2016, at 7:17 PM, Jeff Evanswrote: > > Colleagues, > > 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 most correct way would be to use NSURLComponents to append the fragment: NSURL* fileURL = [NSURL fileURLWithPath:somePathString]; // or, even better, an API that gives file URLs directly, skipping the path string NSURLComponents* components = [NSURLComponents componentsWithURL:fileURL resolvingAgainstBaseURL:YES]; components.fragment = @"anchorname"; NSURL* urlWithFragment = components.URL; If you can't use NSURLComponents because you're targeting releases of OS X before 10.9, you should just use string operations: NSURL* fileURL = // … as above … NSString* urlString = fileURL.absoluteString; urlString = [urlString stringByAppending:@"#anchorname"]; NSURL* urlWithFragment = [NSURL URLWithString:urlString]; Regards, Ken ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Using an anchor in NSURL file url
Colleagues, 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 %23. I tried [NSURL URLWithString: anchorname relativeToURL: baseURL] but this produces a url with the form #anchorname--[path]/filename.html and that doesn't work (opens the file, but ignores the anchor). Is there any way to use an anchor in a file url for WKWebView? Thanks, Jeff ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Using an anchor in NSURL file url
On Jan 21, 2016, at 17:17 , Jeff Evanswrote: > > 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 %23. 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. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com