94 lines
3.1 KiB
Swift
94 lines
3.1 KiB
Swift
//
|
|
// OnboardingViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 8/18/18.
|
|
// Copyright © 2018 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import AuthenticationServices
|
|
|
|
protocol OnboardingViewControllerDelegate {
|
|
|
|
func didFinishOnboarding()
|
|
|
|
}
|
|
|
|
class OnboardingViewController: UIViewController {
|
|
|
|
var delegate: OnboardingViewControllerDelegate?
|
|
|
|
@IBOutlet weak var urlTextField: UITextField!
|
|
|
|
var authenticationSession: ASWebAuthenticationSession?
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
}
|
|
|
|
@IBAction func loginPressed(_ sender: Any) {
|
|
guard let text = urlTextField.text,
|
|
var components = URLComponents(string: text) else { return }
|
|
|
|
LocalData.shared.instanceURL = text
|
|
MastodonController.shared.createClient {
|
|
let clientID = LocalData.shared.clientID!
|
|
|
|
let callbackURL = "tusker://oauth"
|
|
|
|
components.path = "/oauth/authorize"
|
|
components.queryItems = [
|
|
URLQueryItem(name: "client_id", value: clientID),
|
|
URLQueryItem(name: "response_type", value: "code"),
|
|
URLQueryItem(name: "scope", value: "read write follow"),
|
|
URLQueryItem(name: "redirect_uri", value: callbackURL)
|
|
]
|
|
let url = components.url!
|
|
|
|
print("oauth url: \(url)")
|
|
|
|
DispatchQueue.main.async {
|
|
self.delegate?.didFinishOnboarding()
|
|
UIApplication.shared.open(url, options: [:], completionHandler: nil)
|
|
}
|
|
|
|
// self.delegate?.didFinishOnboarding()
|
|
// self.authenticationSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURL) { url, error in
|
|
// guard error == nil,
|
|
// let url = url,
|
|
// let components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { fatalError() }
|
|
//
|
|
// print("callback url: \(url)")
|
|
//
|
|
// let item = components.queryItems?.first { $0.name == "code" }
|
|
// if let accessToken = item?.value {
|
|
// LocalData.shared.accessToken = accessToken
|
|
// MastodonController.shared.client.accessToken = accessToken
|
|
// self.delegate?.didFinishOnboarding()
|
|
// self.authenticationSession = nil
|
|
// }
|
|
// }
|
|
// self.authenticationSession!.start()
|
|
}
|
|
}
|
|
|
|
@IBAction func clearDataPressed(_ sender: Any) {
|
|
LocalData.shared.instanceURL = nil
|
|
LocalData.shared.clientID = nil
|
|
LocalData.shared.clientSecret = nil
|
|
LocalData.shared.refreshToken = nil
|
|
}
|
|
|
|
/*
|
|
// MARK: - Navigation
|
|
|
|
// In a storyboard-based application, you will often want to do a little preparation before navigation
|
|
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
|
// Get the new view controller using segue.destination.
|
|
// Pass the selected object to the new view controller.
|
|
}
|
|
*/
|
|
|
|
}
|