Search code examples
swiftexc-bad-accessappdelegatefbsdk

FBSDKShareButton successfully posting link, but crashing on return to app


Here's the function I'm calling:

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

This is the function I have in 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
}

When I comment out the above function, the crash goes away but the window doesn't close. Interestingly, if I put a print statement at the beginning of the function, it doesn't get called, so the app is crashing simply from trying to call the function.

Here's the error output:

* 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

Solution

  • Try to replace this:

    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
    

    }

    with

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

    Hope this will solve your problem.