UIDocumentInteractionControllerDelegate EXC_BAD_ACCESS in Swift
I am currently trying to open a file with another application using a UIDocumentInteractionController instance. Everything works fine (for example, showing the options dialog while opening the app), but when actually going to the app, I get EXC_BAD_ACCESS instead of the didFinishSendingToApplication: delegate callback. Before porting this code to Swift from Objective-C everything works fine. Any ideas what is wrong here?
self.documentInteractionController = UIDocumentInteractionController(URL: self.fileURL)
self.documentInteractionController!.UTI = "net.whatsapp.movie"
self.documentInteractionController!.delegate = self
Here is all the stack trace I can get (the crash actually happens in the main function, so I can't get anything else ...):
* 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
source to share
The real problem is that the delegate signature is incorrect in the SDK. Instead
documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String)
you must use
documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String?)
Please not the last ?
one that says that the application parameter can be nil
, which it always is.
You have to do the same with the call willBeginSendingToApplication
, as it can also have an application parameter nil
.
It is safe to ignore warnings complaining about different optionality than expected in the protocol.
source to share
I faced the same problem and spent days trying to fix this issue.
Finally, I found that the main reason for this error is the implementation func documentInteractionController(controller: UIDocumentInteractionController, didEndSendingToApplication application: String)
in the class.
I removed this feature and used instead willBeginSendingToApplication
and the app won't crash when switching apps.
Tested on a real device running iOS 7.1 and iOS 8.3. FYI
source to share