UIDocumentInteractionControllerDelegate EXC_BAD_ACCESS in Swift

608 views Asked by At

I'm currently trying to open a file with another application using an instance of UIDocumentInteractionController. Everything works fine (e.g. showing the options dialog, opening the application) but when the app switch actually happens I receive a EXC_BAD_ACCESS instead of the didFinishSendingToApplication: delegate callback. Before I ported this code to Swift from Objective-C everything was working fine. Any ideas whats is going wrong here?

self.documentInteractionController = UIDocumentInteractionController(URL: self.fileURL)
self.documentInteractionController!.UTI = "net.whatsapp.movie"
self.documentInteractionController!.delegate = self

Here is all of the stack trace I can get (the crash actually happens in the main function so I can't get anything more...):

* thread #1: tid = 0x12ef7e, 0x0000000182dd18c8 CoreFoundation`CFStringCreateCopy + 36, queue = 'com.apple.main-thread', stopreason = EXC_BAD_ACCESS (code=1, address=0x0)
* frame #0: 0x0000000182dd18c8 CoreFoundation`CFStringCreateCopy + 36
frame #1: 0x0000000101df09d8 libswiftCore.dylib`function signature specialization <Arg[0] = Owned To Guaranteed, Arg[1] = Dead> of Swift.String.init (Swift.String.Type)(_cocoaString : Swift.AnyObject) -> Swift.String + 108
frame #2: 0x0000000101dd45b0 libswiftCore.dylib`Swift.String.init (Swift.String.Type)(_cocoaString : Swift.AnyObject) -> Swift.String + 24
frame #3: 0x0000000100253814 MyApp`@objc MyApp.MyViewController.documentInteractionController (MyApp.MyViewController)(ObjectiveC.UIDocumentInteractionController, didEndSendingToApplication : Swift.String) -> () + 84 at MyViewController.swift:0
frame #4: 0x0000000183dedf9c Foundation`__NSThreadPerformPerform + 372
frame #5: 0x0000000182ea4240 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
frame #6: 0x0000000182ea34e4 CoreFoundation`__CFRunLoopDoSources0 + 264
frame #7: 0x0000000182ea1594 CoreFoundation`__CFRunLoopRun + 712
frame #8: 0x0000000182dcd2d4 CoreFoundation`CFRunLoopRunSpecific + 396
frame #9: 0x000000018c5e36fc GraphicsServices`GSEventRunModal + 168
frame #10: 0x0000000187992fac UIKit`UIApplicationMain + 1488
frame #11: 0x00000001000c6374 MyApp`main(argc=1, argv=0x000000016fdf79c0) + 124 at main.m:33
frame #12: 0x0000000194d8ea08 libdyld.dylib`start + 4
2

There are 2 answers

0
Jorge Vasquez On BEST ANSWER

The real issue is that the delegate signature is wrong on the SDK. Instead of

documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String)

you should use

documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String?)

Please not the last ?, which says the application parameter can be nil, which it always is.

You should do the same to the willBeginSendingToApplication call, as it too can have a nil application parameter.

It's safe to ignore the warnings complaining about different optionality than expected by the protocol.

0
Wilkin Ho On

I met the same problem and spent days to try to fix this problem.

I finally found that the root cause of this error is from implementing func documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String) in the class.

I removed this function and use willBeginSendingToApplication instead and the app will not crash on app switching.

Tested on real device running iOS 7.1 and iOS 8.3. FYI