简体   繁体   English

iOS Swift Swift SDK共享对话框永不成功

[英]ios facebook swift sdk share dialog never return success

I'm using the swift SDK installed through cocoapods. 我正在使用通过cocoapods安装的swift SDK。 What I want is a simple share function, with different message alerts when different results returned, such as .cancelled or .success. 我想要的是一个简单的共享功能,当返回不同的结果(例如.cancelled或.success)时,具有不同的消息警报。

Sharing part is fine, I could post content onto my facebook, but it stays blank after posting and won't return success. 共享部分很好,我可以将内容发布到我的Facebook上,但是发布后它保持空白,不会返回成功。 The only way to exit the blank screen is to click done button on the top left but that will return cancelled. 退出黑屏的唯一方法是单击左上角的完成按钮,但返回取消。 I need to show a different message when it's actually posted successfully. 实际成功发布后,我需要显示其他消息。

Here's my code: 这是我的代码:

let url=URL(string: imageUrl as String)
let content = LinkShareContent(url: url!)
let shareDialog = ShareDialog(content: content)
shareDialog.presentingViewController = self
shareDialog.mode = .browser
shareDialog.failsOnInvalidData = true
shareDialog.completion = { result in
  // Handle share results
  switch result{
  case .success:
  self.view.showToast("Shared successfully", position: .bottom, popTime: 1, dismissOnTap: false)
  case .failed:
  self.view.showToast("Shared failed", position: .bottom, popTime: 1, dismissOnTap: false)
  case .cancelled:
  self.view.showToast("Shared cancelled", position: .bottom, popTime: 1, dismissOnTap: false)
  }
}
try? shareDialog.show()

When I actually cancel the post it gives expected alert message so that part is correct. 当我实际取消发布时,它会发出预期的警报消息,因此该部分正确。 I tried to use .sharesheet mode and in that mode the dialog will close itself, return success and give correct alert message after posting content. 我尝试使用.sharesheet模式,在该模式下,对话框将自行关闭,返回成功,并在发布内容后给出正确的警报消息。

So my question here is, is there something wrong with my code that it couldn't return success? 所以我的问题是,我的代码有什么问题,它无法返回成功? or it's the .browser mode? 还是.browser模式? In either case, how to make it close automatically after posting and get the success return? 无论哪种情况,如何使它在发布后自动关闭并获得成功回报?

Thank you guys. 感谢大伙们。

You can use delegation method to get the share status call back (FBSDKSharing delegates methods) iedidCompleteWithResults, didFailWithError, sharerDidCancel. 您可以使用委派方法来获取共享状态回叫(FBSDKSharing委托方法)iedidCompleteWithResults,didFailWithError,sharerDidCancel。 Please refer their link 请参考他们的链接

So I figured it out, basically you just need to add a few lines in the AppDelegate file as in the quickstart of Facebook SDK for ios. 所以我想通了,基本上,您只需要在AppDelegate文件中添加几行,就像iOS版Facebook SDK的快速入门中一样。

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [[FBSDKApplicationDelegate sharedInstance] application:application
                       didFinishLaunchingWithOptions:launchOptions];
  return YES;
}

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {
  return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                     openURL:url
                                           sourceApplication:sourceApplication
                                                  annotation:annotation];
}

They didn't give out a swift version of quickstart so I didn't know you need to modify the AppDelegate. 他们没有给出快速入门的快速版本,所以我不知道您需要修改AppDelegate。 Anyway here's the swift code and don't forget to import FBSDKCoreKit in AppDelegate 无论如何,这是快速的代码,不要忘记在AppDelegate中导入FBSDKCoreKit

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    FBSDKAppEvents.activateApp()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM