IRKit 设备和SDK(这个库)允许您从您的iOS设备控制家庭电子产品。IRKit 设备内部有红外LED、接收器和WiFi模块。任何带有互联网或本地WiFi连接的设备都可以使用IRKit设备来发送IR信号。
这个库提供以下功能:
查看 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;
}
% 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信号表示为 IRSignal 实例。
[signal sendWithCompletion:^(NSError *error) {
NSLog(@"sent error: %@", error);
}];
您可以通过 学习 获取第一个 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的集合。《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];
IRKit的设备HTTP API不支持HTTPS。
在您的Info.plist
中添加以下行。
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>