Skip to content

Commit

Permalink
Mark ACAccountStore as deprecated in macOS 10.13 and iOS 11
Browse files Browse the repository at this point in the history
  • Loading branch information
meteochu committed May 10, 2020
1 parent 754cbd0 commit a08088f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 52 deletions.
1 change: 1 addition & 0 deletions Sources/Swifter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 3 additions & 1 deletion Sources/SwifterCredential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
85 changes: 47 additions & 38 deletions SwifterDemoMac/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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!)
}
}
}
1 change: 1 addition & 0 deletions SwifterDemoiOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import UIKit
import SwifteriOS

enum AuthorizationMode {
@available(iOS, deprecated: 11.0)
case acaccount
case browser
case sso
Expand Down
30 changes: 17 additions & 13 deletions SwifterDemoiOS/AuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a08088f

Please sign in to comment.