1

I've got an iOS app with Core Data via MagicalRecord. Now I'd like to extend the app with a WatchKit Extension and therefore need access to MagicalRecord. Is this possible or do I have to switch over to "native" Core Data? It would be perfect if I could use Magical Record in the WatchKit extension, but I couldn't figure out how until now. I can't even include my cocoapods in the shared Cocoa Touch Framework... (use CocoaPods 0.36.0 in CocoaTouchFramework with Swift)

Community
  • 1
  • 1
swalkner
  • 16,679
  • 31
  • 123
  • 210

2 Answers2

1

There shouldn't be any problems using MagicalRecord in your WatchKit Extension. You will need to do the following.

  1. In your General Settings for your WatchKit Extension you will need to add libPods.a to your Linked Frameworks and Libraries.

enter image description here

enter image description here

  1. You will need to have MagicalRecord used a database in the shared group folder and not its default app folder. To do that you will need to do this when you set up MagicalRecord

    NSString* dbPath = [self sharedAppGroupDirectory];
    dbPath = [dbPath stringByAppendingPathComponent:@"MyDatabaseName.sqlite"];
    NSURL* dbURL = [NSURL fileURLWithPath:dbPath];
    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:(id)dbURL];
    

You can get the shared App Group Directory with this.

NSFileManager *fm = [NSFileManager defaultManager];
NSString *appGroupName = @"group.com.mygroup";
NSURL *groupContainerURL = [fm containerURLForSecurityApplicationGroupIdentifier:appGroupName];
NSString* path = [groupContainerURL filePathString];
Stephen Johnson
  • 5,293
  • 1
  • 23
  • 37
  • okay, I added libPods.a, but when I import it into my ```InterfaceController.swift``` via ```import MagicalRecord```, I get ```No such module 'MagicalRecord'``` - do I have to put something special in my Podfile? – swalkner Mar 23 '15 at 21:02
  • I also do not know how to include my ```NSManagedObjectModel```-subclasses into the WatchKit-Extension... – swalkner Mar 23 '15 at 21:24
  • Unless, MagicalRecord added a swift implementation since I last checked, you will need to use an Obj-C to Swift bridge header to use MagicalRecord in your swift class. Your for subclass you will need to add it to your WatchKit extension target. See http://stackoverflow.com/questions/27112728/calling-a-method-on-watchkit/27113641#27113641 – Stephen Johnson Mar 23 '15 at 22:28
  • I made a new project, where the WatchKit Extension is Objective-C, I can't import MagicalRecord nevertheless: ```#import ``` leads to ```'MagicalRecord/MagicalRecord.h' file not found``` - although I'm adding ```libPods.a``` - what am I missing here? – swalkner Mar 24 '15 at 06:28
0

Add one line in your Profile file

link_with 'yourApp', 'yourAppExtension'

Then run the command line on your Terminal

pod install
Changwei
  • 672
  • 1
  • 5
  • 16