1

I'm trying to read/write data in extra class file which is public. (StoreData.swift)

My function :

  func FetchName (NameforDate: String) -> NSString
         {
             var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate)
             var context:NSManagedObjectContext = appDel.managedObjectContext! 
    }

At line where is "(UIApplication.sharedApplication()." I get the error:

StoreData.swift:115:49: 'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.

I created App and Today Extension.

martinpristas
  • 489
  • 5
  • 12
  • UIApplication is not available in extensions. – quellish Nov 14 '14 at 16:48
  • I'm trying to find some other way how can I access to CoreData without UIApplication – martinpristas Nov 14 '14 at 17:14
  • You do not need UIApplication to access CoreData. Your application and extension must each access Core Data independantly. This can be done any number of ways, but that is beyond the scope of your question. – quellish Nov 14 '14 at 18:06
  • hmm... Can you give me a advice how to read/write data to CoreData without UIApplication please ? ... something like var context:NSManagedObjectContext = xxx.managedObjectContext ? .. – martinpristas Nov 14 '14 at 18:19
  • You would have to write a different question. This question is about why you are not able to access sharedApplication – quellish Nov 14 '14 at 18:20
  • Allright I will, thank you for your respond ! – martinpristas Nov 14 '14 at 18:26

3 Answers3

0

If you need a managed object context you can create your own within your extension.Your AppDelegate will not be available to you. I would suggest moving out that from your app delegate to a class you can share between both. You will still be able to listen for core data updates so they don't need to share contexts/persistent store coordinates.

EDIT:

I'd like to point out that extensions are essentially a different sandbox. Therefore if you create a shared class the instances will not be the same shared between App and Extension. Therefore making the persistent store coordinator and contexts different.

rfrittelli
  • 1,211
  • 13
  • 15
  • My public class communicate with ViewController of App and TodayViewController for extension by all variables, that's alright, but when I create some SharedApplication data it gives me a error... – martinpristas Nov 14 '14 at 17:07
  • make sure the class you made is added to both targets. – rfrittelli Nov 14 '14 at 18:35
-1

Your issue:

StoreData.swift:115:49: 'sharedApplication()' is unavailable: Use view controller based solutions where appropriate instead.

Is because -[UIApplication sharedApplication] is not available to extensions. Your extension does not run as part of your application, so there is no application context for an extension to message.

This is clearly explained in the App Extension Programming Guide section on Understanding How an App Extension Works:

Because of its focused role in the system, an app extension is ineligible to participate in certain activities. An app extension cannot:

• Access a sharedApplication object, and so cannot use any of the methods on that object

Community
  • 1
  • 1
quellish
  • 21,123
  • 4
  • 76
  • 83
  • Thank you for your respond! It should help me find the other way how to acces to CoreData without UIApplication... There are some other ways how to read/write data to CoreData without UIApplication ? ... Thanks – martinpristas Nov 14 '14 at 18:21
  • CoreData is completely independent of UIKit and UIApplication. It is a totally separate framework that has no dependancies on UIKit. – quellish Nov 14 '14 at 18:22
  • @quellish, so how to open/import a file from gmail/google drive/messenger app to my app ? Any workaround ? – Jamshed Alam Mar 01 '20 at 10:43
-2

To share data between App and Today Widget, following Apple guidelines, you have to create an App Group under target's capabilities tab and link it to both App and Widget.

To access your shared defaults:

NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:YOUR_APP_GROUP_ID];

It works exactly like [NSUserDefaults standardUserDefaults] so you can store a minimal part of your data but it's all you can do.

EDIT:

Here you can find Swift doc for Suite Defaults: https://developer.apple.com/Library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/index.html#//apple_ref/occ/instm/NSUserDefaults/initWithSuiteName:

EDIT 2:

Use a Singleton to access your Core Data instead, you can find an easy tutorial here: http://www.johnwordsworth.com/2010/04/iphone-code-snippet-the-singleton-pattern/

  • That's a problem to use NSUserDefaults because I want refresh app data when I pull down widget or open the app... That refreshing App data is based on CoreData for my app... For Example, When I pull down Widget I want to call function refresh_data() and get a new variable from that function .. This function loading data from CoreData – martinpristas Nov 14 '14 at 17:30
  • Have you tried to move the NSManagedObjectContext inside a singleton Class instead your AppDelegate? this will make your core data accessible from today widget – Giuseppe Travasoni Nov 15 '14 at 09:12