IRKit 3.0.2

IRKit 3.0.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年12月

OHTSUKA Masakazu 维护。



IRKit 3.0.2

  • 作者
  • OHTSUKA Masakazu

IRKit iOS SDK

IRKit 设备和SDK(这个库)允许您从您的iOS设备控制家庭电子产品。IRKit 设备内部有红外LED、接收器和WiFi模块。任何带有互联网或本地WiFi连接的设备都可以使用IRKit设备来发送IR信号。

这个库提供以下功能:

  • 提供 UIViewController 子类,用于封装连接和从IRKit设备中学习IR信号的复杂过程
  • 提供一个简单的接口来发送IR信号

获取IRKit设备

查看 IRKit 设备

安装

使用 1.0.0 或更高版本的 CocoaPods

$ cat Podfile
source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

platform :ios, '8.0'
workspace 'MyApp.xcworkspace'
project 'MyApp/MyApp.xcodeproj'

pod 'IRKit', '~> 3.0'

target 'MyApp' do
  project 'MyApp/MyApp.xcodeproj'
end

$ pod install

用法

查看 Minimal 应用 中的工作最小IR遥控应用程序。

包含头文件

#import <IRKit/IRKit.h>

初始化

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [IRKit startWithAPIKey:@"#{ fill in your apikey }"];
    return YES;
}

获取API密钥

% curl -d "email={your email}" "http://api.getirkit.com/1/apps"
{"message":"You will receive an email shortly, please click the URL in it to get an apikey"}

并在电子邮件中打开URL。

发送IR信号

IR信号表示为 IRSignal 实例。

[signal sendWithCompletion:^(NSError *error) {
    NSLog(@"sent error: %@", error);
}];

如何获取IRSignal?

您可以通过 学习 获取第一个 IRSignal,这意味着:您将旧的红外遥控器指向IRKit设备的IR接收器,并按它的按钮。IRKit设备会将其发送到您的应用(带有IRKit iOS SDK)。

但首先,您需要与IRKit设备 配对

// in your main view controller
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    // find IRKit if none is known
    if ([IRKit sharedInstance].countOfReadyPeripherals == 0) {
        IRNewPeripheralViewController *vc = [[IRNewPeripheralViewController alloc] init];
        vc.delegate = self;
        [self presentViewController:vc
                           animated:YES
                         completion:^{
                             NSLog(@"presented");
                         }];
    }
}

#pragma mark - IRNewPeripheralViewControllerDelegate

- (void)newPeripheralViewController:(IRNewPeripheralViewController *)viewController
            didFinishWithPeripheral:(IRPeripheral *)peripheral {
    NSLog( @"peripheral: %@", peripheral );

    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 NSLog(@"dismissed");
                             }];
}

配对完成后,您就可以从它那里学习一个IRSignal了。

IRNewSignalViewController *vc = [[IRNewSignalViewController alloc] init];
vc.delegate = self;
[self presentViewController:vc
                   animated:YES
                 completion:^{
                     NSLog(@"presented");
                 }];

#pragma mark - IRNewSignalViewControllerDelegate

- (void)newSignalViewController:(IRNewSignalViewController *)viewController
            didFinishWithSignal:(IRSignal *)signal {
    NSLog( @"signal: %@", signal );

    if (signal) {
        // successfully learned!
        _signal = signal;
    }
    [self dismissViewControllerAnimated:YES
                             completion:^{
                                 LOG(@"dismissed");
                             }];
}

多个IRSignal的集合

大多数遥控器都有不止一个按钮,所以您可能想要管理IRSignal的集合。《IRSignals》(带有《s》后缀)就是它。

_signals = [[IRSignals alloc] init];

// and add a signal to the collection
[_signals addSignalsObject:_signal];

// send multiple IRSignal-s sequentially
[_signals sendSequentiallyWithCompletion:^(NSError *error) {
    NSLog( @"sent with error: %@", error );
}];

// and save it in NSUserDefaults
[_signals saveToStandardUserDefaultsWithKey:@"irkit.signals"];

// or load it from NSUserDefaults
[_signals loadFromStandardUserDefaultsKey:@"irkit.signals"];

// or send it elsewhere
NSData *data = [_signals data];

// or as JSON
NSString *json = [_signals JSONRepresentation];

App Transport Security

IRKit的设备HTTP API不支持HTTPS。
在您的Info.plist中添加以下行。

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

贡献指南