WatchKit Extension is not seeing data saved in NSUserDefaults with App Group

我与影子孤独终老i 提交于 2019-12-12 12:50:17

问题


I've been through a ton of SO posts, and this USED to work, but it stopped working. I'm not sure what happened. I developed this iPhone+WatchKit app with watchOS 1.0 and everything worked fine.

I've upgraded my app, project, and Apple Watch to watchOS 2.0, and now I can't get any data via NSUserDefaults using my App Group.

  1. App Groups is enabled in Xcode on the host App, and WatchKit Extension. I even tried turning it on for the WatchKit App as well.

  2. My group name is called "group.com.mycompany.myapp" (with my real company name and app name) and it's selected on all of the targets.

  3. I've confirmed the Build settings for my host app and WatchKit extension reference the entitlements files and I've checked that those entitlements files contain the app groups security option for my chosen app group.

  4. I've made sure the bundle identifiers are different for the host app, watchkit extension, and watchkit app. I use "com.mycompany.myapp", "com.mycompany.myapp.watchkitextension", and "com.mycompany.myapp.watchkitapp", respectively.

Here are some screenshots of my Xcode config for the host app, watchkit extension, and watchkit app, just in case:

Here is the code in my host app that is able to read the data properly:

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kAppGroupName];
NSLog(@"app defaults: %@", [defaults dictionaryRepresentation]);

That produces this in my console, with kKeyEmployeeId and kKeyStoreNumber being the data that I'm trying to share between the host and watch.

2015-12-13 13:51:35.618 MyApp[27516:16126253] app defaults: {
    AppleLanguages =     (
        "en-US",
        en
    );
    INNextHearbeatDate = "472211882.83309";
    NSInterfaceStyle = macintosh;
    NSLanguages =     (
        "en-US",
        en
    );
    "com.apple.content-rating.AppRating" = 1000;
    "com.apple.content-rating.ExplicitBooksAllowed" = 1;
    "com.apple.content-rating.ExplicitMusicPodcastsAllowed" = 1;
    "com.apple.content-rating.MovieRating" = 1000;
    "com.apple.content-rating.TVShowRating" = 1000;
    kKeyEmployeeId = Kenny;
    kKeyStoreNumber = 001;
}

In my WatchKit Extension, I have the same code, but it doesn't provide me the two key:value pairs that I need:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kAppGroupName];
    NSLog(@"defaults: %@", [defaults dictionaryRepresentation]);

}

It produces this, which is very similar to the one above, but without the two keys I really need:

2015-12-13 13:28:29.840 MyApp WatchKit Extension[720:322804] defaults: {
    AppleLanguages =     (
        en
    );
    INNextHearbeatDate = "472434306.599217";
    NSInterfaceStyle = macintosh;
    NSLanguages =     (
        en
    );
    "com.apple.content-rating.AppRating" = 1000;
    "com.apple.content-rating.ExplicitBooksAllowed" = 1;
    "com.apple.content-rating.ExplicitMusicPodcastsAllowed" = 1;
    "com.apple.content-rating.MovieRating" = 1000;
    "com.apple.content-rating.TVShowRating" = 1000;
}

The kAppGroupName and other constants are defined like so:

NSString * const kAppGroupName = @"group.com.inadaydevelopment.MyApp";
NSString * const kKeyStoreNumber = @"kKeyStoreNumber";
NSString * const kKeyEmployeeId = @"kKeyEmployeeId";

回答1:


In Watch OS1 this worked, but in Watch OS2 there has been some changes. You need to use something called WatchConnectivity to send the data you want to save to the watch. Then when the watch receives the data you sent to it, save it to the Apple watch's default NSUserDefaults.

WCSession.defaultSession() will return the WCSession singleton for transferring data between your iOS and Watch app.

Here is a tutorial and example.




回答2:


Since watch os 2 apps run on the watch and not the iPhone, I don't think you have access to NSUserDefaults or app groups. You'll have to use WatchConnectivity framework to transfer data to and from the watch.




回答3:


You can follow this steps

1) set the session

if ([WCSession isSupported])
{
    [[WCSession defaultSession] setDelegate:self];
    [[WCSession defaultSession] activateSession];
}

2) Prepare data dictionary and send it with below method

[WCSession defaultSession] sendMessage:dataDict replyHandler:^(NSDictionary<NSString *,id> * _Nonnull replyMessage) {

            //You task on completion

        } errorHandler:^(NSError * _Nonnull error) {

            if (error)
            {
                //Handle the error
            }
        }];

3) And in watch app code You can set delegate method

 - (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message replyHandler:(void(^)(NSDictionary<NSString *, id> *replyMessage))replyHandler
    {
          //Handle the response
    }


来源:https://stackoverflow.com/questions/34256852/watchkit-extension-is-not-seeing-data-saved-in-nsuserdefaults-with-app-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!