5

I'm trying to build an today extension for my app.

I'm using CoreData and NSFetchedResultsController and get the following error:

Use of undeclared type 'AppDelegate'

In this line of code:

var appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate. 

Does someone know how to solve this error / use AppDelegate in the today extension and would like to help me?

horst
  • 575
  • 1
  • 6
  • 15
  • Do you have the proper import statements? – milo526 Apr 18 '15 at 17:22
  • Did you rename your applicaiton delegate to something different from AppDelegate? – Martin Koles Apr 18 '15 at 17:24
  • What do you mean with proper import statements @milo526 (import AppDelegate isn't working). No I don't rename my application delegate (I don't really know how to do this). – horst Apr 18 '15 at 17:43
  • Did you import UIKit & Foundation? – milo526 Apr 18 '15 at 17:44
  • Yes I imported UIKit & Foundation. – horst Apr 18 '15 at 18:20
  • 3
    I am experiencing same problem. Have you found the solution? I am trying to add extension to my existing project, and share the existing Core Data database. We get same problem with you. So, how do you fix the problem? – Yi Jiang Jul 13 '15 at 00:17
  • Same problem faced here as mentioned by @RobertYiJiang. Could anyone fix the same. Does it require some project setting ? – Abhijit May 03 '17 at 17:14

1 Answers1

2

A Today Extension isn't an application, so UIApplication and AppDelegate aren't available. I'm not for sure what you are trying to do with the AppDelegate, but extensions do have a NSExtensionContext available by calling:

 self.extensionContext
duncanc4
  • 1,191
  • 1
  • 9
  • 17
  • 1
    I'd like to use the NSFetchedResultsController to use CoreData. – horst Apr 18 '15 at 20:59
  • 2
    NSFetchedResultsController isn't tied to a UIApplicationDelegate. If you want to use it in your today extension, just you just use it in TodayViewController. I'm guessing in your application, you use NSFetchedResultsController in your AppDelegate to get the results from CoreData. If you want to share this code between your app and the extension, you will need to move this code out of AppDelegate and in to a framework that can be used by both the app and the extension. – duncanc4 Apr 18 '15 at 21:14