

How to load NSURL that contains "#" with WebView?
source link: https://www.codesd.com/item/how-to-load-nsurl-that-contains-with-webview.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to load NSURL that contains "#" with WebView?
I have a URL string which 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 "#" is replaced by "%23".If you don't use encoding, the NSURL will be nil. My problem is that the webview load incorrect webpage which is different from Browser.How can I handle this url string so that I can load the correct webpage?
So I think from your question now, the root problem is that Apple's documentation uses some rather oddly formed URLs. They contain more than one #
character, which isn't technically valid for a URL. The first is valid (and important); any others should be escaped.
Safari is able to handle this, I think because it does more than just display/use the raw URL string. The best solution I've found to date is to hand off to WebView
's pasteboard handling, like 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 at http://www.mikeabdullah.net/webkit-encode-unescaped-urls.html
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK