SRMR
SRMR

Reputation: 3134

Pass Data from iOS query to Watch

I run a Parse query in my iOS app TableViewController.m.

I need to ask the iOS app for that query data from my WatchKit extension InterfaceController.m

How would I go about this using openParentApplication(_:reply:) + handleWatchKitExtensionRequest(_:reply:)?

TableViewController.m (iOS)

- (void)viewDidLoad {
        // GMT Date from Phone
        NSDate *gmtNow = [NSDate date];
        NSLog(@"GMT Now: %@", gmtNow);

        // Query Parse
        PFQuery *query = [self queryForTable];
        [query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                NSMutableArray *localMatchup = [@[] mutableCopy];

                for (PFObject *object in objects) {
                    // Add objects to local Arrays
                    [localMatchup addObject:[object objectForKey:@"matchup"]];

                    // App Group
                    NSString *container = @"group.com.me.off";
                    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

                    // Matchup
                    [defaults setObject:localMatchup forKey:@"KeyMatchup"];
                    NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
                    NSLog(@"Default Matchup: %@", savedMatchup);
                    savedMatchup = matchupArray;
                }

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });

            }
        }];
    }

AppDelegate.m (iOS)

    - (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {

NSString * request = [userInfo objectForKey:@"requestString"];
if ([request isEqualToString:@"executeMethodA"]) {

        // GMT Date from Phone
        NSDate *gmtNow = [NSDate date];
        NSLog(@"GMT Now: %@", gmtNow);

        // Query Parse
        PFQuery *query = [self queryForTable];
        [query whereKey:@"dateGame" greaterThanOrEqualTo:gmtNow];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                NSMutableArray *localMatchup = [@[] mutableCopy];

                for (PFObject *object in objects) {
                    // Add objects to local Arrays
                    [localMatchup addObject:[object objectForKey:@"matchup"]];

                    // App Group
                    NSString *container = @"group.com.me.off";
                    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:container];

                    // Matchup
                    [defaults setObject:localMatchup forKey:@"KeyMatchup"];
                    NSArray *savedMatchup = [defaults objectForKey:@"KeyMatchup"];
                    NSLog(@"Default Matchup: %@", savedMatchup);
                    savedMatchup = matchupArray;
                }

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
                    reply(@{@"success:": @true});

            }
        }];
    }
             // Assuming this is where I'd do the reply?
             // Not sure what would go here for the reply though?
reply(@{@"success:": @false});

}

InterfaceController.m (WatchKit)

NSString *requestString = [NSString stringWithFormat:@"executeMethodA"]; // This string is arbitrary, just must match here and at the iPhone side of the implementation.
NSDictionary *applicationData = [[NSDictionary alloc] initWithObjects:@[requestString] forKeys:@[@"theRequestString"]];

[WKInterfaceController openParentApplication:applicationData reply:^(NSDictionary *replyInfo, NSError *error) {
    // What would I put here?
    NSLog(@"\nReply info: %@\nError: %@",replyInfo, error);
}];

I'm using Objective-C. Will add any extra info needed, thanks!

Upvotes: 0

Views: 361

Answers (1)

bgilham
bgilham

Reputation: 5939

Make the Parse query in your AppDelegate method and package it in an NSDictionary and call reply(queryDict); The replyInfo dictionary in your InterfaceController will be populated with queryDict.

Upvotes: 1

Related Questions