diff --git a/ExampleProject/KINWebBrowserExample.xcodeproj/project.pbxproj b/ExampleProject/KINWebBrowserExample.xcodeproj/project.pbxproj index 1d2ff60..6657463 100644 --- a/ExampleProject/KINWebBrowserExample.xcodeproj/project.pbxproj +++ b/ExampleProject/KINWebBrowserExample.xcodeproj/project.pbxproj @@ -230,6 +230,7 @@ developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( + English, en, ); mainGroup = 3BBECF1C18A1E58D00A2499D; @@ -427,6 +428,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "KINWebBrowserExample/KINWebBrowserExample-Prefix.pch"; INFOPLIST_FILE = "KINWebBrowserExample/KINWebBrowserExample-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; @@ -441,6 +443,7 @@ GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "KINWebBrowserExample/KINWebBrowserExample-Prefix.pch"; INFOPLIST_FILE = "KINWebBrowserExample/KINWebBrowserExample-Info.plist"; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; PRODUCT_NAME = "$(TARGET_NAME)"; WRAPPER_EXTENSION = app; }; diff --git a/ExampleProject/KINWebBrowserExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/ExampleProject/KINWebBrowserExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 0000000..18d9810 --- /dev/null +++ b/ExampleProject/KINWebBrowserExample.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/ExampleProject/KINWebBrowserExample/Images.xcassets/AppIcon.appiconset/Contents.json b/ExampleProject/KINWebBrowserExample/Images.xcassets/AppIcon.appiconset/Contents.json index b7f3352..9221b9b 100644 --- a/ExampleProject/KINWebBrowserExample/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/ExampleProject/KINWebBrowserExample/Images.xcassets/AppIcon.appiconset/Contents.json @@ -2,57 +2,97 @@ "images" : [ { "idiom" : "iphone", - "size" : "29x29", - "scale" : "2x" + "scale" : "2x", + "size" : "20x20" }, { "idiom" : "iphone", - "size" : "40x40", - "scale" : "2x" + "scale" : "3x", + "size" : "20x20" }, { "idiom" : "iphone", - "size" : "60x60", - "scale" : "2x" + "scale" : "2x", + "size" : "29x29" }, { "idiom" : "iphone", - "size" : "60x60", - "scale" : "3x" + "scale" : "3x", + "size" : "29x29" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "40x40" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "40x40" + }, + { + "idiom" : "iphone", + "scale" : "2x", + "size" : "60x60" + }, + { + "idiom" : "iphone", + "scale" : "3x", + "size" : "60x60" }, { "idiom" : "ipad", - "size" : "29x29", - "scale" : "1x" + "scale" : "1x", + "size" : "20x20" }, { "idiom" : "ipad", - "size" : "29x29", - "scale" : "2x" + "scale" : "2x", + "size" : "20x20" }, { "idiom" : "ipad", - "size" : "40x40", - "scale" : "1x" + "scale" : "1x", + "size" : "29x29" }, { "idiom" : "ipad", - "size" : "40x40", - "scale" : "2x" + "scale" : "2x", + "size" : "29x29" }, { "idiom" : "ipad", - "size" : "76x76", - "scale" : "1x" + "scale" : "1x", + "size" : "40x40" }, { "idiom" : "ipad", - "size" : "76x76", - "scale" : "2x" + "scale" : "2x", + "size" : "40x40" + }, + { + "idiom" : "ipad", + "scale" : "1x", + "size" : "76x76" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "76x76" + }, + { + "idiom" : "ipad", + "scale" : "2x", + "size" : "83.5x83.5" + }, + { + "idiom" : "ios-marketing", + "scale" : "1x", + "size" : "1024x1024" } ], "info" : { - "version" : 1, - "author" : "xcode" + "author" : "xcode", + "version" : 1 } -} \ No newline at end of file +} diff --git a/ExampleProject/KINWebBrowserExample/KINWebBrowserExampleViewController.m b/ExampleProject/KINWebBrowserExample/KINWebBrowserExampleViewController.m index 910f10d..da67f56 100644 --- a/ExampleProject/KINWebBrowserExample/KINWebBrowserExampleViewController.m +++ b/ExampleProject/KINWebBrowserExample/KINWebBrowserExampleViewController.m @@ -38,7 +38,7 @@ @interface KINWebBrowserExampleViewController () @end -static NSString *const defaultAddress = @"http://www.apple.com/"; +static NSString *const defaultAddress = @"https://www.apple.com"; @implementation KINWebBrowserExampleViewController @@ -87,6 +87,11 @@ - (void)webBrowser:(KINWebBrowserViewController *)webBrowser didFailToLoadURL:(N NSLog(@"Failed To Load URL : %@ With Error: %@", URL, error); } +- (void)webBrowserViewControllerWillDismiss:(KINWebBrowserViewController*)viewController { + NSLog(@"View Controller will dismiss: %@", viewController); + +} + #pragma mark - IBActions diff --git a/ExampleProject/Podfile b/ExampleProject/Podfile index 8b6339e..ce76fad 100644 --- a/ExampleProject/Podfile +++ b/ExampleProject/Podfile @@ -1,6 +1,7 @@ source 'https://github.com/CocoaPods/Specs.git' xcodeproj 'KINWebBrowserExample.xcodeproj' -platform :ios, :deployment_target => '7.0' +platform :ios, :deployment_target => '8.0' + +pod 'KINWebBrowser' -pod 'KINWebBrowser', '~> 1.1' diff --git a/KINWebBrowser.podspec b/KINWebBrowser.podspec index 55ad292..745c86d 100644 --- a/KINWebBrowser.podspec +++ b/KINWebBrowser.podspec @@ -2,21 +2,24 @@ Pod::Spec.new do |s| s.name = "KINWebBrowser" - - s.version = "1.1.0" + s.version = "1.3.3" s.summary = "A web browser module for your apps." s.description = <<-DESC - KINWebBrowser is a web browser module for your apps. Powered by WKWebView on iOS 8. Backwards compatible with iOS 7 using UIWebView. KINWebBrowser offers the simplest way to add a web browser to your apps. + KINWebBrowser is a web browser module for your apps. Powered by WKWebView on iOS 8. KINWebBrowser offers the simplest way to add a web browser to your apps. DESC s.homepage = "https://github.com/dfmuir/KINWebBrowser" s.license = { :type => 'MIT', :file => 'LICENSE' } s.author = { "David F. Muir V" => "dfmuir@gmail.com" } - s.platform = :ios, '7.0' + s.platform = :ios, '8.0' s.source = { :git => "https://github.com/dfmuir/KINWebBrowser.git", :tag => s.version.to_s } s.source_files = 'KINWebBrowser', 'KINWebBrowser/**/*.{h,m}' s.resources = "Assets/*.png" s.requires_arc = true - s.frameworks = 'WebKit' -end \ No newline at end of file + s.weak_framework = 'WebKit' + + s.dependency 'TUSafariActivity', '1.0.4' + s.dependency 'ARChromeActivity', '1.0.5' + +end diff --git a/KINWebBrowser/KINWebBrowserViewController.h b/KINWebBrowser/KINWebBrowserViewController.h index af1c9a4..d9595fc 100644 --- a/KINWebBrowser/KINWebBrowserViewController.h +++ b/KINWebBrowser/KINWebBrowserViewController.h @@ -36,9 +36,9 @@ @class KINWebBrowserViewController; /* - + UINavigationController+KINWebBrowserWrapper category enables access to casted KINWebBroswerViewController when set as rootViewController of UINavigationController - + */ @interface UINavigationController(KINWebBrowser) @@ -54,18 +54,17 @@ - (void)webBrowser:(KINWebBrowserViewController *)webBrowser didStartLoadingURL:(NSURL *)URL; - (void)webBrowser:(KINWebBrowserViewController *)webBrowser didFinishLoadingURL:(NSURL *)URL; - (void)webBrowser:(KINWebBrowserViewController *)webBrowser didFailToLoadURL:(NSURL *)URL error:(NSError *)error; -- (BOOL)webBrowser:(KINWebBrowserViewController *)webBrowser shouldStartLoadWithRequest:(NSURLRequest *)request; -- (void)willDismissWebBrowser:(KINWebBrowserViewController *)webBrowser; +- (void)webBrowserViewControllerWillDismiss:(KINWebBrowserViewController*)viewController; @end /* - + KINWebBrowserViewController is designed to be used inside of a UINavigationController. For convenience, two sets of static initializers are available. - + */ -@interface KINWebBrowserViewController : UIViewController +@interface KINWebBrowserViewController : UIViewController #pragma mark - Public Properties @@ -77,52 +76,63 @@ // The web views // Depending on the version of iOS, one of these will be set @property (nonatomic, strong) WKWebView *wkWebView; -@property (nonatomic, strong) UIWebView *uiWebView; -- (id)initWithConfiguration:(WKWebViewConfiguration *)configuration NS_AVAILABLE_IOS(8_0); +- (id)initWithConfiguration:(WKWebViewConfiguration *)configuration; #pragma mark - Static Initializers /* Initialize a basic KINWebBrowserViewController instance for push onto navigation stack - + Ideal for use with UINavigationController pushViewController:animated: or initWithRootViewController: - + Optionally specify KINWebBrowser options or WKWebConfiguration */ + (KINWebBrowserViewController *)webBrowser; -+ (KINWebBrowserViewController *)webBrowserWithConfiguration:(WKWebViewConfiguration *)configuration NS_AVAILABLE_IOS(8_0); ++ (KINWebBrowserViewController *)webBrowserWithConfiguration:(WKWebViewConfiguration *)configuration; /* Initialize a UINavigationController with a KINWebBrowserViewController for modal presentation. - + Ideal for use with presentViewController:animated: - + Optionally specify KINWebBrowser options or WKWebConfiguration */ + (UINavigationController *)navigationControllerWithWebBrowser; -+ (UINavigationController *)navigationControllerWithWebBrowserWithConfiguration:(WKWebViewConfiguration *)configuration NS_AVAILABLE_IOS(8_0); - ++ (UINavigationController *)navigationControllerWithWebBrowserWithConfiguration:(WKWebViewConfiguration *)configuration; +@property (nonatomic, strong) UIBarButtonItem *actionButton; @property (nonatomic, strong) UIColor *tintColor; @property (nonatomic, strong) UIColor *barTintColor; @property (nonatomic, assign) BOOL actionButtonHidden; @property (nonatomic, assign) BOOL showsURLInNavigationBar; @property (nonatomic, assign) BOOL showsPageTitleInNavigationBar; +//Allow for custom activities in the browser by populating this optional array +@property (nonatomic, strong) NSArray *customActivityItems; + #pragma mark - Public Interface -// Load a NSURL to webView + +// Load a NSURLURLRequest to web view +// Can be called any time after initialization +- (void)loadRequest:(NSURLRequest *)request; + +// Load a NSURL to web view // Can be called any time after initialization - (void)loadURL:(NSURL *)URL; -// Loads a URL as NSString to webView +// Loads a URL as NSString to web view // Can be called any time after initialization - (void)loadURLString:(NSString *)URLString; - (void)doneButtonPressed:(id)sender; -@end +// Loads an string containing HTML to web view +// Can be called any time after initialization +- (void)loadHTMLString:(NSString *)HTMLString; + +@end diff --git a/KINWebBrowser/KINWebBrowserViewController.m b/KINWebBrowser/KINWebBrowserViewController.m index 336f3ce..7fa50fd 100644 --- a/KINWebBrowser/KINWebBrowserViewController.m +++ b/KINWebBrowser/KINWebBrowserViewController.m @@ -32,16 +32,18 @@ #import "KINWebBrowserViewController.h" -static void *KINContext = &KINContext; +#import "TUSafariActivity.h" +#import "ARChromeActivity.h" -@interface KINWebBrowserViewController () +static void *KINWebBrowserContext = &KINWebBrowserContext; + +@interface KINWebBrowserViewController () @property (nonatomic, assign) BOOL previousNavigationControllerToolbarHidden, previousNavigationControllerNavigationBarHidden; -@property (nonatomic, strong) UIBarButtonItem *backButton, *forwardButton, *refreshButton, *stopButton, *actionButton, *fixedSeparator, *flexibleSeparator; -@property (nonatomic, strong) NSTimer *fakeProgressTimer; +@property (nonatomic, strong) UIBarButtonItem *backButton, *forwardButton, *refreshButton, *stopButton, *fixedSeparator, *flexibleSeparator; @property (nonatomic, strong) UIPopoverController *actionPopoverController; -@property (nonatomic, assign) BOOL uiWebViewIsLoading; -@property (nonatomic, strong) NSURL *uiWebViewCurrentURL; +@property (nonatomic, strong) NSURL *URLToLaunchWithPermission; +@property (nonatomic, strong) UIAlertView *externalAppPermissionAlertView; @end @@ -55,24 +57,24 @@ + (KINWebBrowserViewController *)webBrowser { } + (KINWebBrowserViewController *)webBrowserWithConfiguration:(WKWebViewConfiguration *)configuration { - KINWebBrowserViewController *webBrowserViewController = [[KINWebBrowserViewController alloc] initWithConfiguration:configuration]; + KINWebBrowserViewController *webBrowserViewController = [[self alloc] initWithConfiguration:configuration]; return webBrowserViewController; } + (UINavigationController *)navigationControllerWithWebBrowser { - KINWebBrowserViewController *webBrowserViewController = [[KINWebBrowserViewController alloc] initWithConfiguration:nil]; + KINWebBrowserViewController *webBrowserViewController = [[self alloc] initWithConfiguration:nil]; return [KINWebBrowserViewController navigationControllerWithBrowser:webBrowserViewController]; } + (UINavigationController *)navigationControllerWithWebBrowserWithConfiguration:(WKWebViewConfiguration *)configuration { - KINWebBrowserViewController *webBrowserViewController = [[KINWebBrowserViewController alloc] initWithConfiguration:configuration]; + KINWebBrowserViewController *webBrowserViewController = [[self alloc] initWithConfiguration:configuration]; return [KINWebBrowserViewController navigationControllerWithBrowser:webBrowserViewController]; } + (UINavigationController *)navigationControllerWithBrowser:(KINWebBrowserViewController *)webBrowser { UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:webBrowser action:@selector(doneButtonPressed:)]; [webBrowser.navigationItem setRightBarButtonItem:doneButton]; - + UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:webBrowser]; return navigationController; } @@ -86,23 +88,20 @@ - (id)init { - (id)initWithConfiguration:(WKWebViewConfiguration *)configuration { self = [super init]; if(self) { - - if([WKWebView class]) { - if(configuration) { - self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; - } - else { - self.wkWebView = [[WKWebView alloc] init]; - } + if(configuration) { + self.wkWebView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; } else { - self.uiWebView = [[UIWebView alloc] init]; + self.wkWebView = [[WKWebView alloc] init]; } + self.actionButtonHidden = NO; self.showsURLInNavigationBar = NO; self.showsPageTitleInNavigationBar = YES; - + + self.externalAppPermissionAlertView = [[UIAlertView alloc] initWithTitle:@"Leave this app?" message:@"This web page is trying to open an outside app. Are you sure you want to open it?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open App", nil]; + } return self; } @@ -111,34 +110,22 @@ - (id)initWithConfiguration:(WKWebViewConfiguration *)configuration { - (void)viewDidLoad { [super viewDidLoad]; - + self.previousNavigationControllerToolbarHidden = self.navigationController.toolbarHidden; self.previousNavigationControllerNavigationBarHidden = self.navigationController.navigationBarHidden; - - if(self.wkWebView) { - [self.wkWebView setFrame:self.view.bounds]; - [self.wkWebView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [self.wkWebView setNavigationDelegate:self]; - [self.wkWebView setMultipleTouchEnabled:YES]; - [self.wkWebView setAutoresizesSubviews:YES]; - [self.wkWebView.scrollView setAlwaysBounceVertical:YES]; - [self.wkWebView setUIDelegate:self]; - [self.view addSubview:self.wkWebView]; - - [self.wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:KINContext]; - } - else if(self.uiWebView) { - [self.uiWebView setFrame:self.view.bounds]; - [self.uiWebView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; - [self.uiWebView setDelegate:self]; - [self.uiWebView setMultipleTouchEnabled:YES]; - [self.uiWebView setAutoresizesSubviews:YES]; - [self.uiWebView setScalesPageToFit:YES]; - [self.uiWebView.scrollView setAlwaysBounceVertical:YES]; - [self.view addSubview:self.uiWebView]; - } - - + + [self.wkWebView setFrame:self.view.bounds]; + [self.wkWebView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight]; + [self.wkWebView setNavigationDelegate:self]; + [self.wkWebView setUIDelegate:self]; + [self.wkWebView setMultipleTouchEnabled:YES]; + [self.wkWebView setAutoresizesSubviews:YES]; + [self.wkWebView.scrollView setAlwaysBounceVertical:YES]; + [self.view addSubview:self.wkWebView]; + + [self.wkWebView addObserver:self forKeyPath:NSStringFromSelector(@selector(estimatedProgress)) options:0 context:KINWebBrowserContext]; + + self.progressView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleDefault]; [self.progressView setTrackTintColor:[UIColor colorWithWhite:1.0f alpha:0.0f]]; [self.progressView setFrame:CGRectMake(0, self.navigationController.navigationBar.frame.size.height-self.progressView.frame.size.height, self.view.frame.size.width, self.progressView.frame.size.height)]; @@ -147,10 +134,10 @@ - (void)viewDidLoad { - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - + [self.navigationController setNavigationBarHidden:NO animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; - + [self.navigationController.navigationBar addSubview:self.progressView]; [self updateToolbarState]; @@ -163,19 +150,17 @@ - (void)viewWillDisappear:(BOOL)animated { [self.navigationController setToolbarHidden:self.previousNavigationControllerToolbarHidden animated:animated]; - [self.uiWebView setDelegate:nil]; [self.progressView removeFromSuperview]; } #pragma mark - Public Interface +- (void)loadRequest:(NSURLRequest *)request { + [self.wkWebView loadRequest:request]; +} + - (void)loadURL:(NSURL *)URL { - if(self.wkWebView) { - [self.wkWebView loadRequest:[NSURLRequest requestWithURL:URL]]; - } - else if(self.uiWebView) { - [self.uiWebView loadRequest:[NSURLRequest requestWithURL:URL]]; - } + [self loadRequest:[NSURLRequest requestWithURL:URL]]; } - (void)loadURLString:(NSString *)URLString { @@ -183,6 +168,10 @@ - (void)loadURLString:(NSString *)URLString { [self loadURL:URL]; } +- (void)loadHTMLString:(NSString *)HTMLString { + [self.wkWebView loadHTMLString:HTMLString baseURL:nil]; +} + - (void)setTintColor:(UIColor *)tintColor { _tintColor = tintColor; [self.progressView setTintColor:tintColor]; @@ -201,81 +190,8 @@ - (void)setActionButtonHidden:(BOOL)actionButtonHidden { [self updateToolbarState]; } - -#pragma mark - UIWebViewDelegate - -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { - BOOL *shouldLoad = YES; - if([self.delegate respondsToSelector:@selector(webBrowser:shouldStartLoadWithRequest:)]) { - shouldLoad = [self.delegate webBrowser:self shouldStartLoadWithRequest:request]; - } - if(webView == self.uiWebView) { - if (shouldLoad) { - self.uiWebViewCurrentURL = request.URL; - self.uiWebViewIsLoading = YES; - [self updateToolbarState]; - - [self fakeProgressViewStartLoading]; - - if([self.delegate respondsToSelector:@selector(webBrowser:didStartLoadingURL:)]) { - [self.delegate webBrowser:self didStartLoadingURL:request.URL]; - } - } - return shouldLoad; - } - return shouldLoad; -} - -- (void)webViewDidFinishLoad:(UIWebView *)webView { - if(webView == self.uiWebView) { - if(!self.uiWebView.isLoading) { - self.uiWebViewIsLoading = NO; - [self updateToolbarState]; - - [self fakeProgressBarStopLoading]; - } - - if([self.delegate respondsToSelector:@selector(webBrowser:didFinishLoadingURL:)]) { - [self.delegate webBrowser:self didFinishLoadingURL:self.uiWebView.request.URL]; - } - } -} - -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { - if(webView == self.uiWebView) { - if(!self.uiWebView.isLoading) { - self.uiWebViewIsLoading = NO; - [self updateToolbarState]; - - [self fakeProgressBarStopLoading]; - } - if([self.delegate respondsToSelector:@selector(webBrowser:didFailToLoadURL:error:)]) { - [self.delegate webBrowser:self didFailToLoadURL:self.uiWebView.request.URL error:error]; - } - } -} - -#pragma mark - WKUIDelegate - -- (WKWebView*) webView:(WKWebView*)webView createWebViewWithConfiguration:(WKWebViewConfiguration*) configuration forNavigationAction:(WKNavigationAction*) navigationAction windowFeatures:(WKWindowFeatures*) windowFeatures { - if (!navigationAction.targetFrame.isMainFrame) { - [webView loadRequest:navigationAction.request]; - return nil; - } - return webView; - -} - #pragma mark - WKNavigationDelegate -- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { - NSURLRequest *request = navigationAction.request; - BOOL shouldLoad = YES; - if([self.delegate respondsToSelector:@selector(webBrowser:shouldStartLoadWithRequest:)]) { - shouldLoad = [self.delegate webBrowser:self shouldStartLoadWithRequest:request]; - } - decisionHandler(shouldLoad == YES ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel); -} - (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation { if(webView == self.wkWebView) { @@ -315,11 +231,51 @@ - (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigatio } } +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + if(webView == self.wkWebView) { + + NSURL *URL = navigationAction.request.URL; + if(![self externalAppRequiredToOpenURL:URL]) { + if(!navigationAction.targetFrame) { + [self loadURL:URL]; + decisionHandler(WKNavigationActionPolicyCancel); + return; + } + } + else if([[UIApplication sharedApplication] canOpenURL:URL]) { + [self launchExternalAppWithURL:URL]; + decisionHandler(WKNavigationActionPolicyCancel); + return; + } + } + decisionHandler(WKNavigationActionPolicyAllow); +} + + +// - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { +// NSURLRequest *request = navigationAction.request; +// BOOL shouldLoad = YES; +// if([self.delegate respondsToSelector:@selector(webBrowser:shouldStartLoadWithRequest:)]) { +// shouldLoad = [self.delegate webBrowser:self shouldStartLoadWithRequest:request]; +// } +// decisionHandler(shouldLoad == YES ? WKNavigationActionPolicyAllow : WKNavigationActionPolicyCancel); +// } + +#pragma mark - WKUIDelegate + +- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures{ + if (!navigationAction.targetFrame.isMainFrame) { + [webView loadRequest:navigationAction.request]; + } + return nil; +} + #pragma mark - Toolbar State - (void)updateToolbarState { - BOOL canGoBack = self.wkWebView.canGoBack || self.uiWebView.canGoBack; - BOOL canGoForward = self.wkWebView.canGoForward || self.uiWebView.canGoForward; + + BOOL canGoBack = self.wkWebView.canGoBack; + BOOL canGoForward = self.wkWebView.canGoForward; [self.backButton setEnabled:canGoBack]; [self.forwardButton setEnabled:canGoForward]; @@ -329,17 +285,11 @@ - (void)updateToolbarState { } NSArray *barButtonItems; - if(self.wkWebView.loading || self.uiWebViewIsLoading) { + if(self.wkWebView.loading) { barButtonItems = @[self.backButton, self.fixedSeparator, self.forwardButton, self.fixedSeparator, self.stopButton, self.flexibleSeparator]; if(self.showsURLInNavigationBar) { - NSString *URLString; - if(self.wkWebView) { - URLString = [self.wkWebView.URL absoluteString]; - } - else if(self.uiWebView) { - URLString = [self.uiWebViewCurrentURL absoluteString]; - } + NSString *URLString = [self.wkWebView.URL absoluteString]; URLString = [URLString stringByReplacingOccurrencesOfString:@"http://" withString:@""]; URLString = [URLString stringByReplacingOccurrencesOfString:@"https://" withString:@""]; @@ -351,12 +301,7 @@ - (void)updateToolbarState { barButtonItems = @[self.backButton, self.fixedSeparator, self.forwardButton, self.fixedSeparator, self.refreshButton, self.flexibleSeparator]; if(self.showsPageTitleInNavigationBar) { - if(self.wkWebView) { - self.navigationItem.title = self.wkWebView.title; - } - else if(self.uiWebView) { - self.navigationItem.title = [self.uiWebView stringByEvaluatingJavaScriptFromString:@"document.title"]; - } + self.navigationItem.title = self.wkWebView.title; } } @@ -367,13 +312,24 @@ - (void)updateToolbarState { } [self setToolbarItems:barButtonItems animated:YES]; + + self.tintColor = self.tintColor; + self.barTintColor = self.barTintColor; + + } - (void)setupToolbarItems { + NSBundle *bundle = [NSBundle bundleForClass:[KINWebBrowserViewController class]]; + self.refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshButtonPressed:)]; self.stopButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(stopButtonPressed:)]; - self.backButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backbutton"] style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed:)]; - self.forwardButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forwardbutton"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardButtonPressed:)]; + + UIImage *backbuttonImage = [UIImage imageWithContentsOfFile: [bundle pathForResource:@"backbutton" ofType:@"png"]]; + self.backButton = [[UIBarButtonItem alloc] initWithImage:backbuttonImage style:UIBarButtonItemStylePlain target:self action:@selector(backButtonPressed:)]; + + UIImage *forwardbuttonImage = [UIImage imageWithContentsOfFile: [bundle pathForResource:@"forwardbutton" ofType:@"png"]]; + self.forwardButton = [[UIBarButtonItem alloc] initWithImage:forwardbuttonImage style:UIBarButtonItemStylePlain target:self action:@selector(forwardButtonPressed:)]; self.actionButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionButtonPressed:)]; self.fixedSeparator = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil]; self.fixedSeparator.width = 50.0f; @@ -381,80 +337,70 @@ - (void)setupToolbarItems { } #pragma mark - Done Button Action +// - (void)doneButtonPressed:(id)sender { +// if([self.delegate respondsToSelector:@selector(willDismissWebBrowser:)]) { +// [self.delegate willDismissWebBrowser:self]; +// } +// [self.navigationController dismissViewControllerAnimated:YES completion:nil]; +// } - (void)doneButtonPressed:(id)sender { - if([self.delegate respondsToSelector:@selector(willDismissWebBrowser:)]) { - [self.delegate willDismissWebBrowser:self]; - } - [self.navigationController dismissViewControllerAnimated:YES completion:nil]; + [self dismissAnimated:YES]; } #pragma mark - UIBarButtonItem Target Action Methods - (void)backButtonPressed:(id)sender { - - if(self.wkWebView) { - [self.wkWebView goBack]; - } - else if(self.uiWebView) { - [self.uiWebView goBack]; - } + [self.wkWebView goBack]; [self updateToolbarState]; } - (void)forwardButtonPressed:(id)sender { - if(self.wkWebView) { - [self.wkWebView goForward]; - } - else if(self.uiWebView) { - [self.uiWebView goForward]; - } + [self.wkWebView goForward]; [self updateToolbarState]; } - (void)refreshButtonPressed:(id)sender { - if(self.wkWebView) { - [self.wkWebView stopLoading]; - [self.wkWebView reload]; - } - else if(self.uiWebView) { - [self.uiWebView stopLoading]; - [self.uiWebView reload]; - } + [self.wkWebView stopLoading]; + [self.wkWebView reload]; } - (void)stopButtonPressed:(id)sender { - if(self.wkWebView) { - [self.wkWebView stopLoading]; - } - else if(self.uiWebView) { - [self.uiWebView stopLoading]; - } + [self.wkWebView stopLoading]; } - (void)actionButtonPressed:(id)sender { NSURL *URLForActivityItem; - if(self.wkWebView) { - URLForActivityItem = self.wkWebView.URL; - } - else if(self.uiWebView) { - URLForActivityItem = self.uiWebView.request.URL; - } - if(URLForActivityItem == nil) { return; } - dispatch_async(dispatch_get_main_queue(), ^{ - UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[URLForActivityItem] applicationActivities:@[]]; - if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { - if(self.actionPopoverController) { - [self.actionPopoverController dismissPopoverAnimated:YES]; + NSString *URLTitle; + URLForActivityItem = self.wkWebView.URL; + URLTitle = self.wkWebView.title; + if (URLForActivityItem) { + dispatch_async(dispatch_get_main_queue(), ^{ + TUSafariActivity *safariActivity = [[TUSafariActivity alloc] init]; + ARChromeActivity *chromeActivity = [[ARChromeActivity alloc] init]; + + NSMutableArray *activities = [[NSMutableArray alloc] init]; + [activities addObject:safariActivity]; + [activities addObject:chromeActivity]; + if(self.customActivityItems != nil) { + [activities addObjectsFromArray:self.customActivityItems]; } - self.actionPopoverController = [[UIPopoverController alloc] initWithContentViewController:controller]; - [self.actionPopoverController presentPopoverFromBarButtonItem:self.actionButton permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES]; - } - else { - [self presentViewController:controller animated:YES completion:NULL]; - } - }); + + UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[URLForActivityItem] applicationActivities:activities]; + + if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) { + if(self.actionPopoverController) { + [self.actionPopoverController dismissPopoverAnimated:YES]; + } + self.actionPopoverController = [[UIPopoverController alloc] initWithContentViewController:controller]; + [self.actionPopoverController presentPopoverFromBarButtonItem:self.actionButton permittedArrowDirections: UIPopoverArrowDirectionAny animated:YES]; + } + else { + [self presentViewController:controller animated:YES completion:NULL]; + } + }); + } } @@ -480,46 +426,44 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N } } +#pragma mark - External App Support -#pragma mark - Fake Progress Bar Control (UIWebView) - -- (void)fakeProgressViewStartLoading { - [self.progressView setProgress:0.0f animated:NO]; - [self.progressView setAlpha:1.0f]; +- (BOOL)externalAppRequiredToOpenURL:(NSURL *)URL { + NSSet *validSchemes = [NSSet setWithArray:@[@"http", @"https"]]; + return ![validSchemes containsObject:URL.scheme]; +} - if(!self.fakeProgressTimer) { - self.fakeProgressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f/60.0f target:self selector:@selector(fakeProgressTimerDidFire:) userInfo:nil repeats:YES]; +- (void)launchExternalAppWithURL:(NSURL *)URL { + self.URLToLaunchWithPermission = URL; + if (![self.externalAppPermissionAlertView isVisible]) { + [self.externalAppPermissionAlertView show]; } + } -- (void)fakeProgressBarStopLoading { - if(self.fakeProgressTimer) { - [self.fakeProgressTimer invalidate]; - } +#pragma mark - UIAlertViewDelegate - if(self.progressView) { - [self.progressView setProgress:1.0f animated:YES]; - [UIView animateWithDuration:0.3f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{ - [self.progressView setAlpha:0.0f]; - } completion:^(BOOL finished) { - [self.progressView setProgress:0.0f animated:NO]; - }]; +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { + if(alertView == self.externalAppPermissionAlertView) { + if(buttonIndex != alertView.cancelButtonIndex) { + [[UIApplication sharedApplication] openURL:self.URLToLaunchWithPermission]; + } + self.URLToLaunchWithPermission = nil; } } -- (void)fakeProgressTimerDidFire:(id)sender { - CGFloat increment = 0.005/(self.progressView.progress + 0.2); - if([self.uiWebView isLoading]) { - CGFloat progress = (self.progressView.progress < 0.75f) ? self.progressView.progress + increment : self.progressView.progress + 0.0005; - if(self.progressView.progress < 0.95) { - [self.progressView setProgress:progress animated:YES]; - } +#pragma mark - Dismiss + +- (void)dismissAnimated:(BOOL)animated { + if([self.delegate respondsToSelector:@selector(webBrowserViewControllerWillDismiss:)]) { + [self.delegate webBrowserViewControllerWillDismiss:self]; } + [self.navigationController dismissViewControllerAnimated:animated completion:nil]; } #pragma mark - Interface Orientation -- (NSUInteger)supportedInterfaceOrientations { +- (UIInterfaceOrientationMask)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } @@ -530,8 +474,6 @@ - (BOOL)shouldAutorotate { #pragma mark - Dealloc - (void)dealloc { - [self.uiWebView setDelegate:nil]; - [self.wkWebView setNavigationDelegate:nil]; [self.wkWebView setUIDelegate:nil]; if ([self isViewLoaded]) { diff --git a/README.md b/README.md index a1cf578..d12eef2 100644 --- a/README.md +++ b/README.md @@ -3,13 +3,13 @@ KINWebBrowser KINWebBrowser is a web browser module for your apps. -Powered by [WKWebView](https://developer.apple.com/library/IOs/documentation/WebKit/Reference/WKWebView_Ref/index.html) on iOS 8. Backwards compatible with iOS 7 using [UIWebView](https://developer.apple.com/library/ios/documentation/Uikit/reference/UIWebView_Class/index.html). +Powered by [WKWebView](https://developer.apple.com/library/IOs/documentation/WebKit/Reference/WKWebView_Ref/index.html) on iOS 8. ![KINWebBrowser Screenshots](http://i.imgur.com/z1jkWKG.png) Features ------------------------ -* iOS 7 & 8 support for iPhone and iPad devices +* iOS 8 support for iPhone and iPad devices * Safari-like interface * Animated progress bar * Customizable UI including tint color @@ -47,13 +47,13 @@ KINWebBrowserViewController *webBrowser = [webBrowserNavigationController rootWe Installation ------------------------ -#### Cocoapods +#### CocoaPods [CocoaPods](http://cocoapods.org) is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the ["Getting Started" for more information](http://guides.cocoapods.org/using/getting-started.html). ###### Podfile ```ruby -platform :ios, '7.0' +platform :ios, '8.0' pod 'KINWebBrowser' ```