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.

112 lines
3.5 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 FBSDKShareKit
  19. extension GameRequest {
  20. /// A dialog for sending game requests.
  21. public final class Dialog {
  22. fileprivate let sdkDialog: FBSDKGameRequestDialog
  23. fileprivate let sdkDelegate: SDKDelegate
  24. /// The content for the game request.
  25. public let request: GameRequest
  26. /// The completion handler to be invoked upon completion of the request.
  27. public var completion: ((Result) -> Void)? {
  28. didSet {
  29. sdkDelegate.completion = completion
  30. }
  31. }
  32. /// Specifies whether frictionless requests are enabled.
  33. public var frictionlessRequestsEnabled: Bool {
  34. get {
  35. return sdkDialog.frictionlessRequestsEnabled
  36. }
  37. set {
  38. sdkDialog.frictionlessRequestsEnabled = false
  39. }
  40. }
  41. /**
  42. Create a game request dialog with a given request.
  43. - parameter request: The game request to send.
  44. */
  45. public init(request: GameRequest) {
  46. self.request = request
  47. sdkDialog = FBSDKGameRequestDialog()
  48. sdkDelegate = SDKDelegate()
  49. sdkDelegate.setupAsDelegateFor(sdkDialog)
  50. sdkDialog.content = request.sdkContentRepresentation
  51. }
  52. /**
  53. Begins the game request from the receiver.
  54. - throws: If the dialog fails to be presented.
  55. */
  56. public func show() throws {
  57. var error: Error?
  58. let completionHandler = sdkDelegate.completion
  59. sdkDelegate.completion = {
  60. if case .failed(let resultError) = $0 {
  61. error = resultError
  62. }
  63. }
  64. sdkDialog.show()
  65. sdkDelegate.completion = completionHandler
  66. if let error = error {
  67. throw error
  68. }
  69. }
  70. /**
  71. Validates the content on the receiver.
  72. - throws: If an error occurs during validation.
  73. */
  74. public func validate() throws {
  75. return try sdkDialog.validate()
  76. }
  77. }
  78. }
  79. extension GameRequest.Dialog {
  80. /**
  81. Convenience method to build and show a game request dialog.
  82. - parameter request: The request to send.
  83. - parameter completion: The completion handler to be invoked upon completion of the request.
  84. - returns: The dialog instance that has been shown.
  85. - throws: If the dialog fails to be presented.
  86. */
  87. @discardableResult
  88. public static func show(_ request: GameRequest, completion: ((GameRequest.Result) -> Void)?) throws -> Self {
  89. let dialog = self.init(request: request)
  90. dialog.completion = completion
  91. try dialog.show()
  92. return dialog
  93. }
  94. }