iOS/iPhone/iPad/MacOSX/Android プログラミング, Objective-C, Cocoaなど
先日のCocoa勉強会の復習だ。
Empty Applicationを生成する。
Storyboardファイルを新規作成する。
Main Storyboardに、さっき作成したStoryboardファイルを設定する。
ViewControllerを追加する。追加した物かどうか、分かりやすくする為、背景を黄色に設定。
アプリケーション・デリゲートでwindowプロパティの生成をコメントアウトする。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/*
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
*/
return YES;
}
実行する。
今回はここまで。
AppleStore銀座で開催されたAruduinoハンズオンセミナー初級編に参加してきた。
今回は、その復習の結果だが、素人なので間違いがある可能性がある。その場合は指摘してくれると嬉しい!
オームの法則
I=(1/R)*V[A]
I:電流[A]
V:電圧[V]
1/R:比例定数。
R:電気抵抗[Ω]
発光ダイオード(LED)
Arduinoからの電源電圧は5V、LEDの順方向降下電圧は3.5V(予想)、セミナーで使用した抵抗は1,600Ω(だと思う)
電流 = 電圧 ÷ 抵抗 = (5V - 3.5V) ÷ 1.6kΩ ≒ 1mA
つまり、1mAの電流を流れる事を期待したということか。
プルアップ抵抗/プルダウン抵抗
回路にOn/Offするスイッチを入れた場合、Off時は断線した状態。つまり、電圧不定の状態になる。プルアップの場合、例えば、5Vの電源と間に10kΩの抵抗を入れてつなぐと、0.5mA(=5V÷10kΩ)と低い電流で安定することになる。そして、スイッチをOnにすると抵抗値が低い側に電流が流れるので、望みどおりとなる。
プルダウンの場合、スイッチがOffの際、マイコンの0Vの端子と接続させて安定させ、Onの際に5Vの端子と接続させるということか。まだ、説明していて自身がない。
今回は、基礎的な話なので、今後は、Arduinoとの関係について調べて説明していきたい。
今回は、USBパラレル変換モジュールをMacに接続してみる、ストーリーボードとセグウェ、スペルチェッカー、Local Notificationと通知センター、アクションシートのバグの発表があった。
Notificationには、LocalとPushの二種類があって、前者は発生源が手元のアプリケーション、後者は発生源が外部という事のようだ。
Local Notificationの登録は、通知する日時を指定する方法と、直ぐに通知する方法の二通りがある。
通知する日時を指定する手順は以下のとおり。
- (IBAction)scheduleNotification:(id)sender
{
DBGMSG(@"%s", __func__);
NSDate *today = [NSDate date];
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif == nil)
return;
localNotif.fireDate = [today dateByAddingTimeInterval:10];
localNotif.timeZone = [NSTimeZone defaultTimeZone];
localNotif.alertBody = [NSString stringWithString:NSLocalizedString(@"Local Notify", nil)];
localNotif.alertAction = NSLocalizedString(@"local notify", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber + 1;
NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@"local notify" forKey:@"Key"];
localNotif.userInfo = infoDict;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}
UILocalNotificationのプロパティfireDateに、通知する日時を設定する。上記の例は、現在の時刻の10秒後だ。
UILocalNotificationのインスタンスをUIApplicationのscheduleLocalNotification:メソッドに設定すると、指定された日時に通知される。
直ぐに通知させたい場合は、presentLocalNotificationNow:メソッドを呼ぶ。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
DBGMSG(@"Application entered background state.");
NSAssert(self.bgTask == UIBackgroundTaskInvalid, nil);
self.bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
while ([application backgroundTimeRemaining] > 1.0) {
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
if (localNotif) {
localNotif.alertBody = [NSString stringWithString:NSLocalizedString(@"background", nil)];
localNotif.alertAction = NSLocalizedString(@"action", nil);
localNotif.soundName = UILocalNotificationDefaultSoundName;
localNotif.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber + 1;
[application presentLocalNotificationNow:localNotif];
break;
}
}
[application endBackgroundTask:self.bgTask];
self.bgTask = UIBackgroundTaskInvalid;
});
}
通知を受けてアプリケーションを呼び出す場合は、アプリケーション側で準備が必要となる。
アプリケーションの起動されていない場合は、アプリケーションのデリゲートのdidFinishLaunchingWithOptions:メソッドで通知からの呼び出しかどうかの判断を行う。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSString *value = [localNotif.userInfo objectForKey:@"Key"];
DBGMSG(@"%s, Notify: %@", __func__, value);
application.applicationIconBadgeNumber = localNotif.applicationIconBadgeNumber - 1;
}
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
起動済みの場合は、didReceiveLocalNotification:メソッドが呼ばれる。
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif
{
NSString *value = [notif.userInfo objectForKey:@"Key"];
NSLog(@"%s, Notify: %@", __func__, value);
app.applicationIconBadgeNumber = notif.applicationIconBadgeNumber - 1;
}
今回は、Arduinoだ。
まずは、Arduinoを入手しないと話にならない。私はスイッチサイエンスの製品をAmazonで購入した。
ArduinoとMacとは、USBケーブルで接続となるとなるがA-Bタイプを購入する事。A-miniBは別物だ!私はそれで失敗した。

製品が入手できたら以下のサイトの説明通りにすればいい。
ただ、私の場合だけの可能性があるが、入手したArduino UNO R3は、初期状態でExamples/1.Basics/Blinkがアップロードされている様で、この手順で試しても最初は変化が分からなかった。
アプリケーションに初期設定情報を持たせる場合、バージョン管理が重要になる。できれば、最初の版からバージョン情報を持たせる事を薦める。
バージョン情報は、新旧の比較を考えると数値型の方が便利だと思うが、バージョンが異なると以前の初期設定を初期化していいのなら、アプリケーションのバージョン番号と同じ内容を文字列として持たせても問題ないと考えている。
@interface Document : NSObject
@property (strong, nonatomic) NSString *version;
@property (strong, nonatomic) NSString *message;
- (void)clearDefaults;
- (void)updateDefaults;
- (void)loadDefaults;
@end
Documentクラスに、バージョン情報が一致しない場合は初期化するコードを追加する。
@implementation Document
@synthesize version = _version;
@synthesize message = _message;
- (id)init
{
if ((self = [super init]) != nil) {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
self.version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
NSString *aVersion = @"1.0";
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"version"]) {
aVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"version"];
}
if ([aVersion compare:self.version] != NSOrderedSame) {
[self clearDefaults];
}
}
return self;
}
- (void)dealloc
{
self.version = nil;
self.message = nil;
}
- (void)clearDefaults
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"message"];
}
}
- (void)updateDefaults
{
BOOL fUpdate = NO;
NSString *aVersion = @"";
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"version"]) {
aVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"version"];
}
if (self.version) {
if ([aVersion compare:self.version] != NSOrderedSame) {
[[NSUserDefaults standardUserDefaults] setObject:self.version forKey:@"version"];
fUpdate = YES;
}
}
NSString *aMessage = @"";
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
aMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"message"];
}
if (self.message) {
if ([aMessage compare:self.message] != NSOrderedSame) {
[[NSUserDefaults standardUserDefaults] setObject:self.message forKey:@"message"];
fUpdate = YES;
}
}
if (fUpdate) {
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)loadDefaults
{
NSString *aVersion = @"";
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"version"]) {
aVersion = [[NSUserDefaults standardUserDefaults] objectForKey:@"version"];
}
if ([aVersion compare:self.version] != NSOrderedSame) {
[self clearDefaults];
}
else {
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
self.message = [[NSUserDefaults standardUserDefaults] objectForKey:@"message"];
}
}
}
@end
アプリケーションの初期設定の情報は、NSUserDefaultsクラスを使えば実現できる。ただ、単純にNSUserDefaultsを使って値を読み書きするだけだと、扱う情報が増えてくると複雑になってくるので、先日紹介したDocumentクラスを使った実装を紹介する。
自分のアプリケーション用のDocumentクラスを用意する。
@interface Document : NSObject
@property (strong, nonatomic) NSString *message;
- (void)clearDefaults;
- (void)updateDefaults;
- (void)loadDefaults;
@end
初期設定の項目をプロパティとして持ち、読み書き処理をメソッドして用意する。以下が実装例だ。
@implementation Document
@synthesize message = _message;
- (void)clearDefaults
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
DBGMSG(@"remove message:%@", self.message);
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"message"];
}
}
- (void)updateDefaults
{
NSString *aMessage = nil;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
aMessage = [[NSUserDefaults standardUserDefaults] objectForKey:@"message"];
}
if (self.message) {
if ((aMessage) && ([aMessage compare:self.message] == NSOrderedSame)) {
}
else {
[[NSUserDefaults standardUserDefaults] setObject:self.message forKey:@"message"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
}
- (void)loadDefaults
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"message"]) {
self.message = [[NSUserDefaults standardUserDefaults] objectForKey:@"message"];
}
}
@end
iOSでは、ユーザに保存という処理を意識させないのが優れたUIの条件のようだ。アプリケーションのデリゲート・クラスでアプリケーションが非アクティブになるタイミングで初期設定の値を保存する。
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize document = _document;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.document = [[Document alloc] init];
[self.document loadDefaults];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self.document updateDefaults];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[self.document updateDefaults];
}
@end
iPhone用グラフ描画ライブラリで、軽量なものとして、S7GraphViewというものがある。自分のアプリケーションでも利用しているのがこれだ。
個人的には気に入っているのだが、最近は更新が滞り、先日、改良コードを送付したのだが、返信がない。そして、久しぶりにサイトをアクセスしたところ、無くなったようだ。
その為、困ってしまったので、何か継承できたらと考えている。
ただし、パクリにならないよう、継承するのなら、一から設計し直すつもりだ。