Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a WKWebView version to replace UIWebView when running under iOS 8... #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Demo/SVWeb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
28AD73600D9D9599002E5188 /* MainWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28AD735F0D9D9599002E5188 /* MainWindow.xib */; };
28C286E10D94DF7D0034E888 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 28C286E00D94DF7D0034E888 /* ViewController.m */; };
28F335F11007B36200424DE2 /* ViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 28F335F01007B36200424DE2 /* ViewController.xib */; };
79D8AE171A700F4E00CD7521 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79D8AE161A700F4E00CD7521 /* WebKit.framework */; };
D53D8F1316A8688600711E30 /* SVWebViewController.strings in Resources */ = {isa = PBXBuildFile; fileRef = D53D8F1516A8688600711E30 /* SVWebViewController.strings */; };
D5FA63C516A8707A0087531C /* Default-568h@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = D5FA63C416A8707A0087531C /* Default-568h@2x.png */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -69,6 +70,7 @@
431F485D17145A510045AA32 /* fr */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = fr; path = fr.lproj/SVWebViewController.strings; sourceTree = "<group>"; };
627D469418D9EA4300E514BB /* de */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/SVWebViewController.strings; sourceTree = "<group>"; };
6DAD2E4EAB184F72ACE29999 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
79D8AE161A700F4E00CD7521 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
8D1107310486CEB800E47090 /* SVWeb-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "SVWeb-Info.plist"; plistStructureDefinitionIdentifier = "com.apple.xcode.plist.structure-definition.iphone.info-plist"; sourceTree = "<group>"; };
A0667BBC457D4307AC51AC26 /* Pods.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.xcconfig; path = Pods/Pods.xcconfig; sourceTree = "<group>"; };
D53D8F1716A868C900711E30 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/SVWebViewController.strings; sourceTree = "<group>"; };
Expand All @@ -82,6 +84,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
79D8AE171A700F4E00CD7521 /* WebKit.framework in Frameworks */,
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */,
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */,
);
Expand Down Expand Up @@ -206,6 +209,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
79D8AE161A700F4E00CD7521 /* WebKit.framework */,
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */,
1D30AB110D05D00D00671497 /* Foundation.framework */,
6DAD2E4EAB184F72ACE29999 /* libPods.a */,
Expand Down
1 change: 1 addition & 0 deletions SVWebViewController.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ Pod::Spec.new do |s|
s.source_files = 'SVWebViewController/**/*.{h,m}'
s.resources = 'SVWebViewController/**/*.{bundle,png,lproj}'
s.requires_arc = true
s.frameworks = 'WebKit'
end
140 changes: 112 additions & 28 deletions SVWebViewController/SVWebViewController.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
//
// https://github.com/samvermette/SVWebViewController

#import <WebKit/WebKit.h>

#import "SVWebViewControllerActivityChrome.h"
#import "SVWebViewControllerActivitySafari.h"
#import "SVWebViewController.h"

@interface SVWebViewController () <UIWebViewDelegate>
@interface SVWebViewController () <UIWebViewDelegate, WKNavigationDelegate>

@property (nonatomic, strong) UIBarButtonItem *backBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *forwardBarButtonItem;
Expand All @@ -19,6 +21,8 @@ @interface SVWebViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIBarButtonItem *actionBarButtonItem;

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WKWebView* wkWebView; // Will use a WebKit WebView if available (iOS8+)

@property (nonatomic, strong) NSURLRequest *request;

@end
Expand All @@ -28,11 +32,20 @@ @implementation SVWebViewController

#pragma mark - Initialization

- (void)dealloc {
[self.webView stopLoading];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.webView.delegate = nil;
self.delegate = nil;
- (void)dealloc
{
if (self.wkWebView)
{
[self.wkWebView stopLoading];
self.wkWebView = nil;
}
else
{
[self.webView stopLoading];
self.webView.delegate = nil;
self.delegate = nil;
}
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

- (instancetype)initWithAddress:(NSString *)urlString {
Expand All @@ -51,14 +64,23 @@ - (instancetype)initWithURLRequest:(NSURLRequest*)request {
return self;
}

- (void)loadRequest:(NSURLRequest*)request {
[self.webView loadRequest:request];
- (void)loadRequest:(NSURLRequest*)request
{
if (self.wkWebView)
{
[self.wkWebView loadRequest:request];
}
else
{
[self.webView loadRequest:request];
}
}

#pragma mark - View lifecycle

- (void)loadView {
self.view = self.webView;
- (void)loadView
{
self.view = self.wkWebView ?: self.webView;
[self loadRequest:self.request];
}

Expand All @@ -67,9 +89,13 @@ - (void)viewDidLoad {
[self updateToolbarItems];
}

- (void)viewDidUnload {
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;

self.webView = nil;
self.wkWebView = nil;

_backBarButtonItem = nil;
_forwardBarButtonItem = nil;
_refreshBarButtonItem = nil;
Expand Down Expand Up @@ -121,6 +147,20 @@ - (UIWebView*)webView {
return _webView;
}

- (WKWebView*)wkWebView
{
if (!_wkWebView)
{
if ([WKWebView class])
{
_wkWebView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_wkWebView.navigationDelegate = self;
}
}

return _wkWebView;
}

- (UIBarButtonItem *)backBarButtonItem {
if (!_backBarButtonItem) {
_backBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"SVWebViewController.bundle/SVWebViewControllerBack"]
Expand Down Expand Up @@ -166,12 +206,16 @@ - (UIBarButtonItem *)actionBarButtonItem {

#pragma mark - Toolbar

- (void)updateToolbarItems {
self.backBarButtonItem.enabled = self.self.webView.canGoBack;
self.forwardBarButtonItem.enabled = self.self.webView.canGoForward;

UIBarButtonItem *refreshStopBarButtonItem = self.self.webView.isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;

- (void)updateToolbarItems
{
self.backBarButtonItem.enabled = self.wkWebView ? self.wkWebView.canGoBack : self.webView.canGoBack;
self.forwardBarButtonItem.enabled = self.wkWebView ? self.wkWebView.canGoForward : self.webView.canGoForward;

BOOL isLoading = self.wkWebView ? self.wkWebView.isLoading : self.webView.isLoading;
self.actionBarButtonItem.enabled = !isLoading;

UIBarButtonItem *refreshStopBarButtonItem = isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

Expand Down Expand Up @@ -259,27 +303,67 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
return YES;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self updateToolbarItems];
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

self.navigationItem.title = webView.title;
[self updateToolbarItems];
}

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self updateToolbarItems];
}

#pragma mark - Target actions

- (void)goBackTapped:(UIBarButtonItem *)sender {
[self.webView goBack];
- (void)goBackTapped:(UIBarButtonItem *)sender
{
if (self.wkWebView)
[self.wkWebView goBack];
else
[self.webView goBack];
}

- (void)goForwardTapped:(UIBarButtonItem *)sender {
[self.webView goForward];
- (void)goForwardTapped:(UIBarButtonItem *)sender
{
if (self.wkWebView)
[self.wkWebView goForward];
else
[self.webView goForward];
}

- (void)reloadTapped:(UIBarButtonItem *)sender {
[self.webView reload];
- (void)reloadTapped:(UIBarButtonItem *)sender
{
if (self.wkWebView)
[self.wkWebView reload];
else
[self.webView reload];
}

- (void)stopTapped:(UIBarButtonItem *)sender {
[self.webView stopLoading];
- (void)stopTapped:(UIBarButtonItem *)sender
{
if (self.wkWebView)
[self.wkWebView stopLoading];
else
[self.webView stopLoading];

[self updateToolbarItems];
}

- (void)actionButtonTapped:(id)sender {
NSURL *url = self.webView.request.URL ? self.webView.request.URL : self.request.URL;
- (void)actionButtonTapped:(id)sender
{
// Can't see the request URL in the wkWebView and this code will fall down to self.request.URL for iOS8 so should be ok
NSURL *url = self.webView.request.URL ? self.webView.request.URL : self.request.URL;
if (url != nil) {
NSArray *activities = @[[SVWebViewControllerActivitySafari new], [SVWebViewControllerActivityChrome new]];

Expand Down