简体   繁体   中英

ios facebook swift sdk share dialog never return success

I'm using the swift SDK installed through cocoapods. What I want is a simple share function, with different message alerts when different results returned, such as .cancelled or .success.

Sharing part is fine, I could post content onto my facebook, but it stays blank after posting and won't return success. 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.

So my question here is, is there something wrong with my code that it couldn't return success? or it's the .browser mode? 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. 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.

- (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. Anyway here's the swift code and don't forget to import FBSDKCoreKit in AppDelegate

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()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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