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

UIWebview -> WKWebView ( Tested ) #334

Merged
merged 9 commits into from
Mar 18, 2020
Merged
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
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ branches:
- master
language: objective-c
os: osx
osx_image: xcode9.2
osx_image: xcode10
cache:
- cocoapods
env:
Expand All @@ -16,9 +16,9 @@ env:
before_install:
- |
if [ "$TEST_TYPE" = iOS ] || [ "$TEST_TYPE" = macOS ] || [ "$TEST_TYPE" = tvOS ]; then
gem install xcpretty -N --no-ri --no-rdoc
gem install xcpretty -N --no-document
elif [ "$TEST_TYPE" = CocoaPods ]; then
gem install cocoapods --pre --quiet --no-ri --no-rdoc
gem install cocoapods --pre --no-document
elif [ "$TEST_TYPE" = Carthage ]; then
brew update
brew install carthage || brew upgrade carthage
Expand Down
15 changes: 15 additions & 0 deletions Bolts.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
1EC3017118CDAA8400D06D07 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC3017018CDAA8400D06D07 /* AppDelegate.m */; };
1EC3017318CDAA8400D06D07 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1EC3017218CDAA8400D06D07 /* Images.xcassets */; };
1EC3019118CDABCE00D06D07 /* AppLinkTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1EC3019018CDABCE00D06D07 /* AppLinkTests.m */; };
46A4AE4424094C690022BB98 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46A4AE4324094C690022BB98 /* WebKit.framework */; };
46A4AE4524094DD70022BB98 /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46A4AE4324094C690022BB98 /* WebKit.framework */; };
46D245272409796A00AB92BE /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 46A4AE4324094C690022BB98 /* WebKit.framework */; };
7C60AEC61ACF19F900747DD7 /* CancellationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC51ACF19F900747DD7 /* CancellationTests.m */; };
7C60AEC71ACF19FD00747DD7 /* CancellationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 7C60AEC51ACF19F900747DD7 /* CancellationTests.m */; };
7C60AEC81ACF1A0100747DD7 /* BFCancellationToken.h in Headers */ = {isa = PBXBuildFile; fileRef = 7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -228,6 +231,7 @@
1EC3017018CDAA8400D06D07 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
1EC3017218CDAA8400D06D07 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = "<group>"; };
1EC3019018CDABCE00D06D07 /* AppLinkTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppLinkTests.m; sourceTree = "<group>"; };
46A4AE4324094C690022BB98 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
7C60AEBD1ACF08F300747DD7 /* BFCancellationToken.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFCancellationToken.h; sourceTree = "<group>"; };
7C60AEBE1ACF08F300747DD7 /* BFCancellationToken.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BFCancellationToken.m; sourceTree = "<group>"; };
7C60AEC11ACF093D00747DD7 /* BFCancellationTokenSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BFCancellationTokenSource.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -317,6 +321,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
46D245272409796A00AB92BE /* WebKit.framework in Frameworks */,
1D5D7DB81BE3CE8200FD67C7 /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -359,6 +364,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
46A4AE4424094C690022BB98 /* WebKit.framework in Frameworks */,
81ED941B1BE147CF00795F05 /* Foundation.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -367,6 +373,7 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
46A4AE4524094DD70022BB98 /* WebKit.framework in Frameworks */,
818ADC6C1BE1A80F00C8006C /* Bolts.framework in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -606,6 +613,7 @@
8E9C3CEB17DE9DE000427E62 /* Frameworks */ = {
isa = PBXGroup;
children = (
46A4AE4324094C690022BB98 /* WebKit.framework */,
81D0EE7C19AFA8260000AE75 /* UIKit.framework */,
8E9C3CEC17DE9DE000427E62 /* Foundation.framework */,
8E9C3CFB17DE9DE000427E62 /* SenTestingKit.framework */,
Expand Down Expand Up @@ -982,6 +990,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 8E9C3CE017DE9DE000427E62;
Expand Down Expand Up @@ -1374,13 +1383,19 @@
isa = XCBuildConfiguration;
baseConfigurationReference = 81D646EA1D2CA0E000690609 /* BoltsTests-iOS.xcconfig */;
buildSettings = {
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
VALID_ARCHS = "arm64 arm64e armv7 armv7s";
};
name = Debug;
};
8E8C8EFA17F23D1D00E3F1C7 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 81D646EA1D2CA0E000690609 /* BoltsTests-iOS.xcconfig */;
buildSettings = {
SDKROOT = iphoneos;
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
VALID_ARCHS = "arm64 arm64e armv7 armv7s";
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
22 changes: 9 additions & 13 deletions Bolts.xcodeproj/xcshareddata/xcschemes/Bolts-iOS.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
codeCoverageEnabled = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81ED94111BE147CF00795F05"
BuildableName = "Bolts.framework"
BlueprintName = "Bolts-iOS"
ReferencedContainer = "container:Bolts.xcodeproj">
</BuildableReference>
</MacroExpansion>
<Testables>
<TestableReference
skipped = "NO">
Expand All @@ -54,17 +63,6 @@
</BuildableReference>
</TestableReference>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "81ED94111BE147CF00795F05"
BuildableName = "Bolts.framework"
BlueprintName = "Bolts-iOS"
ReferencedContainer = "container:Bolts.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
Expand All @@ -85,8 +83,6 @@
ReferencedContainer = "container:Bolts.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
3 changes: 2 additions & 1 deletion Bolts/iOS/BFWebViewAppLinkResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#import <Bolts/BFAppLinkResolving.h>

/*!
A reference implementation for an App Link resolver that uses a hidden UIWebView
A reference implementation for an App Link resolver that uses a hidden WKWebView
to parse the HTML containing App Link metadata.
*/
@interface BFWebViewAppLinkResolver : NSObject <BFAppLinkResolving>
Expand All @@ -24,3 +24,4 @@
+ (instancetype)sharedInstance;

@end

82 changes: 48 additions & 34 deletions Bolts/iOS/BFWebViewAppLinkResolver.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
*/

#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
roremeol marked this conversation as resolved.
Show resolved Hide resolved

#import "BFWebViewAppLinkResolver.h"
#import "BFAppLink.h"
Expand Down Expand Up @@ -47,40 +47,42 @@
static NSString *const BFWebViewAppLinkResolverWebURLKey = @"url";
static NSString *const BFWebViewAppLinkResolverShouldFallbackKey = @"should_fallback";

@interface BFWebViewAppLinkResolverWebViewDelegate : NSObject <UIWebViewDelegate>
@interface BFWebViewAppLinkResolverWebViewDelegate : NSObject <WKNavigationDelegate>

@property (nonatomic, copy) void (^didFinishLoad)(UIWebView *webView);
@property (nonatomic, copy) void (^didFailLoadWithError)(UIWebView *webView, NSError *error);
@property (nonatomic, copy) void (^didFinishLoad)(WKWebView *webView);
@property (nonatomic, copy) void (^didFailLoadWithError)(WKWebView *webView, NSError *error);
@property (nonatomic, assign) BOOL hasLoaded;

@end

@implementation BFWebViewAppLinkResolverWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView {
- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
if (self.didFinishLoad) {
self.didFinishLoad(webView);
}
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation
{

}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
- (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
if (self.didFailLoadWithError) {
self.didFailLoadWithError(webView, error);
}
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
if (self.hasLoaded) {
// Consider loading a second resource to be "success", since it indicates an inner frame
// or redirect is happening. We can run the tag extraction script at this point.
self.didFinishLoad(webView);
return NO;
}
self.hasLoaded = YES;
return YES;

decisionHandler(WKNavigationActionPolicyAllow);
}

@end
Expand Down Expand Up @@ -150,34 +152,41 @@ - (BFTask *)appLinkFromURLInBackground:(NSURL *)url NS_EXTENSION_UNAVAILABLE_IOS
NSHTTPURLResponse *response = task.result[@"response"];
BFTaskCompletionSource *tcs = [BFTaskCompletionSource taskCompletionSource];

UIWebView *webView = [[UIWebView alloc] init];
WKWebView *webView = [[WKWebView alloc] init];
BFWebViewAppLinkResolverWebViewDelegate *listener = [[BFWebViewAppLinkResolverWebViewDelegate alloc] init];
__block BFWebViewAppLinkResolverWebViewDelegate *retainedListener = listener;
listener.didFinishLoad = ^(UIWebView *view) {
listener.didFinishLoad = ^(WKWebView *view) {
if (retainedListener) {
NSDictionary *ogData = [self getALDataFromLoadedPage:view];
[view removeFromSuperview];
view.delegate = nil;
retainedListener = nil;
[tcs setResult:[self appLinkFromALData:ogData destination:url]];
[self getALDataFromLoadedPage:view completion:^(NSDictionary *result, NSError *error) {
[view removeFromSuperview];
view.navigationDelegate = nil;
retainedListener = nil;
[tcs setResult:[self appLinkFromALData:result destination:url]];
}];
}
};
listener.didFailLoadWithError = ^(UIWebView* view, NSError *error) {
listener.didFailLoadWithError = ^(WKWebView* view, NSError *error) {
if (retainedListener) {
[view removeFromSuperview];
view.delegate = nil;
view.navigationDelegate = nil;
retainedListener = nil;
[tcs setError:error];
}
};
webView.delegate = listener;
webView.navigationDelegate = listener;
webView.configuration.preferences.javaScriptEnabled = true;
webView.hidden = YES;
[webView loadData:responseData
MIMEType:response.MIMEType
textEncodingName:response.textEncodingName
baseURL:response.URL];
UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
[window addSubview:webView];
if (@available(iOS 9.0, *)) {
[webView loadData:responseData
MIMEType:response.MIMEType
characterEncodingName:response.textEncodingName
baseURL:response.URL];

UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
[window addSubview:webView];
} else {
// Fallback on earlier versions
}

return tcs.task;
}];
Expand Down Expand Up @@ -220,14 +229,19 @@ - (NSDictionary *)parseALData:(NSArray *)dataArray {
return al;
}

- (NSDictionary *)getALDataFromLoadedPage:(UIWebView *)webView {
// Run some JavaScript in the webview to fetch the meta tags.
NSString *jsonString = [webView stringByEvaluatingJavaScriptFromString:BFWebViewAppLinkResolverTagExtractionJavaScript];
NSError *error = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&error];
return [self parseALData:arr];
- (void)getALDataFromLoadedPage:(WKWebView *)webView completion:(void (^ _Nullable)(NSDictionary * _Nullable, NSError * _Nullable error))completionHandler {
[webView evaluateJavaScript:BFWebViewAppLinkResolverTagExtractionJavaScript completionHandler:^(id result, NSError * error) {
if (error == nil) {
NSString *jsonString = result;
NSError *parseError = nil;
NSArray *arr = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]
options:0
error:&parseError];
completionHandler([self parseALData:arr], parseError);
} else {
completionHandler(nil, error);
}
}];
}

/*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ NSError *error = nil;

### Resolving App Link Metadata

Bolts allows for custom App Link resolution, which may be used as a performance optimization (e.g. caching the metadata) or as a mechanism to allow developers to use a centralized index for obtaining App Link metadata. A custom App Link resolver just needs to be able to take a URL and return a `BFAppLink` containing the ordered list of `BFAppLinkTarget`s that are applicable for this device. Bolts provides one of these out of the box that performs this resolution on the device using a hidden UIWebView.
Bolts allows for custom App Link resolution, which may be used as a performance optimization (e.g. caching the metadata) or as a mechanism to allow developers to use a centralized index for obtaining App Link metadata. A custom App Link resolver just needs to be able to take a URL and return a `BFAppLink` containing the ordered list of `BFAppLinkTarget`s that are applicable for this device. Bolts provides one of these out of the box that performs this resolution on the device using a hidden ~~UIWebView~~ WKWebview.

You can use any resolver that implements the `BFAppLinkResolving` protocol by using one of the overloads on `BFAppLinkNavigation`:

Expand Down
2 changes: 1 addition & 1 deletion Vendor/xctoolchain