You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.8 KiB

6 years ago
  1. // Copyright (c) 2016-present, Facebook, Inc. All rights reserved.
  2. //
  3. // You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
  4. // copy, modify, and distribute this software in source code or binary form for use
  5. // in connection with the web services and APIs provided by Facebook.
  6. //
  7. // As with any software that integrates with the Facebook platform, your use of
  8. // this software is subject to the Facebook Developer Principles and Policies
  9. // [http://developers.facebook.com/policy/]. This copyright notice shall be
  10. // included in all copies or substantial portions of the software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  14. // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  15. // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  16. // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  17. // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  18. import Foundation
  19. import UIKit
  20. import FBSDKShareKit
  21. extension AppInvite {
  22. /// A dialog to send app invites.
  23. public final class Dialog {
  24. fileprivate let sdkDialog: FBSDKAppInviteDialog
  25. fileprivate let sdkDelegate: SDKDelegate
  26. /// The invite to send.
  27. public let invite: AppInvite
  28. /**
  29. A UIViewController to present the dialog from.
  30. If not specified, the top most view controller will be automatically determined as best as possible.
  31. */
  32. public var presentingViewController: UIViewController? {
  33. get {
  34. return sdkDialog.fromViewController
  35. }
  36. set {
  37. sdkDialog.fromViewController = newValue
  38. }
  39. }
  40. /// The completion handler to be invoked upon showing the dialog.
  41. public var completion: ((Result) -> Void)? {
  42. get {
  43. return sdkDelegate.completion
  44. }
  45. set {
  46. sdkDelegate.completion = newValue
  47. }
  48. }
  49. /**
  50. Create a dialog with an invite.
  51. - parameter invite: The invite to send.
  52. */
  53. public init(invite: AppInvite) {
  54. sdkDialog = FBSDKAppInviteDialog()
  55. sdkDialog.content = invite.sdkInviteRepresentation
  56. sdkDelegate = SDKDelegate()
  57. sdkDelegate.setupAsDelegateFor(sdkDialog)
  58. self.invite = invite
  59. }
  60. /**
  61. Attempt to show the dialog modally.
  62. - throws: If the dialog fails to present.
  63. */
  64. public func show() throws {
  65. var error: Error?
  66. let completionHandler = sdkDelegate.completion
  67. sdkDelegate.completion = {
  68. if case .failed(let resultError) = $0 {
  69. error = resultError
  70. }
  71. }
  72. sdkDialog.show()
  73. sdkDelegate.completion = completionHandler
  74. if let error = error {
  75. throw error
  76. }
  77. }
  78. /**
  79. Validates the contents of the reciever as valid.
  80. - throws: If the content is invalid.
  81. */
  82. public func validate() throws {
  83. try sdkDialog.validate()
  84. }
  85. }
  86. }
  87. extension AppInvite.Dialog {
  88. /**
  89. Convenience method to show a `Dialog` with a `presentingViewController`, `invite`, and `completion`.
  90. - parameter viewController: The view controller to present from.
  91. - parameter invite: The invite to send.
  92. - parameter completion: The completion handler to invoke upon success.
  93. - throws: If the dialog fails to present.
  94. - returns: The dialog that has been presented.
  95. */
  96. @discardableResult
  97. public static func show(from viewController: UIViewController,
  98. invite: AppInvite,
  99. completion: ((AppInvite.Result) -> Void)? = nil) throws -> Self {
  100. let dialog = self.init(invite: invite)
  101. dialog.presentingViewController = viewController
  102. dialog.completion = completion
  103. try dialog.show()
  104. return dialog
  105. }
  106. }