Skip to content

Commit e19f1ed

Browse files
robertbarclayp2
authored andcommitted
Added a guard check to request the shared UIApplication instance usin… (#212)
Added a guard check to request the shared UIApplication instance using the "key for value" method on UIApplication to bypass the compiler warnings allowing OAuth2 library to be used within a library that is used as a private framework for an iOS app that has app extensions. This performs an optionality check on UIApplication, so that if its available to be called, it can be called, if not, it throws the appropriate error. Fixes #133.
1 parent 863d3a2 commit e19f1ed

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Sources/iOS/OAuth2Authorizer+iOS.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,16 @@ open class OAuth2Authorizer: OAuth2AuthorizerUI {
5454
- throws: UnableToOpenAuthorizeURL on failure
5555
*/
5656
public func openAuthorizeURLInBrowser(_ url: URL) throws {
57-
if !UIApplication.shared.openURL(url) {
57+
58+
// By asking for the shared instance method by using the "value for key" method on UIApplication, we are able to
59+
// bypass the Swift compilation restriction that blocks the library from being compiled for an extension when
60+
// directly referencing it. We do it as an optional so in the advent of this method being called, like in an
61+
// extension, we handle it as though its not supported.
62+
guard let application = UIApplication.value(forKey: "sharedApplication") as? UIApplication else {
63+
throw OAuth2Error.unableToOpenAuthorizeURL
64+
}
65+
66+
if !application.openURL(url) {
5867
throw OAuth2Error.unableToOpenAuthorizeURL
5968
}
6069
}

0 commit comments

Comments
 (0)