1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-31 03:00:05 +00:00

macOS: Added support for loading local files in WebBrowserComponent

This commit is contained in:
ed 2021-02-19 17:41:35 +00:00
parent 0fc0dce1b8
commit 9622fc8e05

View file

@ -45,9 +45,9 @@ static NSMutableURLRequest* getRequestForURL (const String& url, const StringArr
NSString* urlString = juceStringToNS (url);
#if JUCE_IOS || (defined (MAC_OS_X_VERSION_10_9) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_9)
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
#else
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
#endif
if (NSURL* nsURL = [NSURL URLWithString: urlString])
@ -312,7 +312,9 @@ public:
const StringArray* headers,
const MemoryBlock* postData)
{
if (url.trimStart().startsWithIgnoreCase ("javascript:"))
auto trimmed = url.trimStart();
if (trimmed.startsWithIgnoreCase ("javascript:"))
{
[webView evaluateJavaScript: juceStringToNS (url.fromFirstOccurrenceOf (":", false, false))
completionHandler: nil];
@ -322,8 +324,17 @@ public:
stop();
if (NSMutableURLRequest* request = getRequestForURL (url, headers, postData))
if (trimmed.startsWithIgnoreCase ("file:"))
{
auto file = URL (url).getLocalFile();
if (NSURL* nsUrl = [NSURL fileURLWithPath: juceStringToNS (file.getFullPathName())])
[webView loadFileURL: nsUrl allowingReadAccessToURL: nsUrl];
}
else if (NSMutableURLRequest* request = getRequestForURL (url, headers, postData))
{
[webView loadRequest: request];
}
}
void goBack() { [webView goBack]; }
@ -540,7 +551,24 @@ public:
stop();
if (NSMutableURLRequest* request = getRequestForURL (url, headers, postData))
auto getRequest = [&]() -> NSMutableURLRequest*
{
if (url.trimStart().startsWithIgnoreCase ("file:"))
{
auto file = URL (url).getLocalFile();
if (NSURL* nsUrl = [NSURL fileURLWithPath: juceStringToNS (file.getFullPathName())])
return [NSMutableURLRequest requestWithURL: nsUrl
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 30.0];
return nullptr;
}
return getRequestForURL (url, headers, postData);
};
if (NSMutableURLRequest* request = getRequest())
{
#if JUCE_MAC
[[webView mainFrame] loadRequest: request];