-
Notifications
You must be signed in to change notification settings - Fork 15
Custom Provider
Implement subclass of Provider class. It must override name variable and init function. A name of plist file that will contains parameters must be Name of social network in future. It is important to know that redirectUri parameter is already implemented in the Provider class, so do not override it.
class CustomProvider: Provider {
override var name: String {
return "Name of social network"
}
var nameOfParameter: String {
return options["Name of parameter"] as! String
}
required init?() {
super.init()
guard options["Name of parameter"] != nil else {
LogService.output("There is no name of parameter in \(name).plist file")
return nil
}
}
}Implement this provider only if if iOS social accounts support type identifier of social network. Import Accounts framework for use ACAccountStore class.
import AccountsAlso import Social framework for use SLRequest class if needed.
import SocialImplement subclass of CustomProvider class. It must override authorize:completion: function. The advisable way of authorization flow by system provider is get system account by account:withTypeIdentifier:completion: function of ACAccountStore class, then get remote account using system account and, perhaps, additional parameters. The last things to do are parse all received data, initialize instance of Session class and return it in completion closure.
class CustomSystemProvider: CustomProvider {
override func authorize(_ completion: @escaping Providing.Completion) {
systemAccount { [unowned self] systemAccount, error in
guard let systemAccount = systemAccount else {
completion(nil, error)
return
}
self.remoteAccount(withSystemAccount: systemAccount,
completion: completion)
}
}
private func systemAccount(_ completion: @escaping ACAccountStore.Completion) {
let typeIdentifier = "The identifier of supported system account types"
ACAccountStore.account(withTypeIdentifier: typeIdentifier,
completion: completion)
}
private func remoteAccount(withSystemAccount systemAccount: ACAccount,
completion: @escaping Providing.Completion) {
// Parse all received data
// Initialize instance of `Session` class
// Return `Session` instance in completion closure
}
}Implement subclass of CustomProvider class. It must override authorize:completion: function. The advisable way of authorization flow by web provider is get access token with WebRequestService that provide authorization by UIWebView, then get account using access token and, perhaps, additional parameters. The last things to do are parse all received data, initialize instance of Session class and return it in completion closure.
class CustomWebProvider: CustomProvider {
override func authorize(_ completion: @escaping Providing.Completion) {
accessToken { [unowned self] accessToken, error in
guard let accessToken = accessToken as? String else {
completion(nil, error)
return
}
self.account(withAccessToken: accessToken, completion: completion)
}
}
private func accessToken(_ completion: @escaping URLRequest.Completion) {
let urlString = "Authorization URL of social network"
let request = URLRequest(url: URL(string: urlString)!)
WebRequestService.load(request, ofProvider: self) { url, error in
guard error == nil else {
completion(nil, error)
return
}
// Parse url into access token
// Return access token in completion closure
}
}
private func account(withAccessToken accessToken: String,
completion: @escaping Providing.Completion) {
// Parse all received data
// Initialize instance of `Session` class
// Return `Session` instance in completion closure
}
}