将数据从iOS查询传递到Watch

时间:2015-03-15 19:38:24

标签: ios objective-c iphone parse-platform watchkit

我在iOS应用Parse中运行TableViewController.m查询。

我需要向iOS应用询问我的WatchKit扩展程序中的查询数据InterfaceController.m

我如何使用 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);
}];

我使用的是Objective-C。将添加所需的任何额外信息,谢谢!

1 个答案:

答案 0 :(得分:1)

在AppDelegate方法中创建Parse查询并将其打包到NSDictionary中并调用reply(queryDict); InterfaceController中的replyInfo字典将填充queryDict。