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.

155 lines
4.7 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. /// A dialog for sharing content on Facebook.
  20. public final class ShareDialog<Content: ContentProtocol> {
  21. fileprivate let sdkSharer: FBSDKShareDialog
  22. fileprivate let sdkShareDelegate: SDKSharingDelegateBridge<Content>
  23. /**
  24. A `UIViewController` to present the dialog from.
  25. If not specified, the top most view controller will be automatically determined as best as possible.
  26. */
  27. public var presentingViewController: UIViewController? {
  28. get {
  29. return sdkSharer.fromViewController
  30. }
  31. set {
  32. sdkSharer.fromViewController = newValue
  33. }
  34. }
  35. /**
  36. The mode with which to display the dialog.
  37. Defaults to `.Automatic`, which will automatically choose the best available mode.
  38. */
  39. public var mode: ShareDialogMode {
  40. get {
  41. return ShareDialogMode(sdkShareMode: sdkSharer.mode)
  42. }
  43. set {
  44. sdkSharer.mode = newValue.sdkShareMode
  45. }
  46. }
  47. /**
  48. Create a `ShareDialog` with a given content.
  49. - parameter content: The content to share.
  50. */
  51. public init(content: Content) {
  52. sdkSharer = FBSDKShareDialog()
  53. sdkShareDelegate = SDKSharingDelegateBridge<Content>()
  54. sdkShareDelegate.setupAsDelegateFor(sdkSharer)
  55. sdkSharer.shareContent = ContentBridger.bridgeToObjC(content)
  56. }
  57. }
  58. extension ShareDialog: ContentSharingProtocol {
  59. /// The content that is being shared.
  60. public var content: Content {
  61. get {
  62. guard let swiftContent: Content = ContentBridger.bridgeToSwift(sdkSharer.shareContent) else {
  63. fatalError("Content of our private share dialog has changed type. Something horrible has happened.")
  64. }
  65. return swiftContent
  66. }
  67. }
  68. /// The completion handler to be invoked upon the share performing.
  69. public var completion: ((ContentSharerResult<Content>) -> Void)? {
  70. get {
  71. return sdkShareDelegate.completion
  72. }
  73. set {
  74. sdkShareDelegate.completion = newValue
  75. }
  76. }
  77. /// Whether or not this sharer fails on invalid data.
  78. public var failsOnInvalidData: Bool {
  79. get {
  80. return sdkSharer.shouldFailOnDataError
  81. }
  82. set {
  83. sdkSharer.shouldFailOnDataError = newValue
  84. }
  85. }
  86. /**
  87. Validates the content on the receiver.
  88. - throws: If The content could not be validated.
  89. */
  90. public func validate() throws {
  91. try sdkSharer.validate()
  92. }
  93. }
  94. extension ShareDialog: ContentSharingDialogProtocol {
  95. /**
  96. Shows the dialog.
  97. - throws: If the dialog cannot be presented.
  98. */
  99. public func show() throws {
  100. var error: Error?
  101. let completionHandler = sdkShareDelegate.completion
  102. sdkShareDelegate.completion = {
  103. if case .failed(let resultError) = $0 {
  104. error = resultError
  105. }
  106. }
  107. sdkSharer.show()
  108. sdkShareDelegate.completion = completionHandler
  109. if let error = error {
  110. throw error
  111. }
  112. }
  113. }
  114. extension ShareDialog {
  115. /**
  116. Convenience method to create and show a `ShareDialog` with a `fromViewController`, `content`, and `completion`.
  117. - parameter viewController: The viewController to present the dialog from.
  118. - parameter content: The content to share.
  119. - parameter completion: The completion handler to invoke.
  120. - returns: The `ShareDialog` that has been presented.
  121. - throws: If the dialog fails to validate.
  122. */
  123. @discardableResult
  124. public static func show(from viewController: UIViewController,
  125. content: Content,
  126. completion: ((ContentSharerResult<Content>) -> Void)? = nil) throws -> Self {
  127. let shareDialog = self.init(content: content)
  128. shareDialog.presentingViewController = viewController
  129. shareDialog.completion = completion
  130. try shareDialog.show()
  131. return shareDialog
  132. }
  133. }