LineSDKLoginButton.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // LineSDKLoginButton.swift
  3. //
  4. // Copyright (c) 2016-present, LY Corporation. All rights reserved.
  5. //
  6. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  7. // copy and distribute this software in source code or binary form for use
  8. // in connection with the web services and APIs provided by LY Corporation.
  9. //
  10. // As with any software that integrates with the LY Corporation platform, your use of this software
  11. // is subject to the LINE Developers Agreement [http://terms2.line.me/LINE_Developers_Agreement].
  12. // This copyright notice shall be included in all copies or substantial portions of the software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  15. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18. // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. @objc public protocol LineSDKLoginButtonDelegate: AnyObject {
  22. func loginButtonDidStartLogin(_ button: LineSDKLoginButton)
  23. func loginButton(_ button: LineSDKLoginButton, didSucceedLogin loginResult: LineSDKLoginResult?)
  24. func loginButton(_ button: LineSDKLoginButton, didFailLogin error: Error?)
  25. }
  26. @objcMembers
  27. public class LineSDKLoginButton: NSObject {
  28. public var button: UIButton { return _binaryCompatibleButton }
  29. // A wrapper for providing a binary-compatible version of the SDK.
  30. private var _binaryCompatibleButton: LoginButton
  31. @objc public enum LineSDKLoginButtonSize: Int {
  32. case small
  33. case normal
  34. init(_ value: LoginButton.ButtonSize) {
  35. switch value {
  36. case .normal: self = .normal
  37. case .small: self = .small
  38. }
  39. }
  40. var unwrapped: LoginButton.ButtonSize {
  41. switch self {
  42. case .small: return .small
  43. case .normal: return .normal
  44. }
  45. }
  46. }
  47. public weak var loginDelegate: LineSDKLoginButtonDelegate?
  48. public weak var buttonPresentingViewController: UIViewController?
  49. public var loginPermissions: Set<LineSDKLoginPermission> = [.profile]
  50. public var loginManagerParameters = LineSDKLoginManagerParameters()
  51. public var buttonSizeValue: LineSDKLoginButtonSize {
  52. set {
  53. _binaryCompatibleButton.buttonSize = newValue.unwrapped
  54. }
  55. get {
  56. return LineSDKLoginButtonSize(_binaryCompatibleButton.buttonSize)
  57. }
  58. }
  59. public var buttonTextValue: String? {
  60. set {
  61. _binaryCompatibleButton.buttonText = newValue
  62. }
  63. get {
  64. return _binaryCompatibleButton.buttonText
  65. }
  66. }
  67. public override init() {
  68. _binaryCompatibleButton = LoginButton()
  69. }
  70. public func login() {
  71. LineSDKLoginManager.sharedManager.login(
  72. permissions: loginPermissions,
  73. inViewController: buttonPresentingViewController,
  74. parameters: loginManagerParameters
  75. ) {
  76. result, error in
  77. if error == nil {
  78. self.loginDelegate?.loginButton(self, didSucceedLogin: result)
  79. } else {
  80. self.loginDelegate?.loginButton(self, didFailLogin: error)
  81. }
  82. }
  83. self.loginDelegate?.loginButtonDidStartLogin(self)
  84. }
  85. // MARK: - Deprecated
  86. /// - Warning: Deprecated. Use `loginManagerParameters` instead.
  87. ///
  88. @available(*, deprecated, message: "Use `LineSDKLoginButton.loginManagerParameters` instead.")
  89. public var loginManagerOptions: [LineSDKLoginManagerOptions]?
  90. }