Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
479 views
in Technique[技术] by (71.8m points)

ios - Easy way to update app content via apple watch

I couldn't find an easy way to update the view in my iPhone app when I push a button on the AppleWatch App yet.

I tried it with NSUserDefaults Observer like this:

iPhone App Viewcontroller(inside ViewDidLoad()):

 // Create and share access to an NSUserDefaults object.
 mySharedDefaults = NSUserDefaults(suiteName: "group.sharedTest")

//Add Observer    
NSUserDefaults.standardUserDefaults().addObserver(self, forKeyPath: "test", options: NSKeyValueObservingOptions.New, context: nil)

In the Watchkit Extensions InterfaceController I added an IBAction with the button with this code:

mySharedDefaults!.setObject("testValue", forKey: "test")
mySharedDefaults!.synchronize()

But when I press the button, nothing happens! If I set an object inside my iPhone ViewController it works, but not if it is updated via the app! Can someone help me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You weren't lucky because when you wrote your question there wasn't API for this in iOS SDK. Three days ago, 10th December 2014 Apple released iOS 8.2 beta 2 SDK with two, important for this task, methods.

In WatchKit Framework, WKInterfaceController class

// Obj-C
+ (BOOL)openParentApplication:(NSDictionary *)userInfo
                        reply:(void (^)(NSDictionary *replyInfo,
                                        NSError *error))reply

By calling this method iOS will run your app in the background and AppDelegate of app will receive this message

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

This is second method added in iOS SDK Beta 2 (in terms of this question) in UIKit Framework, UIApplicationDelegate class.

You can use NSDictionary and reply block to communicate Watch app and iOS app.

Example

In your WKInterfaceController subclass

- (IBAction)callPhoneAppButtonTapped
{
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"text to display", @"key", nil];

    [InterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) {
        NSLog(@"Reply received by Watch app: %@", replyInfo);
    }];
}

and then in your iOS AppDelegate class

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
    NSLog(@"Request received by iOS app");
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"your value to return to Apple Watch", @"key", nil];

    reply(dictionary);
}

When you tap button on Apple Watch simulator your iOS app in iOS Simulator will be launched, and you should be able to see NSLog's in proper places.

Note

This solution works for transporting objects between Watch and iOS apps. But if you plan to transport more data, access images, file etc, you should use Shared app group. You set shared app group in Capabilities in your Xcode Project file. Use containerURLForSecurityApplicationGroupIdentifier (NSFileManager class) to get URLs to files in shared group.

If you want to share Preferences initWithSuiteName from NSUserDefaults is what you are looking for.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...