Skip to content

Commit

Permalink
Move each url handler to its own file
Browse files Browse the repository at this point in the history
Add a factory to choose default one
Add to iOS demo app the `ASWebAuthentifcationURLHandler`
  • Loading branch information
phimage committed Nov 1, 2019
1 parent 04199eb commit e14ac9c
Show file tree
Hide file tree
Showing 13 changed files with 670 additions and 260 deletions.
52 changes: 37 additions & 15 deletions Demo/Common/FormViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ enum URLHandlerType {
case `internal`
case external
case safari
case asWeb
}


Expand Down Expand Up @@ -82,6 +83,7 @@ class FormViewController: FormViewControllerType {
#if os(iOS)
@IBOutlet weak var safariURLHandler: UISwitch!
@IBOutlet weak var safariURLHandlerView: UITableViewCell!
@IBOutlet weak var asWebURLHandler: UISwitch!
#endif

@IBAction func ok(_ sender: AnyObject?) {
Expand Down Expand Up @@ -115,9 +117,12 @@ class FormViewController: FormViewControllerType {
urlHandlerType = .`internal`
}
#if os(iOS)
if safariURLHandler == sender {
urlHandlerType = .safari
}
if safariURLHandler == sender {
urlHandlerType = .safari
}
if asWebURLHandler == sender {
urlHandlerType = .asWeb
}
#endif
} else {
// set another...
Expand All @@ -128,9 +133,12 @@ class FormViewController: FormViewControllerType {
urlHandlerType = .external
}
#if os(iOS)
if safariURLHandler == sender {
urlHandlerType = .`internal`
}
if safariURLHandler == sender {
urlHandlerType = .`internal`
}
if asWebURLHandler == sender {
urlHandlerType = .`internal`
}
#endif
}
}
Expand All @@ -144,32 +152,46 @@ class FormViewController: FormViewControllerType {
return .`internal`
}
#if os(iOS)
if safariURLHandler.isOn {
return .safari
}
if safariURLHandler.isOn {
return .safari
}
if asWebURLHandler.isOn {
return .asWeb
}
#endif
return .`internal`
}
set {
switch newValue {
case .external:
externalURLHandler.setOn(true, animated: false)
externalURLHandler.setOn(true, animated: true)
internalURLHandler.setOn(false, animated: true)
#if os(iOS)
safariURLHandler.setOn(false, animated: true)
safariURLHandler.setOn(false, animated: true)
asWebURLHandler.setOn(false, animated: true)
#endif
break
case .`internal`:
internalURLHandler.setOn(true, animated: false)
internalURLHandler.setOn(true, animated: true)
externalURLHandler.setOn(false, animated: true)
#if os(iOS)
safariURLHandler.setOn(false, animated: true)
safariURLHandler.setOn(false, animated: true)
asWebURLHandler.setOn(false, animated: true)
#endif
break
case .safari:
#if os(iOS)
safariURLHandler.setOn(true, animated: false)
#endif
safariURLHandler.setOn(true, animated: true)
asWebURLHandler.setOn(false, animated: true)
#endif
externalURLHandler.setOn(false, animated: true)
internalURLHandler.setOn(false, animated: true)
break
case .asWeb:
#if os(iOS)
safariURLHandler.setOn(false, animated: true)
asWebURLHandler.setOn(true, animated: true)
#endif
externalURLHandler.setOn(false, animated: true)
internalURLHandler.setOn(false, animated: true)
break
Expand Down
22 changes: 21 additions & 1 deletion Demo/Common/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,15 @@ extension ViewController {
}
#endif
return OAuthSwiftOpenURLExternally.sharedInstance
case .asWeb:
#if os(iOS)
if #available(iOS 13.0, *) {
return ASWebAuthenticationURLHandler(callbackUrlScheme: "oauth-swift://oauth-callback/", presentationContextProvider: self)
}
#endif
return OAuthSwiftOpenURLExternally.sharedInstance
}

#if os(OSX)
// a better way is
// - to make this ViewController implement OAuthSwiftURLHandlerType and assigned in oauthswift object
Expand Down Expand Up @@ -1666,6 +1673,19 @@ extension ViewController: NSTableViewDataSource, NSTableViewDelegate {
}
#endif

#if os(iOS)
import SafariServices
#if canImport(AuthenticationServices)
import AuthenticationServices
#endif
@available(iOS 13.0, macCatalyst 13.0, *)
extension ViewController: ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return UIApplication.shared.topWindow ?? ASPresentationAnchor()
}
}
#endif

struct FormViewControllerData {
var key: String
var secret: String
Expand Down
42 changes: 35 additions & 7 deletions Demo/iOS/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,20 @@
</variation>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" id="qg0-2m-1ru">
<rect key="frame" x="0.0" y="216" width="414" height="44"/>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" rowHeight="66" id="qg0-2m-1ru">
<rect key="frame" x="0.0" y="216" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="qg0-2m-1ru" id="Fpd-Jw-SLu">
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="66"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Open with internal web view controller" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontForContentSizeCategory="YES" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Xf8-Xn-uYs">
<rect key="frame" x="28" y="12" width="292" height="20.5"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" misplaced="YES" text="Open with internal web view controller" textAlignment="natural" lineBreakMode="wordWrap" numberOfLines="2" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Xf8-Xn-uYs">
<rect key="frame" x="28" y="12" width="292" height="21.5"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="CSA-T2-sKN">
<rect key="frame" x="337" y="6.5" width="51" height="31"/>
<rect key="frame" x="337" y="17.5" width="51" height="31"/>
<connections>
<action selector="urlHandlerChange:" destination="m6z-f8-zu8" eventType="valueChanged" id="H29-wK-fUB"/>
</connections>
Expand All @@ -149,7 +149,7 @@
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" id="F9S-Z9-luE">
<rect key="frame" x="0.0" y="260" width="414" height="44"/>
<rect key="frame" x="0.0" y="282" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="F9S-Z9-luE" id="w42-20-4Er">
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
Expand All @@ -175,6 +175,33 @@
</constraints>
</tableViewCellContentView>
</tableViewCell>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="blue" hidesAccessoryWhenEditing="NO" indentationLevel="1" indentationWidth="0.0" id="DGv-Hb-Xdh">
<rect key="frame" x="0.0" y="326" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="DGv-Hb-Xdh" id="JWC-Wj-3zD">
<rect key="frame" x="0.0" y="0.0" width="414" height="44"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ldM-Hw-txY">
<rect key="frame" x="337" y="6.5" width="51" height="31"/>
<connections>
<action selector="urlHandlerChange:" destination="m6z-f8-zu8" eventType="valueChanged" id="ryI-O1-5Ye"/>
</connections>
</switch>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Open with ASWebAuthentication" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="PE9-KD-WST">
<rect key="frame" x="28" y="11.5" width="249" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<constraints>
<constraint firstItem="ldM-Hw-txY" firstAttribute="centerY" secondItem="JWC-Wj-3zD" secondAttribute="centerY" id="3MT-wI-wcA"/>
<constraint firstAttribute="trailingMargin" secondItem="ldM-Hw-txY" secondAttribute="trailing" constant="8" id="DQP-QR-hA7"/>
<constraint firstItem="PE9-KD-WST" firstAttribute="leading" secondItem="JWC-Wj-3zD" secondAttribute="leadingMargin" constant="8" id="Rrz-HC-zhH"/>
<constraint firstItem="PE9-KD-WST" firstAttribute="centerY" secondItem="JWC-Wj-3zD" secondAttribute="centerY" id="S8E-Yc-ROE"/>
</constraints>
</tableViewCellContentView>
</tableViewCell>
</cells>
</tableViewSection>
</sections>
Expand All @@ -196,6 +223,7 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="asWebURLHandler" destination="ldM-Hw-txY" id="U1M-nH-aA1"/>
<outlet property="externalURLHandler" destination="2QY-0I-Guy" id="J8T-Ej-Zr5"/>
<outlet property="internalURLHandler" destination="CSA-T2-sKN" id="FNM-eW-Dot"/>
<outlet property="keyTextField" destination="sfn-b6-aQr" id="WuE-bx-0JF"/>
Expand Down
Loading

0 comments on commit e14ac9c

Please sign in to comment.