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

Replacing UIWebView with WKWebView as per ITMS-90809: Deprecated API #170

Open
wants to merge 2 commits 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: 3 additions & 1 deletion Demo/SVWeb-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
<key>CFBundleIconFile</key>
<string></string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>$(MARKETING_VERSION)</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
41 changes: 34 additions & 7 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 */; };
AB41CEB9245A6FC90094FF87 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AB41CEB8245A6FC90094FF87 /* 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 @@ -71,6 +72,7 @@
6DAD2E4EAB184F72ACE29999 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
AB41CEB8245A6FC90094FF87 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
D53D8F1716A868C900711E30 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/SVWebViewController.strings; sourceTree = "<group>"; };
D53D8F1816A8693200711E30 /* es-ES */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = "es-ES"; path = "es-ES.lproj/SVWebViewController.strings"; sourceTree = "<group>"; };
D53D8F1916A8693A00711E30 /* es */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = es; path = es.lproj/SVWebViewController.strings; sourceTree = "<group>"; };
Expand All @@ -84,6 +86,7 @@
files = (
1D60589F0D05DD5A006BFB54 /* Foundation.framework in Frameworks */,
1DF5F4E00D08C38300B7A737 /* UIKit.framework in Frameworks */,
AB41CEB9245A6FC90094FF87 /* WebKit.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -206,6 +209,7 @@
29B97323FDCFA39411CA2CEA /* Frameworks */ = {
isa = PBXGroup;
children = (
AB41CEB8245A6FC90094FF87 /* WebKit.framework */,
1DF5F4DF0D08C38300B7A737 /* UIKit.framework */,
1D30AB110D05D00D00671497 /* Foundation.framework */,
6DAD2E4EAB184F72ACE29999 /* libPods.a */,
Expand Down Expand Up @@ -239,17 +243,13 @@
29B97313FDCFA39411CA2CEA /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0500;
LastUpgradeCheck = 1140;
};
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "SVWeb" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
developmentRegion = en;
hasScannedForEncodings = 1;
knownRegions = (
English,
Japanese,
French,
German,
en,
fr,
de,
Expand All @@ -271,6 +271,7 @@
sv,
vi,
zh_CN,
Base,
);
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
projectDirPath = "";
Expand Down Expand Up @@ -353,6 +354,8 @@
GCC_PREFIX_HEADER = SVWeb_Prefix.pch;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
INFOPLIST_FILE = "SVWeb-Info.plist";
MARKETING_VERSION = 0.3;
PRODUCT_BUNDLE_IDENTIFIER = "com.yourcompany.${PRODUCT_NAME:rfc1034identifier}";
PRODUCT_NAME = SVWeb;
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
Expand All @@ -362,13 +365,37 @@
C01FCF4F08A954540054247B /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_ENABLE_OBJC_ARC = NO;
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_COMMA = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = c99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
Expand Down
1 change: 1 addition & 0 deletions Demo/SVWeb_Prefix.pch
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#endif
1 change: 1 addition & 0 deletions Demo/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

int main(int argc, char *argv[]) {
@autoreleasepool {
Expand Down
4 changes: 2 additions & 2 deletions SVWebViewController/SVModalWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// https://github.com/samvermette/SVWebViewController

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

@class SVWebViewController;
@class SVModalWebViewController;
Expand All @@ -16,7 +16,7 @@ typedef NS_ENUM(NSUInteger, SVWebViewControllerDismissButtonStyle) {
SVWebViewControllerDismissButtonStyleCancel
};

@protocol SVModalWebViewControllerDelegate <UIWebViewDelegate>
@protocol SVModalWebViewControllerDelegate <WKNavigationDelegate>

@optional
- (void)controllerDidPressDoneButton:(SVModalWebViewController *)controller;
Expand Down
4 changes: 2 additions & 2 deletions SVWebViewController/SVModalWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ - (UIBarButtonItem *)barButtonItemForDismissButtonStyle:(SVWebViewControllerDism

#pragma mark - Delegate

- (void)setWebViewDelegate:(id<UIWebViewDelegate>)webViewDelegate {
- (void)setWebViewDelegate:(id<WKNavigationDelegate>)webViewDelegate {
self.webViewController.delegate = webViewDelegate;
}

- (id<UIWebViewDelegate>)webViewDelegate {
- (id<WKNavigationDelegate>)webViewDelegate {
return self.webViewController.delegate;
}

Expand Down
6 changes: 4 additions & 2 deletions SVWebViewController/SVWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
//
// https://github.com/samvermette/SVWebViewController

#import <WebKit/WebKit.h>

@interface SVWebViewController : UIViewController

- (instancetype)initWithAddress:(NSString *)urlString;
- (instancetype)initWithURL:(NSURL *)URL;
- (instancetype)initWithURLRequest:(NSURLRequest *)request;

@property (nonatomic, weak) id<UIWebViewDelegate> delegate;
@property (nonatomic, readonly) UIWebView *webView;
@property (nonatomic, weak) id<WKNavigationDelegate> delegate;
@property (nonatomic, readonly) WKWebView *webView;
@property (nonatomic, strong) NSMutableArray<UIActivity *> *activities;

@end
82 changes: 50 additions & 32 deletions SVWebViewController/SVWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
#import "SVWebViewControllerActivitySafari.h"
#import "SVWebViewController.h"

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

@property (nonatomic, strong) UIBarButtonItem *backBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *forwardBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *refreshBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *stopBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *actionBarButtonItem;

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) WKWebView *webView;
@property (nonatomic, strong) NSURLRequest *request;

@end
Expand All @@ -31,7 +31,7 @@ @implementation SVWebViewController
- (void)dealloc {
[self.webView stopLoading];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.webView.delegate = nil;
self.webView.navigationDelegate = nil;
self.delegate = nil;
}

Expand Down Expand Up @@ -112,11 +112,10 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa

#pragma mark - Getters

- (UIWebView*)webView {
- (WKWebView*)webView {
if(!_webView) {
_webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
_webView = [[WKWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView.navigationDelegate = self;
}
return _webView;
}
Expand Down Expand Up @@ -216,47 +215,66 @@ - (void)updateToolbarItems {
}
}

#pragma mark - UIWebViewDelegate
#pragma mark - WKNavigationDelegate

- (void)webViewDidStartLoad:(UIWebView *)webView {
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
[self updateToolbarItems];
if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
[self.delegate webViewDidStartLoad:webView];

if ([self.delegate respondsToSelector:@selector(webView:didStartProvisionalNavigation:)]) {
[self.delegate webView:webView didStartProvisionalNavigation:navigation];
}
}


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


__weak SVWebViewController *weakSelf = self;
__block void (^finalBlock)(void) = ^{
SVWebViewController *strongSelf = weakSelf;
if (strongSelf) {
[strongSelf updateToolbarItems];

if ([strongSelf.delegate respondsToSelector:@selector(webView:didFinishNavigation:)]) {
[strongSelf.delegate webView:webView didFinishNavigation:navigation];
}
}
};

if (self.navigationItem.title == nil) {
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

[self updateToolbarItems];

if ([self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
[self.delegate webViewDidFinishLoad:webView];
id evalCompletion = ^(id _Nullable result, NSError * _Nullable error) {
SVWebViewController *strongSelf = weakSelf;
if (strongSelf && !error && result) {
strongSelf.navigationItem.title = [NSString stringWithFormat:@"%@", result];
}
finalBlock();
};
[webView evaluateJavaScript:@"document.title"
completionHandler:evalCompletion];
} else {
finalBlock();
}
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
[self updateToolbarItems];
if ([self.delegate respondsToSelector:@selector(webView:didFailLoadWithError:)]) {
[self.delegate webView:webView didFailLoadWithError:error];

if ([self.delegate respondsToSelector:@selector(webView:didFailNavigation:withError:)]) {
[self.delegate webView:webView didFailNavigation:navigation withError:error];
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([self.delegate respondsToSelector:@selector(webView:shouldStartLoadWithRequest:navigationType:)]) {
return [self.delegate webView:webView shouldStartLoadWithRequest:request navigationType:navigationType];
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if ([self.delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:decisionHandler:)]) {
[self.delegate webView:webView decidePolicyForNavigationAction:navigationAction decisionHandler:decisionHandler];
} else {
decisionHandler(WKNavigationActionPolicyAllow);
}

return YES;
}

#pragma mark - Target actions
Expand All @@ -279,7 +297,7 @@ - (void)stopTapped:(UIBarButtonItem *)sender {
}

- (void)actionButtonTapped:(id)sender {
NSURL *url = self.webView.request.URL ? self.webView.request.URL : self.request.URL;
NSURL *url = self.webView.URL ? self.webView.URL : self.request.URL;
if (url != nil) {
if ([[url absoluteString] hasPrefix:@"file:///"]) {
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
//

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>

@interface SVWebViewControllerActivity : UIActivity

Expand Down