Skip to content

Commit

Permalink
- updated podspec
Browse files Browse the repository at this point in the history
- introduced PresentableUserAgent
- deprecated makePresentableUserAgent from WebViewUserAgentViewController
- updated test apps
  • Loading branch information
KoCMoHaBTa committed Feb 6, 2020
1 parent 60c8c31 commit b4ead3b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ class AuthorizationCodeGrantFlowInputViewController: UITableViewController, UITe
return
}

let webViewController = WebViewUserAgentViewController()
let userAgent = webViewController.makePresentableUserAgent(present: { [weak self] (webViewController) in
let userAgent = PresentableUserAgent(WebViewUserAgentViewController(), presentationHandler: { [weak self] (webViewController) in

self?.navigationController?.pushViewController(webViewController, animated: true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class ImplicitGrantFlowInputViewController: UITableViewController, UITextFieldDe
return
}

let webViewController = WebViewUserAgentViewController()
let userAgent = webViewController.makePresentableUserAgent(present: { [weak self] (webViewController) in
let userAgent = PresentableUserAgent(WebViewUserAgentViewController(), presentationHandler: { [weak self] (webViewController) in

self?.navigationController?.pushViewController(webViewController, animated: true)

Expand Down
5 changes: 0 additions & 5 deletions MHIdentityKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,5 @@ Pod::Spec.new do |s|
s.osx.exclude_files = "#{s.name}/**/iOS/*.swift", "#{s.name}/**/tvOS/*.swift", "#{s.name}/**/watchOS/*.swift"
s.tvos.exclude_files = "#{s.name}/**/iOS/*.swift", "#{s.name}/**/macOS/*.swift", "#{s.name}/**/watchOS/*.swift"
s.watchos.exclude_files = "#{s.name}/**/iOS/*.swift", "#{s.name}/**/macOS/*.swift", "#{s.name}/**/tvOS/*.swift"

s.ios.source_files = "#{s.name}/**/iOS/*.swift"
s.osx.source_files = "#{s.name}/**/macOS/*.swift"
s.tvos.source_files = "#{s.name}/**/tvOS/*.swift"
s.watchos.source_files = "#{s.name}/**/watchOS/*.swift"

end
12 changes: 11 additions & 1 deletion MHIdentityKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
E662041322CA2CA6002F3404 /* ImplicitGrantFlowInputViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E662041222CA2CA6002F3404 /* ImplicitGrantFlowInputViewController.swift */; };
E662041522CA2CB5002F3404 /* ResourceOwnerPasswordCredentialsGrantFlowInputViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = E662041422CA2CB5002F3404 /* ResourceOwnerPasswordCredentialsGrantFlowInputViewController.swift */; };
E662041822CA2E48002F3404 /* MHIdentityKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = E6985DBB206901AD00BE5F6A /* MHIdentityKit.framework */; };
E66CA2A823EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */; };
E66CA2A923EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */; };
E66CA2AA23EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */; };
E66CA2AB23EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */ = {isa = PBXBuildFile; fileRef = E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */; };
E675980E1F13A032008C1262 /* NetworkResponseValidator.swift in Sources */ = {isa = PBXBuildFile; fileRef = E675980D1F13A032008C1262 /* NetworkResponseValidator.swift */; };
E675981B1F14D5BF008C1262 /* URLEncodingUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = E675981A1F14D5BF008C1262 /* URLEncodingUtilities.swift */; };
E68221AF1F7703B700193B48 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = E68221AE1F7703B700193B48 /* AppDelegate.swift */; };
Expand Down Expand Up @@ -312,13 +316,14 @@
E61974B11EE5688800DAFC15 /* OAuth2IdentityManagerTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OAuth2IdentityManagerTests.swift; sourceTree = "<group>"; };
E645850C1EF7FA7800D34F98 /* UserDefaultsIdentityStorage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserDefaultsIdentityStorage.swift; sourceTree = "<group>"; };
E648E8F6217A296F00FC15BA /* LICENSE */ = {isa = PBXFileReference; lastKnownFileType = text; path = LICENSE; sourceTree = "<group>"; };
E648EF9921F8A77F00CC79D6 /* MHIdentityKit.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = MHIdentityKit.podspec; sourceTree = "<group>"; };
E648EF9921F8A77F00CC79D6 /* MHIdentityKit.podspec */ = {isa = PBXFileReference; lastKnownFileType = text; path = MHIdentityKit.podspec; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.ruby; };
E662040E22CA2C6D002F3404 /* ClientCredentialsGrantFlowInputViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientCredentialsGrantFlowInputViewController.swift; sourceTree = "<group>"; };
E662041022CA2C8F002F3404 /* AuthorizationCodeGrantFlowInputViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthorizationCodeGrantFlowInputViewController.swift; sourceTree = "<group>"; };
E662041222CA2CA6002F3404 /* ImplicitGrantFlowInputViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImplicitGrantFlowInputViewController.swift; sourceTree = "<group>"; };
E662041422CA2CB5002F3404 /* ResourceOwnerPasswordCredentialsGrantFlowInputViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResourceOwnerPasswordCredentialsGrantFlowInputViewController.swift; sourceTree = "<group>"; };
E662042122CA3C8E002F3404 /* MHIdentityKit-macOSTestsHost.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "MHIdentityKit-macOSTestsHost.entitlements"; sourceTree = "<group>"; };
E662042322CA4998002F3404 /* WebViewUserAgentViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewUserAgentViewController.swift; sourceTree = "<group>"; };
E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PresentableUserAgent.swift; sourceTree = "<group>"; };
E66D60571ED85F590049657F /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
E675980D1F13A032008C1262 /* NetworkResponseValidator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkResponseValidator.swift; sourceTree = "<group>"; };
E675981A1F14D5BF008C1262 /* URLEncodingUtilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = URLEncodingUtilities.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -633,6 +638,7 @@
children = (
E6985E89206A588100BE5F6A /* UserAgent.swift */,
E6B09FAA21160873000A94C8 /* AnyUserAgent.swift */,
E66CA2A723EC3A0A0070DFB7 /* PresentableUserAgent.swift */,
E6B09F8A2115E0BA000A94C8 /* iOS */,
E662042222CA4977002F3404 /* macOS */,
);
Expand Down Expand Up @@ -1144,6 +1150,7 @@
E6E83B6B211F13AC0062CB99 /* AnyRequestAuthorizer.swift in Sources */,
E683DBA31ED6B327000D7853 /* Scope.swift in Sources */,
E61974B01EE5684500DAFC15 /* OAuth2IdentityManager.swift in Sources */,
E66CA2A823EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */,
E69D5EF91ED85C240099EE36 /* HTTPBasicAuthorizer.swift in Sources */,
E675980E1F13A032008C1262 /* NetworkResponseValidator.swift in Sources */,
E69D5EF41ED85C030099EE36 /* CredentialsProvider.swift in Sources */,
Expand Down Expand Up @@ -1232,6 +1239,7 @@
E6E83B5B211F0EEA0062CB99 /* AnyNetworkClient.swift in Sources */,
E6985D2A2068FDDB00BE5F6A /* BearerAccessTokenAuthorizer.swift in Sources */,
E6E83B60211F10C60062CB99 /* AnyCredentialsProvider.swift in Sources */,
E66CA2A923EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */,
E6985D1E2068FDD000BE5F6A /* NetworkClient.swift in Sources */,
E6985D282068FDDB00BE5F6A /* RequestAuthorizer.swift in Sources */,
E6985D352068FDE300BE5F6A /* ClientCredentialsGrantFlow.swift in Sources */,
Expand Down Expand Up @@ -1278,6 +1286,7 @@
E6E83B5C211F0EEA0062CB99 /* AnyNetworkClient.swift in Sources */,
E6985DE520690C5D00BE5F6A /* BearerAccessTokenAuthorizer.swift in Sources */,
E6E83B61211F10C60062CB99 /* AnyCredentialsProvider.swift in Sources */,
E66CA2AA23EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */,
E6985DD920690C5D00BE5F6A /* NetworkClient.swift in Sources */,
E6985DE320690C5D00BE5F6A /* RequestAuthorizer.swift in Sources */,
E6985DF020690C5D00BE5F6A /* ClientCredentialsGrantFlow.swift in Sources */,
Expand Down Expand Up @@ -1331,6 +1340,7 @@
E6985DFE20690C5D00BE5F6A /* IdentityStorage.swift in Sources */,
E6985DF420690C5D00BE5F6A /* OSStatus.swift in Sources */,
E6985E0A20690C5D00BE5F6A /* AccessTokenRefresher.swift in Sources */,
E66CA2AB23EC3A0A0070DFB7 /* PresentableUserAgent.swift in Sources */,
E6985DF520690C5D00BE5F6A /* Keychain.swift in Sources */,
E6985E0120690C5D00BE5F6A /* KeychainIdentityStorage.swift in Sources */,
E6B1956C22C9FA6000954DED /* ImplicitGrantFlow.swift in Sources */,
Expand Down
65 changes: 65 additions & 0 deletions MHIdentityKit/Infrastructure/UserAgent/PresentableUserAgent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// PresentableUserAgent.swift
// MHIdentityKit
//
// Created by Milen Halachev on 6.02.20.
// Copyright © 2020 Milen Halachev. All rights reserved.
//

import Foundation


///A type that makes a given user agent persentable.
public struct PresentableUserAgent<T: UserAgent>: UserAgent {

public var userAgent: T
public var presentationHandler: (T) -> Void
public var dismissHandler: (T) -> Void

/**
Makes a presentable UserAgent of a given user agent.

- parameter userAgent: The user agent to present.
- parameter presentationHandler: This is the presentation handler. Called when the user agent has to be shown on screen.
- parameter dismissHandler: This is the dimiss handler. Called when the user agent successfully handles a redirect and has to be dismissed.

- note: If the user agent is UIViewController, It is recommended embed it into UINavigationController with visible toolbar, because it contains web navigation controls. If you present it modally within an UINavigationController - it is your responsibility to setup a cancel/close button, based on your needs.
*/

public init(_ userAgent: T, presentationHandler: @escaping (T) -> Void, dismissHandler: @escaping (T) -> Void) {

self.userAgent = userAgent
self.presentationHandler = presentationHandler
self.dismissHandler = dismissHandler
}

public func perform(_ request: URLRequest, redirectURI: URL?, redirectionHandler: @escaping (URLRequest) throws -> Bool) {

DispatchQueue.main.async {

self.userAgent.perform(request, redirectURI: redirectURI, redirectionHandler: { (request) -> Bool in

let didHandleRedirect = try redirectionHandler(request)

if didHandleRedirect {

self.dismiss()
}

return didHandleRedirect
})

self.present()
}
}

public func present() {

self.presentationHandler(self.userAgent)
}

public func dismiss() {

self.dismissHandler(self.userAgent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,19 @@ open class WebViewUserAgentViewController: UIViewController, WKNavigationDelegat

open func perform(_ request: URLRequest, redirectURI: URL?, redirectionHandler: @escaping (URLRequest) throws -> Bool) {

self.request = request
self.redirectURI = redirectURI
self.redirectionHandler = redirectionHandler

guard self.isViewLoaded else {
DispatchQueue.main.async {

return
self.request = request
self.redirectURI = redirectURI
self.redirectionHandler = redirectionHandler

guard self.isViewLoaded else {

return
}

self.loadData()
}

self.loadData()
}
}

Expand All @@ -208,31 +211,12 @@ extension WebViewUserAgentViewController {
- note: It is recommended embed the view controller into UINavigationController with visible toolbar, because it contains web navigation controls. If you present it modally within an UINavigationController - it is your responsibility to setup a cancel/close button, based on your needs.
*/

@available(*, deprecated, message: "Use PresentableUserAgent instead.")
open func makePresentableUserAgent(present: @escaping (WebViewUserAgentViewController) -> Void, dismiss: @escaping (WebViewUserAgentViewController) -> Void) -> UserAgent {

return AnyUserAgent { [weak self] (request, redirectURL, redicationHandler) in

guard let _self = self else {

return
}

_self.perform(request, redirectURI: redirectURL, redirectionHandler: { (request) -> Bool in

let didHandleRedirect = try redicationHandler(request)

if didHandleRedirect, let _self = self {

dismiss(_self)
}

return didHandleRedirect
})

present(_self)
}

return PresentableUserAgent(self, presentationHandler: present, dismissHandler: dismiss)
}
}

#endif

Original file line number Diff line number Diff line change
Expand Up @@ -287,30 +287,10 @@ extension WebViewUserAgentViewController {
- note: It is recommended embed the view controller into UINavigationController with visible toolbar, because it contains web navigation controls. If you present it modally within an UINavigationController - it is your responsibility to setup a cancel/close button, based on your needs.
*/

@available(*, deprecated, message: "Use PresentableUserAgent instead.")
open func makePresentableUserAgent(present: @escaping (WebViewUserAgentViewController) -> Void, dismiss: @escaping (WebViewUserAgentViewController) -> Void) -> UserAgent {

return AnyUserAgent { [weak self] (request, redirectURL, redicationHandler) in

guard let _self = self else {

return
}

_self.perform(request, redirectURI: redirectURL, redirectionHandler: { (request) -> Bool in

let didHandleRedirect = try redicationHandler(request)

if didHandleRedirect, let _self = self {

dismiss(_self)
}

return didHandleRedirect
})

present(_self)
}

return PresentableUserAgent(self, presentationHandler: present, dismissHandler: dismiss)
}
}

Expand Down

0 comments on commit b4ead3b

Please sign in to comment.