FBSDKShareButton 成功发布链接,但返回应用程序时崩溃

Posted

技术标签:

【中文标题】FBSDKShareButton 成功发布链接,但返回应用程序时崩溃【英文标题】:FBSDKShareButton successfully posting link, but crashing on return to app 【发布时间】:2018-04-14 07:28:53 【问题描述】:

这是我正在调用的函数:

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

当我注释掉上述函数时,崩溃消失了,但窗口没有关闭。有趣的是,如果我在函数的开头放置一个 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

【问题讨论】:

【参考方案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])

希望这能解决您的问题。

【讨论】:

工作就像一个魅力!

以上是关于FBSDKShareButton 成功发布链接,但返回应用程序时崩溃的主要内容,如果未能解决你的问题,请参考以下文章

FBSDKShareButton 即使在用户登录后也被禁用?

我想创建一个 url,将链接指向我在 django 中的视图中的登录功能 我尝试了一切,但没有成功

使用 Makefile 编译失败但在命令行中成功

Swift - 直接在 facebook 上发布

构建成功,但尝试运行程序时出现 GLEW 错误

静态链接 libtcod