IRKit device and SDK(this library) lets you control your home electronics from your iOS devices. IRKit device has a Infrared LED and receiver, and a WiFi module inside. Any device with internet or local wifi connection can use IRKit devices to make it send IR signals.
This library does:
- provide UIViewController subclasses that wraps complex procedures to connect and learn IR signals from IRKit devices
- provide a simple interface to send IR signals
see IRKit device
Use cocoapods 1.0.0 or higher
$ 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
See Minimal app for a working minimal IR remote controller application.
#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"}
and open the URL in the email.
IR signal is represented as a IRSignal instance.
[signal sendWithCompletion:^(NSError *error) {
NSLog(@"sent error: %@", error);
}];
You get your first IRSignal
by learning it, which means: you point your old infrared remote controller at IRKit device's IR receiver, and press it's button.
IRKit device will send it over to your app(with IRKit iOS SDK).
But first, you need to pair with an IRKit device.
// 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");
}];
}
When pairing is done, you're ready to learn an IRSignal from it.
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");
}];
}
Most remote controllers have more than 1 button, so you'll want to manage a collection of IRSignals.
IRSignals
(with a s
postfix) is it.
_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's Device HTTP API doesn't support HTTPS.
Add following lines in your Info.plist
.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>