FBSDKShareButton 成功发布 link,但在 return 时崩溃到应用程序

FBSDKShareButton successfully posting link, but crashing on return to app

这是我正在调用的函数:

func showFBInviteView() {
    let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
    let safeURL = "http://mylink.com"
    content.contentURL = URL(string: safeURL)

    let button : FBSDKShareButton = FBSDKShareButton()  
    button.shareContent = content
    button.sendActions(for: .touchUpInside)  
}

这是我在 AppDelegate 中的功能:

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

    let handled = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)

    return  handled
}

当我注释掉上面的函数时,崩溃消失了,但 window 没有关闭。有趣的是,如果我在函数的开头放置一个 print 语句,它不会被调用,所以应用程序只是因为尝试调用该函数而崩溃。

这是错误输出:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x0000000118f072cf libswiftCore.dylib`swift_getObjectType + 47
    frame #1: 0x000000010fe1ac08 MyApp`@objc AppDelegate.application(_:open:sourceApplication:annotation:) at AppDelegate.swift:0
    frame #2: 0x0000000113b8b33f UIKit`__58-[UIApplication _applicationOpenURLAction:payload:origin:]_block_invoke + 1037
    frame #3: 0x0000000113b8ac31 UIKit`-[UIApplication _applicationOpenURLAction:payload:origin:] + 652
    frame #4: 0x0000000116a55cb8 SafariServices`-[SFSafariViewController remoteViewController:hostApplicationOpenURL:] + 205
    frame #5: 0x0000000116a47c28 SafariServices`-[SFBrowserRemoteViewController willOpenURLInHostApplication:] + 68
    frame #6: 0x0000000118491ccc CoreFoundation`__invoking___ + 140
    frame #7: 0x0000000118491b84 CoreFoundation`-[NSInvocation invoke] + 308
    frame #8: 0x0000000119800848 libdispatch.dylib`_dispatch_client_callout + 8
    frame #9: 0x0000000119805e14 libdispatch.dylib`_dispatch_block_invoke_direct + 592
    frame #10: 0x000000011ee9a470 FrontBoardServices`__FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK__ + 24
    frame #11: 0x000000011ee9a12e FrontBoardServices`-[FBSSerialQueue _performNext] + 439
    frame #12: 0x000000011ee9a68e FrontBoardServices`-[FBSSerialQueue _performNextFromRunLoopSource] + 45
    frame #13: 0x00000001184b0bb1 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #14: 0x00000001184954af CoreFoundation`__CFRunLoopDoSources0 + 271
    frame #15: 0x0000000118494a6f CoreFoundation`__CFRunLoopRun + 1263
    frame #16: 0x000000011849430b CoreFoundation`CFRunLoopRunSpecific + 635
    frame #17: 0x000000011ada1a73 GraphicsServices`GSEventRunModal + 62
    frame #18: 0x0000000113b820b7 UIKit`UIApplicationMain + 159
  * frame #19: 0x000000010fe1c887 MyApp`main at AppDelegate.swift:18
    frame #20: 0x000000011987d955 libdyld.dylib`start + 1
    frame #21: 0x000000011987d955 libdyld.dylib`start + 1

尝试替换为:

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

let handled = FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation)

return  handled

}

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    return FBSDKApplicationDelegate.sharedInstance().application(app,
                                                                 open: url,
                                                                 sourceApplication: options[.sourceApplication] as! String,
                                                                 annotation: options[.annotation])
}

希望这能解决您的问题。