Created
May 25, 2020 05:30
-
-
Save teerasej/25ef6c169f510412b2da4f0d8844ee8f to your computer and use it in GitHub Desktop.
A part of capacitor plugin implementation for line login on iOS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import Capacitor | |
import LineSDK | |
/** | |
* Please read the Capacitor iOS Plugin Development Guide | |
* here: https://capacitor.ionicframework.com/docs/plugins/ios | |
*/ | |
@objc(LineLogin) | |
public class LineLogin: CAPPlugin { | |
struct ErrorCode { | |
static let ParameterError = -1 | |
static let SDKError = -2 | |
} | |
@objc func echo(_ call: CAPPluginCall) { | |
let value = call.getString("value") ?? "" | |
call.success([ | |
"value": value | |
]) | |
} | |
@objc func initialize(_ call: CAPPluginCall) { | |
let params = call.options[0] as AnyObject | |
guard let channelID = params["channel_id"] as? String else { | |
self.parameterError(call: call, description: "channel_id is required") | |
return | |
} | |
// This is things on LINE sdk working... | |
LoginManager.shared.setup(channelID: channelID, universalLinkURL: nil) | |
// let result = CDVPluginResult(status: CDVCommandStatus_OK) | |
// commandDelegate.send(result, callbackId:command.callbackId) | |
call.resolve([ | |
"status": "ok" | |
]) | |
} | |
func _login(_ call: CAPPluginCall, options: LoginManagerOptions) { | |
LoginManager.shared.login(permissions: [.profile, .openID, .email], in: self.viewController, options: options) { | |
result in | |
switch result { | |
case .success(let loginResult): | |
var data = ["userID":nil, "displayName":nil, "pictureURL":nil, "email":nil] as [String : Any?] | |
data.updateValue(loginResult.userProfile?.displayName, forKey: "displayName") | |
data.updateValue(loginResult.userProfile?.userID, forKey: "userID") | |
data.updateValue(loginResult.accessToken.IDToken?.payload.email, forKey: "email") | |
if let pictureURL = loginResult.userProfile?.pictureURL { | |
data.updateValue(String(describing: pictureURL), forKey: "pictureURL") | |
} | |
// let result = CDVPluginResult(status: CDVCommandStatus_OK, messageAs:data as [AnyHashable : Any]) | |
// self.commandDelegate.send(result, callbackId:command.callbackId) | |
call.resolve([ | |
"status":"ok", | |
"data": data as [AnyHashable : Any] | |
]) | |
case .failure(let error): | |
self.sdkError(call: call, error: error) | |
} | |
} | |
} | |
@objc func loginWeb(_ call: CAPPluginCall) { | |
self._login(call, options: LoginManagerOptions.init(rawValue: LoginManagerOptions.onlyWebLogin.rawValue)) | |
} | |
@objc func login(_ call: CAPPluginCall) { | |
self._login(call, options: LoginManagerOptions()) | |
} | |
@objc func logout(_ call: CAPPluginCall) { | |
LoginManager.shared.logout { result in | |
switch result { | |
case .success: | |
// let result = CDVPluginResult(status: CDVCommandStatus_OK) | |
// self.commandDelegate.send(result, callbackId:command.callbackId) | |
call.resolve([ | |
"status":"ok" | |
]) | |
case .failure(let error): | |
self.sdkError(call: call, error: error) | |
} | |
} | |
} | |
@objc func getAccessToken(_ call: CAPPluginCall) { | |
guard let currentAccessToken = AccessTokenStore.shared.current else { | |
call.reject('error', nil); | |
return | |
} | |
let data = ["accessToken":currentAccessToken.value, "expireTime":currentAccessToken.expiresAt.timeIntervalSince1970] as [String : Any?] | |
call.resolve([ | |
"status" : "ok" | |
"data": data as [AnyHashable : Any] | |
]) | |
} | |
@objc func verifyAccessToken(_ call: CAPPluginCall) { | |
API.verifyAccessToken { (result) in | |
switch result { | |
case .success( _): | |
call.resolve([ | |
"status" : "ok" | |
]) | |
case .failure(let error): | |
self.sdkError(call: call, error: error) | |
} | |
} | |
} | |
@objc func refreshAccessToken(_ call: CAPPluginCall) { | |
API.refreshAccessToken { (result) in | |
switch result { | |
case .success(let accessToken): | |
let data = ["accessToken":accessToken.value, "expireTime":accessToken.expiresAt.timeIntervalSince1970] as [String : Any?] | |
call.resolve([ | |
"status" : "ok" | |
"data": data as [AnyHashable : Any] | |
]) | |
case .failure(let error): | |
self.sdkError(call: call, error: error) | |
} | |
} | |
} | |
private func parameterError(call: CAPPluginCall, description: String) { | |
let err = ["code":ErrorCode.ParameterError, "description": description] as [AnyHashable : Any] | |
call.reject(description, err); | |
} | |
private func sdkError(call: CAPPluginCall, error: LineSDKError) { | |
let description = error.errorDescription ?? "" | |
let err = ["code":ErrorCode.SDKError, "sdkErrorCode":error.errorCode, "description": description] as [AnyHashable : Any] | |
call.reject(description, err); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment