Communicating between the iOS app and the Watch Extension

There are four scenarios where an app and an extension might wish to communicate:

  1. Watch extension to suspended/terminated iOS app - use +[WKInterfaceDevice openParentApplication:reply:] if you need to "wake up" your iOS app. If you simply need to leave data in a place for your iOS app to find it later, then you should write it into the shared group container and have your iOS app consult it when it next needs the information.

  2. Watch extension to running iOS app - use either +[WKInterfaceDevice openParentApplication:reply:] or the Darwin notification center.

  3. iOS app to suspended/terminated Watch extension - Since the Watch extension is only launched in reaction to your Watch app being launched on the Apple Watch, an iOS app cannot launch its corresponding Watch extension. Instead, the app should write whatever data it wants to send to the Watch extension into the shared group container, and the extension should consult it when it next needs the information.

  4. iOS app to to running Watch extension - If your iOS app received the -[<UIApplicationDelegate> application:handleWatchKitExtensionRequest:reply:] message, it should use the system-provided reply block to convey information back to the extension. Otherwise you may use the Darwin notification center to pass information back to the extension.


More information on the Darwin notification center can be found in the Documentation.


watchos2 录音功能实现

[self presentAudioRecordingControllerWithOutputURL:fileUrl
                                       actionTitle:@"Some Title"
                                        completion:^(BOOL didSave, NSError * __nullable error) {

                                            NSLog(@"didSave:%d, error:%@", didSave, error);

The extension is not automatically notified of changes to the shared user defaults made by the iOS app, and vice versa. If you would like to be notified, you can do so manually by using the Darwin notification center.



auto engine = LuaEngine::getInstance();




        lua_State* L = engine->getLuaStack()->getLuaState();




        int isOpen = luaL_dofile(L, "/Users/game-netease/svn/gzhuabo/Menghuan/src/operation.lua");


        if(isOpen != 0)




            const char* iResult = lua_tostring(L, -1);


            NSLog(@"Open Lua Error: %i, %s", isOpen, iResult);


            handler(@{@"glanceInfo": @"ERROR"});








            lua_getglobal(L, "getGlance");












            lua_call(L, 0, 1);


            const char* iResult = lua_tostring(L, -1);


            NSString *str = [NSString stringWithUTF8String:iResult];


            NSLog(@"%@", str);


            handler(@{@"glanceInfo": str});




NSArray *array = (NSArray *)context;

    for (id object in array) {

        Activity *obj = (Activity *)[NSKeyedUnarchiver unarchiveObjectWithData: object];

        NSLog(@"name: %@, type: %@", obj.m_name, obj.m_type);

        NSLog(@"time: %@", obj.m_time);

        NSLog(@"donetime: %@", obj.m_doneTime);


@interface Activity : NSObject
@property(nonatomic, retain) NSString *m_name;               //名称
@property(nonatomic, retain) NSString *m_type;               //日常活动、限时活动、节日活动
@property(nonatomic, retain) NSNumber *m_doneTime;           //已经完成次数
@property(nonatomic, retain) NSNumber *m_time;               //每日次数
@property(nonatomic, retain) NSNumber *m_doneActiveness;     //已经完成活跃度
@property(nonatomic, retain) NSNumber *m_activeness;         //每日活跃度

#import <Foundation/Foundation.h>
#import "Activity.h"

@implementation Activity
- (void) encodeWithCoder : (NSCoder *)encode {
    [encode encodeObject:self.m_name forKey:@"name"];
    [encode encodeObject:self.m_type forKey:@"type"];
    [encode encodeObject:self.m_doneTime forKey:@"doneTime"];
    [encode encodeObject:self.m_time forKey:@"time"];
    [encode encodeObject:self.m_doneActiveness forKey:@"doneActiveness"];
    [encode encodeObject:self.m_activeness forKey:@"activeness"];

- (id)initWithCoder:(NSCoder *)aDecoder {
    if(self = [super init])
        self.m_name =           [aDecoder decodeObjectForKey:@"name"];
        self.m_type =           [aDecoder decodeObjectForKey:@"type"];
        self.m_doneTime =       [aDecoder decodeObjectForKey:@"doneTime"];
        self.m_time =           [aDecoder decodeObjectForKey:@"time"];
        self.m_doneActiveness = [aDecoder decodeObjectForKey:@"doneActiveness"];
        self.m_activeness =     [aDecoder decodeObjectForKey:@"activeness"];
    return self;


posted @ 2015-08-14 14:26  wiessharling  阅读(230)  评论(0编辑  收藏  举报