Skip to content

Commit 587ff48

Browse files
authored
Merge pull request #7974 from woocommerce/issue/7946-authenticated-webkit-vc
Product Preview: Add WebKitViewController to handle authenticated sessions
2 parents 1bc1f8f + 43e4dca commit 587ff48

File tree

9 files changed

+776
-13
lines changed

9 files changed

+776
-13
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Foundation
2+
import WebKit
3+
4+
enum LinkBehavior {
5+
case all
6+
case hostOnly(URL)
7+
case urlOnly(URL)
8+
9+
func handle(navigationAction: WKNavigationAction, for webView: WKWebView) -> WKNavigationActionPolicy {
10+
11+
// We only want to apply this policy for links, not for all resource loads
12+
guard navigationAction.navigationType == .linkActivated && navigationAction.request.url == navigationAction.request.mainDocumentURL else {
13+
return .allow
14+
}
15+
16+
// Should not happen, but future checks will not work if we can't check the URL
17+
guard let navigationURL = navigationAction.request.url else {
18+
return .allow
19+
}
20+
21+
switch self {
22+
case .all:
23+
return .allow
24+
case .hostOnly(let url):
25+
if navigationAction.request.url?.host == url.host {
26+
return .allow
27+
} else {
28+
UIApplication.shared.open(navigationURL)
29+
return .cancel
30+
}
31+
case .urlOnly(let url):
32+
if navigationAction.request.url?.absoluteString == url.absoluteString {
33+
return .allow
34+
} else {
35+
UIApplication.shared.open(navigationURL)
36+
return .cancel
37+
}
38+
}
39+
}
40+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Foundation
2+
import UIKit
3+
import WordPressShared.WPFontManager
4+
5+
open class NavigationTitleView: UIView {
6+
@objc public let titleLabel = UILabel(frame: defaultTitleFrame)
7+
@objc public let subtitleLabel = UILabel(frame: defaultSubtitleFrame)
8+
9+
10+
// MARK: - UIView's Methods
11+
convenience init() {
12+
self.init(frame: NavigationTitleView.defaultViewFrame)
13+
}
14+
15+
@objc convenience init(title: String?, subtitle: String?) {
16+
self.init()
17+
titleLabel.text = title ?? String()
18+
subtitleLabel.text = subtitle ?? String()
19+
}
20+
21+
override init(frame: CGRect) {
22+
super.init(frame: frame)
23+
setupSubviews()
24+
}
25+
26+
required public init(coder aDecoder: NSCoder) {
27+
super.init(coder: aDecoder)!
28+
setupSubviews()
29+
}
30+
31+
32+
// MARK: - Helpers
33+
fileprivate func setupSubviews() {
34+
titleLabel.font = WPFontManager.systemSemiBoldFont(ofSize: NavigationTitleView.defaultTitleFontSize)
35+
titleLabel.textColor = .white
36+
titleLabel.textAlignment = .center
37+
titleLabel.backgroundColor = .clear
38+
titleLabel.autoresizingMask = .flexibleWidth
39+
40+
subtitleLabel.font = WPFontManager.systemRegularFont(ofSize: NavigationTitleView.defaultSubtitleFontSize)
41+
subtitleLabel.textColor = .white
42+
subtitleLabel.textAlignment = .center
43+
subtitleLabel.backgroundColor = .clear
44+
subtitleLabel.autoresizingMask = .flexibleWidth
45+
46+
backgroundColor = UIColor.clear
47+
autoresizingMask = [.flexibleWidth, .flexibleBottomMargin, .flexibleTopMargin]
48+
clipsToBounds = true
49+
50+
addSubview(titleLabel)
51+
addSubview(subtitleLabel)
52+
}
53+
54+
// MARK: - Static Constants
55+
fileprivate static let defaultViewFrame = CGRect(x: 0.0, y: 0.0, width: 200.0, height: 35.0)
56+
57+
fileprivate static let defaultTitleFontSize = CGFloat(15)
58+
fileprivate static let defaultTitleFrame = CGRect(x: 0.0, y: 0.0, width: 200.0, height: 19.0)
59+
60+
fileprivate static let defaultSubtitleFontSize = CGFloat(10)
61+
fileprivate static let defaultSubtitleFrame = CGRect(x: 0.0, y: 19.0, width: 200.0, height: 16.0)
62+
}

0 commit comments

Comments
 (0)