From a08088f7b74e696f5bcb1be7a4e2f5a43a9cba85 Mon Sep 17 00:00:00 2001 From: Andy Liang Date: Sat, 9 May 2020 22:32:06 -0700 Subject: [PATCH] Mark ACAccountStore as deprecated in macOS 10.13 and iOS 11 --- Sources/Swifter.swift | 1 + Sources/SwifterCredential.swift | 4 +- SwifterDemoMac/ViewController.swift | 85 ++++++++++++++----------- SwifterDemoiOS/AppDelegate.swift | 1 + SwifterDemoiOS/AuthViewController.swift | 30 +++++---- 5 files changed, 69 insertions(+), 52 deletions(-) diff --git a/Sources/Swifter.swift b/Sources/Swifter.swift index 47305bdc..0c0330da 100644 --- a/Sources/Swifter.swift +++ b/Sources/Swifter.swift @@ -129,6 +129,7 @@ public class Swifter { } #if os(macOS) || os(iOS) + @available(iOS, deprecated: 11.0, message: "Using ACAccount for Twitter is no longer supported as of iOS 11.") public init(account: ACAccount) { self.client = AccountsClient(account: account) } diff --git a/Sources/SwifterCredential.swift b/Sources/SwifterCredential.swift index 184d7ccb..017db059 100644 --- a/Sources/SwifterCredential.swift +++ b/Sources/SwifterCredential.swift @@ -61,8 +61,10 @@ public class Credential { public internal(set) var accessToken: OAuthAccessToken? #if os(macOS) || os(iOS) + @available(iOS, deprecated: 11.0, message: "Using ACAccount for Twitter is no longer supported as of iOS 11.") public internal(set) var account: ACAccount? - + + @available(iOS, deprecated: 11.0, message: "Using ACAccount for Twitter is no longer supported as of iOS 11.") public init(account: ACAccount) { self.account = account } diff --git a/SwifterDemoMac/ViewController.swift b/SwifterDemoMac/ViewController.swift index 4347bede..ff6d46bd 100644 --- a/SwifterDemoMac/ViewController.swift +++ b/SwifterDemoMac/ViewController.swift @@ -28,53 +28,62 @@ import Accounts import SwifterMac class ViewController: NSViewController { - let useACAccount = false @objc dynamic var tweets: [Tweet] = [] override func viewDidLoad() { super.viewDidLoad() - - let failureHandler: (Error) -> Void = { print($0.localizedDescription) } - - if useACAccount { - let accountStore = ACAccountStore() - let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) - - accountStore.requestAccessToAccounts(with: accountType, options: nil) { granted, error in - guard granted else { - print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") - return - } - - guard let twitterAccounts = accountStore.accounts(with: accountType) , !twitterAccounts.isEmpty else { - print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") - return - } - - let twitterAccount = twitterAccounts[0] as! ACAccount - let swifter = Swifter(account: twitterAccount) - - swifter.getHomeTimeline(count: 20, success: { statuses in - print(statuses) - }, failure: failureHandler) - } + + if #available(macOS 10.13, *) { + authorizeWithWebLogin() + } else if useACAccount { + authorizeWithACAccountStore() } else { - let swifter = Swifter(consumerKey: "nLl1mNYc25avPPF4oIzMyQzft", - consumerSecret: "Qm3e5JTXDhbbLl44cq6WdK00tSUwa17tWlO8Bf70douE4dcJe2") - let callbackUrl = URL(string: "swifter://success")! - swifter.authorize(withCallback: callbackUrl, success: { _, _ in - swifter.getHomeTimeline(count: 100, success: { statuses in - guard let tweets = statuses.array else { return } - self.tweets = tweets.map { - return Tweet(name: $0["user"]["name"].string!, text: $0["text"].string!) - } - }, failure: failureHandler) - }, failure: failureHandler) + authorizeWithWebLogin() } } -} + @available(macOS, deprecated: 10.13) + private func authorizeWithACAccountStore() { + let accountStore = ACAccountStore() + let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) + accountStore.requestAccessToAccounts(with: accountType, options: nil) { granted, error in + guard granted else { + print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") + return + } + + guard let twitterAccount = accountStore.accounts(with: accountType).first as? ACAccount else { + print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") + return + } + let swifter = Swifter(account: twitterAccount) + swifter.getHomeTimeline(count: 100, success: { statuses in + self.processTweets(result: statuses) + }) { print($0.localizedDescription) } + } + } + + private func authorizeWithWebLogin() { + let swifter = Swifter( + consumerKey: "nLl1mNYc25avPPF4oIzMyQzft", + consumerSecret: "Qm3e5JTXDhbbLl44cq6WdK00tSUwa17tWlO8Bf70douE4dcJe2" + ) + let callbackUrl = URL(string: "swifter://success")! + swifter.authorize(withCallback: callbackUrl, success: { _, _ in + swifter.getHomeTimeline(count: 100, success: { statuses in + self.processTweets(result: statuses) + }) { print($0.localizedDescription) } + }) { print($0.localizedDescription) } + } + + private func processTweets(result: JSON) { + guard let tweets = result.array else { return } + self.tweets = tweets.map { + return Tweet(name: $0["user"]["name"].string!, text: $0["text"].string!) + } + } +} diff --git a/SwifterDemoiOS/AppDelegate.swift b/SwifterDemoiOS/AppDelegate.swift index 379f06bd..b5e7b725 100755 --- a/SwifterDemoiOS/AppDelegate.swift +++ b/SwifterDemoiOS/AppDelegate.swift @@ -27,6 +27,7 @@ import UIKit import SwifteriOS enum AuthorizationMode { + @available(iOS, deprecated: 11.0) case acaccount case browser case sso diff --git a/SwifterDemoiOS/AuthViewController.swift b/SwifterDemoiOS/AuthViewController.swift index ad21c26c..036c4fa9 100755 --- a/SwifterDemoiOS/AuthViewController.swift +++ b/SwifterDemoiOS/AuthViewController.swift @@ -43,20 +43,24 @@ class AuthViewController: UIViewController, SFSafariViewControllerDelegate { // You can change the authorizationMode to test different results via the AppDelegate switch authorizationMode { case .acaccount: - let store = ACAccountStore() - let type = store.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) - store.requestAccessToAccounts(with: type, options: nil) { granted, error in - guard let twitterAccounts = store.accounts(with: type), granted else { - self.alert(title: "Error", message: error!.localizedDescription) - return - } + if #available(iOS 11.0, *) { + self.alert(title: "Deprecated", message: "ACAccountStore was deprecated on iOS 11.0, please use the OAuth flow instead") + } else { + let store = ACAccountStore() + let type = store.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) + store.requestAccessToAccounts(with: type, options: nil) { granted, error in + guard let twitterAccounts = store.accounts(with: type), granted else { + self.alert(title: "Error", message: error!.localizedDescription) + return + } - if twitterAccounts.isEmpty { - self.alert(title: "Error", message: "There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") - } else { - let twitterAccount = twitterAccounts[0] as! ACAccount - self.swifter = Swifter(account: twitterAccount) - self.fetchTwitterHomeStream() + if twitterAccounts.isEmpty { + self.alert(title: "Error", message: "There are no Twitter accounts configured. You can add or create a Twitter account in Settings.") + } else { + let twitterAccount = twitterAccounts[0] as! ACAccount + self.swifter = Swifter(account: twitterAccount) + self.fetchTwitterHomeStream() + } } } case .browser: