Tag: watchkit

WatchKit SDK不从NSUserDefaults中检索数据

我想为苹果手表制作一个testing应用程序,您可以在手机上设置一些string,然后显示在Apple Watch上。 我决定使用NSUserDefaults类来存储这些数据。 在我的视图控制器为iPhone我有一个方法,把input和存储到本地存储: – (IBAction)saveInfo:(id)sender { NSString *userInput = [myTextField text]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:userInput forKey:@"savedUserInput"]; [defaults synchronize]; self.myLabel.text = [defaults stringForKey:@"savedUserInput"]; } 在我的手表界面控制器中,我有一个方法来检索数据并显示它: – (IBAction)showText2 { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults synchronize]; self.myLabel2.text = [defaults stringForKey:@"savedUserInput"]; } 除了某些原因,我试图检索的数据显示为空,并且标签没有被更新。

将数据传递给Apple Watch应用程序

我试图从我的应用程序传入数据到我的Apple Watch应用程序。 基本上,我使用的方法与创build当前小部件的方法相同,所以我通过NSUserDefaults传递数据。 问题是,当我运行我的应用程序时,数据不会像我所期望的那样更新Watch应用程序中的标签。 这是我有… override init(context: AnyObject?) { // Initialize variables here. super.init(context: context) // Configure interface objects here. NSLog("%@ init", self) var defaults = NSUserDefaults(suiteName: "group.AffordIt") var totalBudgetCalculation = "" if (defaults!.stringForKey("totalBudgetWidget") != nil) { println("Worked") totalBudgetCalculation = defaults!.stringForKey("totalBudgetWidget")! initialBudgetLabel.setText("Initial: \(totalBudgetCalculation)") } var currentBudgetCalculation = "" if (defaults!.stringForKey("currentBudgetWidget") != nil) { currentBudgetCalculation = […]

如何正确使用“openParentApplication”和“handleWatchKitExtensionRequest”以便“reply()”被调用?

情况:我在Watch应用程序中使用openParentApplication在主应用程序中调用handleWatchKitExtensionRequest 。 这在模拟器中很好地工作,它也适用于实际的设备(苹果手表和iPhone),当iPhone应用程序是主动/打开。 问题:当我在实际设备(Apple Watch和iPhone)上运行它时,当主iPhone应用程序未处于活动状态或打开状态时, handleWatchKitExtensionRequest不会将数据返回到openParentApplication 。 WatchKit扩展中的InterfaceController.m中的代码: NSDictionary *requst = @{ @"request" : @"getData" }; [InterfaceController openParentApplication:requst reply:^( NSDictionary *replyInfo, NSError *error ) { // do something with the returned info }]; iPhone上的主应用程序的代理代码: – (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void ( ^)( NSDictionary * ))reply { if ( [[userInfo objectForKey:@"request"] isEqualToString:@"getData"] ) { // get […]