Apple-Watch开发1

本文介绍了iOS应用与WatchKit扩展之间的四种通信场景,包括如何唤醒iOS应用、使用Darwin通知中心传递信息等,并提供了watchOS 2中实现录音功能的具体代码示例。

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
                                            preset:WKAudioRecordingPresetWideBandSpeech
                                   maximumDuration:5.0
                                       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();

 

        ScriptEngineManager::getInstance()->setScriptEngine(engine);

 

        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"});

 

        }

 

        else

 

        {

 

            lua_getglobal(L, "getGlance");

 

            /*

 

             lua_call

 

             第一个参数:函数的参数个数

 

             第二个参数:函数返回值个数

 

             */

 

            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;         //每日活跃度
@end

#endif
#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;
}
@end

 

转载于:https://www.cnblogs.com/wiessharling/p/4729889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值