diff --git a/CachedWebView.xcodeproj/project.pbxproj b/CachedWebView.xcodeproj/project.pbxproj index ab34f63..3ab2a20 100644 --- a/CachedWebView.xcodeproj/project.pbxproj +++ b/CachedWebView.xcodeproj/project.pbxproj @@ -64,9 +64,9 @@ FB237EBE14D5B86100F30AD8 = { isa = PBXGroup; children = ( + FB39D08214D5BB1F002A6D1A /* README.md */, C800AAE6166E77B20027E1E5 /* NSString+Sha1.h */, C800AAE7166E77B20027E1E5 /* NSString+Sha1.m */, - FB39D08214D5BB1F002A6D1A /* README.md */, FB237EEA14D5B87400F30AD8 /* RNCachingURLProtocol.h */, FB237EEB14D5B87400F30AD8 /* RNCachingURLProtocol.m */, FB237EEC14D5B89700F30AD8 /* Reachability.m */, @@ -218,7 +218,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + ARCHS = "$(ARCHS_STANDARD)"; CLANG_ENABLE_OBJC_ARC = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = NO; @@ -244,7 +244,7 @@ isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; - ARCHS = "$(ARCHS_STANDARD_32_BIT)"; + ARCHS = "$(ARCHS_STANDARD)"; CLANG_ENABLE_OBJC_ARC = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; COPY_PHASE_STRIP = YES; @@ -268,6 +268,7 @@ GCC_PREFIX_HEADER = "CachedWebView/CachedWebView-Prefix.pch"; INFOPLIST_FILE = "CachedWebView/CachedWebView-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; WRAPPER_EXTENSION = app; }; name = Debug; @@ -279,6 +280,7 @@ GCC_PREFIX_HEADER = "CachedWebView/CachedWebView-Prefix.pch"; INFOPLIST_FILE = "CachedWebView/CachedWebView-Info.plist"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; WRAPPER_EXTENSION = app; }; name = Release; diff --git a/CachedWebView/AppDelegate.h b/CachedWebView/AppDelegate.h index 7af46c0..051cc59 100644 --- a/CachedWebView/AppDelegate.h +++ b/CachedWebView/AppDelegate.h @@ -34,6 +34,6 @@ @property (strong, nonatomic) UIWindow *window; -@property (strong, nonatomic) ViewController *viewController; +@property (strong, nonatomic) UITabBarController *viewController; @end diff --git a/CachedWebView/AppDelegate.m b/CachedWebView/AppDelegate.m index e32926f..044c453 100644 --- a/CachedWebView/AppDelegate.m +++ b/CachedWebView/AppDelegate.m @@ -33,18 +33,41 @@ @implementation AppDelegate -@synthesize window = _window; -@synthesize viewController = _viewController; - - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { - [NSURLProtocol registerClass:[RNCachingURLProtocol class]]; - - self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; - self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; - self.window.rootViewController = self.viewController; - [self.window makeKeyAndVisible]; - return YES; + //only cache Steam + [RNCachingURLProtocol setShouldHandleRequest:^BOOL(NSURLRequest * _Nonnull aRequest) { + if(!aRequest.URL.host) { + return NO; + } + + BOOL br = [aRequest.URL.host rangeOfString:@"steamcommunity.com"].location != NSNotFound; + if(br) { + NSLog(@"cache %@", aRequest.URL.host); + } + return br; + }]; + + //set our protocol + [NSURLProtocol registerClass:[RNCachingURLProtocol class]]; + + //prepare the content vcs + ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; + viewController.url = [NSURL URLWithString:@"http://steamcommunity.com"]; + viewController.title = @"cached Steamcommunity"; + ViewController *viewController2 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; + viewController2.url = [NSURL URLWithString:@"http://www.spiegel.de"]; + viewController2.title = @"uncached Spiegel"; + + //add tabbar + self.viewController = [[UITabBarController alloc] init]; + self.viewController.viewControllers = @[viewController, viewController2]; + + //add the window + self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + self.window.rootViewController = self.viewController; + [self.window makeKeyAndVisible]; + return YES; } @end diff --git a/CachedWebView/CachedWebView-Info.plist b/CachedWebView/CachedWebView-Info.plist index 67f5ce1..7c1c228 100644 --- a/CachedWebView/CachedWebView-Info.plist +++ b/CachedWebView/CachedWebView-Info.plist @@ -1,39 +1,44 @@ - - CFBundleDevelopmentRegion - en - CFBundleDisplayName - ${PRODUCT_NAME} - CFBundleExecutable - ${EXECUTABLE_NAME} - CFBundleIdentifier - net.robnapier.${PRODUCT_NAME:rfc1034identifier} - CFBundleInfoDictionaryVersion - 6.0 - CFBundleName - ${PRODUCT_NAME} - CFBundlePackageType - APPL - CFBundleShortVersionString - 1.0 - CFBundleSignature - ???? - CFBundleVersion - 1.0 - LSRequiresIPhoneOS - - UIRequiredDeviceCapabilities - - armv7 - - UISupportedInterfaceOrientations~ipad - - UIInterfaceOrientationPortrait - UIInterfaceOrientationPortraitUpsideDown - UIInterfaceOrientationLandscapeLeft - UIInterfaceOrientationLandscapeRight - - + + CFBundleDevelopmentRegion + en + CFBundleDisplayName + ${PRODUCT_NAME} + CFBundleExecutable + ${EXECUTABLE_NAME} + CFBundleIdentifier + net.robnapier.${PRODUCT_NAME:rfc1034identifier} + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + ${PRODUCT_NAME} + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1.0 + LSRequiresIPhoneOS + + NSAppTransportSecurity + + NSAllowsArbitraryLoads + + + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations~ipad + + UIInterfaceOrientationPortrait + UIInterfaceOrientationPortraitUpsideDown + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + diff --git a/CachedWebView/ViewController.h b/CachedWebView/ViewController.h index 291f305..268b485 100644 --- a/CachedWebView/ViewController.h +++ b/CachedWebView/ViewController.h @@ -29,6 +29,5 @@ #import @interface ViewController : UIViewController -@property (strong, nonatomic) IBOutlet UIWebView *webView; - +@property (strong, nonatomic) NSURL *url; @end diff --git a/CachedWebView/ViewController.m b/CachedWebView/ViewController.m index a018e83..f0a760c 100644 --- a/CachedWebView/ViewController.m +++ b/CachedWebView/ViewController.m @@ -29,16 +29,26 @@ #import "ViewController.h" @interface ViewController () - +@property (strong, nonatomic) IBOutlet UIWebView *webView; @end @implementation ViewController -@synthesize webView = webView_; - (void)viewDidLoad { - [super viewDidLoad]; - [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://cnn.com"]]]; + [super viewDidLoad]; + + id url = self.url; + if(url) { + [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:url]]; + } } +- (void)setUrl:(NSURL *)url { + _url = url; + + if(self.isViewLoaded) { + [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:url]]; + } +} @end diff --git a/CachedWebView/en.lproj/ViewController.xib b/CachedWebView/en.lproj/ViewController.xib index c2a9350..1a3018d 100644 --- a/CachedWebView/en.lproj/ViewController.xib +++ b/CachedWebView/en.lproj/ViewController.xib @@ -1,167 +1,28 @@ - - - - 1280 - 11C74 - 1938 - 1138.23 - 567.00 - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - 933 - - - IBProxyObject - IBUIView - IBUIWebView - - - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - PluginDependencyRecalculationVersion - - - - - IBFilesOwner - IBIPadFramework - - - IBFirstResponder - IBIPadFramework - - - - 274 - - - - 274 - {768, 1004} - - - _NS:693 - - 1 - MSAxIDEAA - - IBIPadFramework - 1 - YES - - - {{0, 20}, {768, 1004}} - - - - - 3 - MQA - - 2 - - - - 2 - - IBIPadFramework - - - - - - - view - - - - 3 - - - - webView - - - - 5 - - - - - - 0 - - - - - - -1 - - - File's Owner - - - -2 - - - - - 2 - - - - - - - - 4 - - - - - - - ViewController - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - UIResponder - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - com.apple.InterfaceBuilder.IBCocoaTouchPlugin - - - - - - 5 - - - - - ViewController - UIViewController - - webView - UIWebView - - - webView - - webView - UIWebView - - - - IBProjectSource - ./Classes/ViewController.h - - - - - 0 - IBIPadFramework - YES - 3 - 933 - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 3e891d4..e5c82be 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,18 @@ RNCachingURLProtocol. For more details see [Drop-in offline caching for UIWebView (and NSURLProtocol)](http://robnapier.net/blog/offline-uiwebview-nsurlprotocol-588). +#Note +Per Default Only HTTP(s) GET Requests are cached offline. + +Set shouldHandleRequest block to change this to either allow only certain hostnames, ports or whatever you like! + +e.g. + + //only cache CNN + [RNCachingURLProtocol setShouldHandleRequest:^BOOL(NSURLRequest * _Nonnull aRequest) { + return [aRequest.URL.host isEqualToString:@"www.cnn.com"]; + }]; + # EXAMPLE See the CachedWebView project for example usage. diff --git a/RNCachingURLProtocol.h b/RNCachingURLProtocol.h index 5c8bd13..0f24a0b 100644 --- a/RNCachingURLProtocol.h +++ b/RNCachingURLProtocol.h @@ -57,15 +57,24 @@ // // For more details see // [Drop-in offline caching for UIWebView (and NSURLProtocol)](http://robnapier.net/blog/offline-uiwebview-nsurlprotocol-588). +// +// Per Default Only HTTP(s) GET Requests are cached offline. +// Set shouldHandleRequest block to change this to either allow only certain hostnames, ports or whatever you like! #import +NS_ASSUME_NONNULL_BEGIN + +typedef BOOL (^RNCachingURLProtocolBlock)(NSURLRequest *aRequest); + @interface RNCachingURLProtocol : NSURLProtocol -+ (NSSet *)supportedSchemes; -+ (void)setSupportedSchemes:(NSSet *)supportedSchemes; ++(RNCachingURLProtocolBlock)shouldHandleRequest; ++ (void)setShouldHandleRequest:(RNCachingURLProtocolBlock)shouldHandleRequest; - (NSString *)cachePathForRequest:(NSURLRequest *)aRequest; - (BOOL) useCache; @end + +NS_ASSUME_NONNULL_END diff --git a/RNCachingURLProtocol.m b/RNCachingURLProtocol.m index c247cf1..25b2b90 100644 --- a/RNCachingURLProtocol.m +++ b/RNCachingURLProtocol.m @@ -28,6 +28,7 @@ #import "RNCachingURLProtocol.h" #import "Reachability.h" #import "NSString+Sha1.h" +#import #define WORKAROUND_MUTABLE_COPY_LEAK 1 @@ -55,161 +56,167 @@ @interface RNCachingURLProtocol () //