Skip to content

Commit

Permalink
make iOS twitter login default using SFSafariViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
huiping.guo committed Jan 15, 2020
1 parent 8889232 commit 2a0b841
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
22 changes: 7 additions & 15 deletions Sources/SwifterAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,12 @@ public extension Swifter {
Begin Authorization with a Callback URL
- Parameter presentFromViewController: The viewController used to present the SFSafariViewController.
The UIViewController must inherit SFSafariViewControllerDelegate
*/

#if os(iOS)
func authorize(withCallback callbackURL: URL,
presentingFrom presenting: UIViewController?,
forceLogin: Bool = false,
safariDelegate: SFSafariViewControllerDelegate? = nil,
success: TokenSuccessHandler?,
failure: FailureHandler? = nil) {
self.postOAuthRequestToken(with: callbackURL, success: { token, response in
Expand All @@ -99,20 +96,15 @@ public extension Swifter {
success?(accessToken!, response)
}, failure: failure)
}
let forceLogin = forceLogin ? "&force_login=true" : ""
let query = "oauth/authorize?oauth_token=\(token!.key)\(forceLogin)"
let forceLogin = forceLogin ? "&force_login=true" : ""
let query = "oauth/authorize?oauth_token=\(token!.key)\(forceLogin)"
let queryUrl = URL(string: query, relativeTo: TwitterURL.oauth.url)!.absoluteURL

if let delegate = safariDelegate ?? (presenting as? SFSafariViewControllerDelegate) {
let safariView = SFSafariViewController(url: queryUrl)
safariView.delegate = delegate
safariView.modalTransitionStyle = .coverVertical
safariView.modalPresentationStyle = .overFullScreen
presenting?.present(safariView, animated: true, completion: nil)
} else {
UIApplication.shared.open(queryUrl, options: [:], completionHandler: nil)
let webAuthenticationViewController = WebAuthenticationViewController(queryUrl: queryUrl) {
let error = SwifterError(message: "User login cancelled ", kind: .cancelled)
failure?(error)
}
presenting?.present(webAuthenticationViewController, animated: true, completion: nil)
}, failure: failure)
}

Expand Down
64 changes: 64 additions & 0 deletions Sources/WebAuthenticationViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// WebAuthenticationViewController.swift
// SwifteriOS
//
// Created by Huiping Guo on 2020/01/14.
// Copyright © 2020 Matt Donnelly. All rights reserved.
//

import Foundation
import UIKit
import SafariServices

class WebAuthenticationViewController: UIViewController {

let queryUrl: URL

let dismissHandler: (() -> Void)?

init(queryUrl: URL, dismissHandler: (() -> Void)? = nil) {
self.queryUrl = queryUrl
self.dismissHandler = dismissHandler
super.init(nibName: nil, bundle: nil)
}

required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override func viewDidLoad() {
super.viewDidLoad()

embedViewController(viewController: createWebViewController())
}

func embedViewController(viewController: UIViewController) {
guard let subview = viewController.view, let superview = view else { return }

addChild(viewController)
view.addSubview(subview)

subview.translatesAutoresizingMaskIntoConstraints = false
subview.topAnchor.constraint(equalTo: superview.topAnchor, constant: 0).isActive = true
subview.bottomAnchor.constraint(equalTo: superview.bottomAnchor, constant: 0).isActive = true
subview.leadingAnchor.constraint(equalTo: superview.leadingAnchor, constant: 0).isActive = true
subview.trailingAnchor.constraint(equalTo: superview.trailingAnchor, constant: 0).isActive = true

viewController.didMove(toParent: self)
}

func createWebViewController() -> SFSafariViewController {
let safariView = SFSafariViewController(url: queryUrl)
safariView.delegate = self
safariView.modalTransitionStyle = .coverVertical
safariView.modalPresentationStyle = .overFullScreen
return safariView
}
}

extension WebAuthenticationViewController: SFSafariViewControllerDelegate {
func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
dismissHandler?()
}

}
4 changes: 4 additions & 0 deletions Swifter.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
4ED2E25C215E1E1A00E2084A /* JSONTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4ED2E25B215E1E1A00E2084A /* JSONTest.swift */; };
7DA5CF6A23CE0C9E00F54CA0 /* WebAuthenticationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DA5CF6923CE0C9E00F54CA0 /* WebAuthenticationViewController.swift */; };
8B116AF1194601970019A4DC /* SwifterHTTPRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B116AF0194601970019A4DC /* SwifterHTTPRequest.swift */; };
8B116AF2194601970019A4DC /* SwifterHTTPRequest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B116AF0194601970019A4DC /* SwifterHTTPRequest.swift */; };
8B1F259F194A663D004BD304 /* SwifterHelp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B1F259E194A663D004BD304 /* SwifterHelp.swift */; };
Expand Down Expand Up @@ -154,6 +155,7 @@

/* Begin PBXFileReference section */
4ED2E25B215E1E1A00E2084A /* JSONTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JSONTest.swift; sourceTree = "<group>"; };
7DA5CF6923CE0C9E00F54CA0 /* WebAuthenticationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebAuthenticationViewController.swift; sourceTree = "<group>"; };
8B116AF0194601970019A4DC /* SwifterHTTPRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwifterHTTPRequest.swift; sourceTree = "<group>"; };
8B1F259E194A663D004BD304 /* SwifterHelp.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = SwifterHelp.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
8B300C1F1944AA1A00993175 /* SwifterDemoiOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwifterDemoiOS.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -363,6 +365,7 @@
8B116AF0194601970019A4DC /* SwifterHTTPRequest.swift */,
D0DCE3ED1F09C5E700B62429 /* SwifterGIF.swift */,
8B33CA3E19587C6D00739FCB /* JSON.swift */,
7DA5CF6923CE0C9E00F54CA0 /* WebAuthenticationViewController.swift */,
8B9F51591944A8E100894629 /* Extensions */,
8B5EFF10195F085B00B354F9 /* Supporting Files */,
);
Expand Down Expand Up @@ -658,6 +661,7 @@
8BF1D6761948825D00E3AA64 /* SwifterSavedSearches.swift in Sources */,
A1E207A91D4B85D90093E498 /* HMAC.swift in Sources */,
8BCF43171947450A00E63301 /* SwifterTweets.swift in Sources */,
7DA5CF6A23CE0C9E00F54CA0 /* WebAuthenticationViewController.swift in Sources */,
8BF1D6851948BCC100E3AA64 /* SwifterAccountsClient.swift in Sources */,
8B548F591945325E00EE2927 /* SwifterAuth.swift in Sources */,
8BCF431D1947907300E63301 /* SwifterMessages.swift in Sources */,
Expand Down

0 comments on commit 2a0b841

Please sign in to comment.