Trouble getting Photos editing extension up and running

I decided to start experimenting with the new Photos editing extension stuff that's now in Xcode 7 beta 2, but have been having trouble getting even a basic extension to load properly in Photos. I started out by creating a new project, then adding a new target using the "Photo Editing Extension" template. I read the part in the release notes about the template still having iOS class names (e.g. UIViewController), so I replaced that stuff and was able to get the extension to compile just fine. The extension does get listed in Photos (and the Extensions pane in System Preferences), but I run into trouble when trying to actually load the extension from within Photos.


Initially, I would get an exception thrown somewhere in the exension internals, with a stack trace looking like this:


Application Specific Information:
Crashing on exception: *** setObjectForKey: object cannot be nil (key: <__NSConcreteUUID 0x7fc7b20210d0> 6B0D50C3-FB4F-4AB5-B90D-022ADE140FAF)


Application Specific Backtrace 1:
0   CoreFoundation                      0x00007fff8e956a55 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00007fff9689313a objc_exception_throw + 48
2   CoreFoundation                      0x00007fff8e834cc7 -[__NSDictionaryM setObject:forKey:] + 2071
3   libextension.dylib                  0x00007fff94d460b2 -[_NSExtensionContextVendor _setPrincipalObject:forUUID:] + 123
4   libextension.dylib                  0x00007fff94d457d5 __105-[_NSExtensionContextVendor _beginRequestWithExtensionItems:listenerEndpoint:withContextUUID:completion:]_block_invoke + 861
5   libdispatch.dylib                   0x00007fff8e0bd814 _dispatch_call_block_and_release + 12
6   libdispatch.dylib                   0x00007fff8e0b9765 _dispatch_client_callout + 8
7   libdispatch.dylib                   0x00007fff8e0cad26 _dispatch_main_queue_callback_4CF + 1677
8   CoreFoundation                      0x00007fff8e89f079 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
9   CoreFoundation                      0x00007fff8e85af94 __CFRunLoopRun + 1908
10  CoreFoundation                      0x00007fff8e85a5b8 CFRunLoopRunSpecific + 296
11  HIToolbox                           0x00007fff94684ce9 RunCurrentEventLoopInMode + 235
12  HIToolbox                           0x00007fff94684a7f ReceiveNextEventCommon + 432
13  HIToolbox                           0x00007fff946848bf _BlockUntilNextEventMatchingListInModeWithFilter + 71
14  AppKit                              0x00007fff97a2c732 _DPSNextEvent + 927
15  AppKit                              0x00007fff97df9f74 -[NSApplication _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 324
16  ViewBridge                          0x00007fff8f22f531 -[NSViewServiceApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 73
17  AppKit                              0x00007fff97a226c2 -[NSApplication run] + 682
18  AppKit                              0x00007fff979a4c4f NSApplicationMain + 1176
19  libxpc.dylib                        0x00007fff8dc951d0 _xpc_objc_main + 793
20  libxpc.dylib                        0x00007fff8dc9691f _xpc_main_listener_event + 0
21  Foundation                          0x00007fff95c60010 +[NSXPCListener serviceListener] + 0
22  PlugInKit                           0x00007fff98a56e96 -[PKService run] + 493
23  PlugInKit                           0x00007fff98a56b77 +[PKService main] + 55
24  PlugInKit                           0x00007fff98a56ed8 +[PKService _defaultRun:arguments:] + 17
25  libextension.dylib                  0x00007fff94d4faaa NSExtensionMain + 51
26  libdyld.dylib                       0x00007fff96c505ad start + 1
27  ???                                 0x0000000000000001 0x0 + 1


I did some poking around in the Info.plist for the extension, and noticed that it had an NSExtensionPrincipalClass key specified, with the NSViewController subclass that was created by the template. The app extension docs (for iOS, since they haven't been updated with Mac info yet) state that you should use the NSExtensionMainStoryboard key instead. So, I created a storyboard containing one scene that uses the same view controller subclass, with that controller set as the initial view controller. That seemed to make a little progress, as I no longer got the error above, but instead get an exception that looks like this:

Application Specific Information:
Crashing on exception: addChildViewController: called with a nil childViewController


Application Specific Backtrace 1:
0   CoreFoundation                      0x00007fff8e956a55 __exceptionPreprocess + 165
1   libobjc.A.dylib                     0x00007fff9689313a objc_exception_throw + 48
2   CoreFoundation                      0x00007fff8e95682a +[NSException raise:format:arguments:] + 106
3   Foundation                          0x00007fff95c7b1bc -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 198
4   AppKit                              0x00007fff97b712ef -[NSViewController insertChildViewController:atIndex:] + 157
5   PhotosUI                            0x00000001028923e9 -[PHEditingViewService setupWithController:] + 84
6   PhotosUI                            0x0000000102892368 -[PHEditingViewService connectToContextWithSessionID:withCompletionHandler:] + 182
7   CoreFoundation                      0x00007fff8e82550c __invoking___ + 140
8   CoreFoundation                      0x00007fff8e82535e -[NSInvocation invoke] + 286
9   CoreFoundation                      0x00007fff8e8c3d36 -[NSInvocation invokeWithTarget:] + 54
10  ViewBridge                          0x00007fff8f27d44b __68-[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:]_block_invoke + 47
11  ViewBridge                          0x00007fff8f287d0d __runAnimationGroup_block_invoke + 21
12  AppKit                              0x00007fff979d5e97 +[NSAnimationContext runAnimationGroup:completionHandler:] + 82
13  ViewBridge                          0x00007fff8f287ce9 runAnimationGroup + 317
14  ViewBridge                          0x00007fff8f27cf0b +[NSVB_View _animateWithAttributes:animations:completion:] + 531
15  ViewBridge                          0x00007fff8f27d3bc -[NSVB_ViewServiceImplicitAnimationDecodingProxy forwardInvocation:] + 183
...and a lot more internal gunk


It seems like it's not able to instantiate the view controller, but at this point, I'm stumped as to exactly why that might be. Anyone had any better luck with this?

I am almost at the same road block. I can see my extension under system preferences, I can select it and then nothing....the view control is just not there!

Either the teamplate is broken or the documentation is lacking of updated information.

Let me know what you find out and if you want to collaborate further.

I was able to resolve the issue and now I can see my NSView via PhotoEditingViewController.xib

The problem was the application template and not the extension one.

Cool, what did you have to change in the application template to get things running?

I had to start all over and created a blank app with the simplest app template. I can share my code via email if you like.

Sure, if you wouldn't mind sending it to bwebster at mac dot com, I'd love to take a look and see what the difference is.

I finally figured out what the heck was going on here. Turns out the difference was that the project I had created was using Swift. I tried creating an identical project using Objective-C instead, and everything worked as expected. The reason was that the extension mechanism is looking up the NSViewController subclass to instantiate by name, based on the NSExtensionPrincipalClass value in the extension's Info.plist. However, when dealing with a Swift class, the module name needs to be included in order for the Objective-C runtime to be able to find it. Changing the NSExtensionPrincipalClass value from "PhotoEditingViewController" to "MyExtension.PhotoEditingViewController" (where "MyExtension" is the name of the extension target), then it's able to find the Swift class and the extension loads properly in Photos. I've filed this with Apple as bug 21738629.

It can also (bizarrely) be caused by omitting the (empty) NSExtensionAttributes dictionary, FYI.

Nope. False alarm. Still broken for me. Turns out iOS stops saving crash logs after an app has crashed 25 times in the same spot. Result: No idea that it is still crashing, but it is still crashing. Blech.

Trouble getting Photos editing extension up and running
 
 
Q