cocoa - How to load NSURL that contains “#” with WebView? -
i have url string contains "#".for example,
nsstring* urlstr = @"https://developer.apple.com/library/ios/#/legacy/library/documentation/xcode/conceptual/ios_development_workflow/10-configuring_development_and_distribution_assets/identities_and_devices.html#//apple_ref/doc/uid/tp40007959-ch4-sw"; urlstr = [urlstr stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]; nsurl* url = [nsurl urlwithstring:urlstr]; [[self.webviewsample mainframe] loadrequest:[nsurlrequest requestwithurl:url]];
after using encoding ,the "#" replaced "%23".if don't use encoding, nsurl nil. problem webview load incorrect webpage different browser.how can handle url string can load correct webpage?
so think question now, root problem apple's documentation uses rather oddly formed urls. contain more 1 #
character, isn't technically valid url. first valid (and important); others should escaped.
safari able handle this, think because more display/use raw url string. best solution i've found date hand off webview
's pasteboard handling, so:
- (nsurl *)urlfromstring:(nsstring *)string; { static nspasteboard *pboard; if (!pboard) pboard = [[nspasteboard pasteboardwithuniquename] retain]; [pboard clearcontents]; [pboard writeobjects:@[string]]; nsurl *result = [webview urlfrompasteboard:pboard]; return result; }
more details @ http://www.mikeabdullah.net/webkit-encode-unescaped-urls.html
Comments
Post a Comment