Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Commit

Permalink
#88 cache server - add support for /Library/.. and /Documents/.. link…
Browse files Browse the repository at this point in the history
…s, instead of the fully qualified paths.
  • Loading branch information
EddyVerbruggen committed May 31, 2015
1 parent e7abd95 commit 4413a93
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ _BETA_ - things may break, [please post your feedback :)](https://github.com/Edd
* [Ionic](http://ionicframework.com/) tip: to prevent flashes of a black background, make sure you set `ion-nav-view`'s `background-color` to `transparent`.
* If you need the [device plugin](org.apache.cordova.device), use at least Cordova-iOS 3.6.3 (deviceready never fires with 3.5.0 due to a currently unknown reason).
* When making AJAX requests to a remote server make sure it supports CORS. See the [Telerik Verified Marketplace documentation](http://plugins.telerik.com/plugin/wkwebview) for more details on this and other valuable pointers.
* You can load files from the app's cache folders by using the entire path (/var/.../Library/...) or simply '/Library/..' (or '/Documents/..').

## 2. Screenshot
This image shows the [SocialSharing plugin](https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin) in action while running [a performance test](https://www.scirra.com/demos/c2/particles/) in an iframe on my iPhone 6 (older devices show an even larger difference).
Expand All @@ -41,6 +42,7 @@ $ cordova prepare
No need for anything else - you can now open the project in XCode 6 if you like.

## 4. Changelog
* __0.3.8__ Adding a way to access files in '/Library/' and '/Documents/' (simply use those prefixes), thanks #88!
* __0.3.7__ Custom URL Schemes did not work, see #98, also this version includes crash recovery, thanks #62!
* __0.3.6__ Bind embedded webserver to localhost so it can't be reached from the outside, thanks #64!
* __0.3.5__ Compatibility with the statusbar plugin: allow the statusbar to not overlay the webview, thanks #6 and #20!
Expand Down
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="com.telerik.plugins.wkwebview"
version="0.3.8-dev">
version="0.3.8">

<name>WKWebView Polyfill</name>

Expand Down
30 changes: 20 additions & 10 deletions src/ios/AppDelegate+WKWebViewPolyfill.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ @implementation AppDelegate (WKWebViewPolyfill)

GCDWebServer* _webServer;
NSMutableDictionary* _webServerOptions;
NSString* appDataFolder;

+ (void)load {
// Swap in our own viewcontroller which loads the wkwebview, but only in case we're running iOS 8+
Expand All @@ -34,6 +35,7 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer {
self.viewController = myMainViewController;
self.window.rootViewController = myMainViewController;
[self.window makeKeyAndVisible];
appDataFolder = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByDeletingLastPathComponent];

// Initialize Server environment variables
NSString *directoryPath = myMainViewController.wwwFolderName;
Expand All @@ -47,16 +49,9 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer {
cacheAge:60
allowRangeRequests:YES];

// Add handler for anything under Data, like Documents and Library
[_webServer addHandlerForMethod:@"GET"
pathRegex:@"/.*/Data/"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
NSData *d = [NSData dataWithContentsOfFile:request.URL.path];
return [GCDWebServerDataResponse responseWithData:d contentType:@"application/octet-stream"];
}
];

[self addHandlerForPath:@"/Library/"];
[self addHandlerForPath:@"/Documents/"];

// Initialize Server startup
if (startWebServer) {
[self startServer];
Expand All @@ -66,6 +61,21 @@ - (void) createWindowAndStartWebServer:(BOOL) startWebServer {
[myMainViewController setServerPort:_webServer.port];
}

- (void)addHandlerForPath:(NSString *) path {
[_webServer addHandlerForMethod:@"GET"
pathRegex:[@".*" stringByAppendingString:path]
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
NSString *fileLocation = request.URL.path;
if ([fileLocation hasPrefix:path]) {
fileLocation = [appDataFolder stringByAppendingString:request.URL.path];
}
NSData *d = [NSData dataWithContentsOfFile:fileLocation];
return [GCDWebServerDataResponse responseWithData:d contentType:@"application/octet-stream"];
}
];
}

- (BOOL)identity_application: (UIApplication *)application
openURL: (NSURL *)url
sourceApplication: (NSString *)sourceApplication
Expand Down

0 comments on commit 4413a93

Please sign in to comment.