Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a Today widget programmatically without storyboard on iOS8?

Tags:

I tried to delete the storyboard file and related Info.plist entry but this time extension stopped working; it doesn't even launch from XCode.

The operation couldn’t be completed. (LaunchServicesError error 0.)

It is easy on the regular app (containing app) as we see it's entry point and application delegate, but how to do it on extensions too?

like image 449
frankish Avatar asked Sep 25 '14 11:09

frankish


People also ask

How to create a custom widget iPhone?

Create your own widget stacksTouch and hold an app or empty area on the Home Screen or Today View until the apps jiggle. Drag a widget on top of another widget. You can stack up to 10 widgets. Tap Done.

How do I create a widget in Xcode?

Open your app project in Xcode and choose File > New > Target. From the Application Extension group, select Widget Extension, and then click Next. Enter the name of your extension. If the widget provides user-configurable properties, check the Include Configuration Intent checkbox.


3 Answers

I did the following steps:

  • Delete the storyboard file from your project
  • Modify the info.plist:

Go to the NSExtension Dictionary, remove this key: NSExtensionMainStoryboard. Replace it with this key NSExtensionPrincipalClass and add your ViewController as the value, e.g. TodayViewController.

before:

<key>NSExtension</key>
<dict>
    <key>NSExtensionMainStoryboard</key>
    <string>MainInterface</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.widget-extension</string>
</dict>

after:

<key>NSExtension</key>
<dict>
    <key>NSExtensionPrincipalClass</key>
    <string>TodayViewController</string>
    <key>NSExtensionPointIdentifier</key>
    <string>com.apple.widget-extension</string>
</dict>
  • If you're using Swift, you have to enable "Embedded Content Contains Swift Code" in the Build Settings of the target. Set it to YES.
  • Additionally I had to add @objc (TodayViewController) in my TodayViewController class (after the imports).

The app should run now. But there were two other things I had to do:

  • Create a view. Obviously there is no view created automatically.

So add these lines:

override func loadView()
{
    view = UIView(frame:CGRect(x:0.0, y:0, width:320.0, height:200.0))
}
  • And set the height of your widget in your viewDidLoad method: self.preferredContentSize = CGSizeMake(0, 200)
like image 64
Raphael Avatar answered Oct 09 '22 20:10

Raphael


Remove NSExtensionMainStoryboard from Info.plist Add NSExtensionPrincipalClass = YourViewController

Don't forget to create your own view in loadView

like image 35
frankish Avatar answered Oct 09 '22 20:10

frankish


FWIW, it didn't work for me until I added a prefix of the module name for my Swift view controller class, e.g.

<key>NSExtension</key>
<dict>
    <!-- ... -->
    <key>NSExtensionPrincipalClass</key>
    <string>SafariActionExtension.RootViewController</string>
    <!-- ... -->
</dict>

That is probably because class lookup for Swift modules turns module names into prefix for class name. E.g. to create your class in code you would write NSStringFromClass("SafariActionExtension.RootViewController"), hence the prefix.

like image 44
Sash Zats Avatar answered Oct 09 '22 22:10

Sash Zats