29

Does anyone know of a good way to write an iOS 8 share extension without the MainInterface.storyboard that comes with the template?

When I delete the file or remove the NSExtensionMainStoryboard from Info.plist, the extension stops working (nothing happens when you tap on the button in the share pane). We tried replacing NSExtensionMainStoryboard with NSExtensionPrincipalClass which also didn't work.

Any ideas?

Andrew
  • 15,357
  • 6
  • 66
  • 101
Steve Gattuso
  • 7,644
  • 10
  • 44
  • 59

4 Answers4

65

Figured it out!

Turns out there's a weird module naming thing going on in Swift, so you can fix it by adding an @objc name to the class:

@objc(PrincipalClassName)

class PrincipalClassName: UIViewController {
...

and then set the NSExtensionPrincipalClass key to PrincipalClassName.

pNre
  • 5,376
  • 2
  • 22
  • 27
Steve Gattuso
  • 7,644
  • 10
  • 44
  • 59
  • Hey! Thanks, can you specify the source of where you found this? – Akshansh Thakur Jul 07 '16 at 17:19
  • 1
    When using Swift, you need to specify 'NotificationContent.NotificationContentViewController` in your Info.plist's `NSExtensionPrincipalClass` key's value field. In here, `NotificationContent` is module name, and `NotificationContentViewController` is the only view controller class. – DawnSong Nov 08 '18 at 07:30
23

Instead of the @objc hack, the proper way to do it in Swift is to include the module name in NSExtensionPrincipalClass, i.e.,

<key>NSExtensionPrincipalClass</key>
<string>$(PRODUCT_MODULE_NAME).ActionViewController</string>

(Documenting the error otherwise:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: ...)'

Hopefully will help someone who run into this error in the future.)

4ae1e1
  • 7,228
  • 8
  • 44
  • 77
14

Found the answers from these answers combined Answer 1 and Answer 2.

For Objective-C you will have to put the following in the info.plist of the App extension: NSExtensionPrincipalClass and make sure that it is under NSExtension Dict

So full answer should be like this , in my case ActionViewController is the App extension viewController

enter image description here

Community
  • 1
  • 1
Mohamed Saleh
  • 2,881
  • 1
  • 23
  • 35
5

Can't add a comment but it is no longer NSPrincipalClass rather it is NSExtensionPrincipalClass

Tosin Afolabi
  • 151
  • 2
  • 6