ログイン



Categories » ‘未分類’

画面輝度の取得と変更

2月 5th, 2012 by

https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UIScreen_Class/Reference/UIScreen.html

iOS5以降では、iPhone(iPad)の画面輝度を取得したり変更できるようになった。ということで試してみた。iOS4系までは、「裏技」を使わないと実装できないかったがiOS5では簡単に実装できた。輝度変化の通知は「UIScreenBrightnessDidChangeNotification」を使うそうだが、うまく動作しなかった。

ソースコード

輝度UPボタン、輝度DOWNボタン、現在の輝度表示用のラベルをヘッダに追加。

@interface WebViewController : UIViewController{
    UIButton *brightButton;
    UIButton *darkButton;
    UILabel *currentBrightness;
}

それぞれ実装。

- (void)updateBrightness
{
    currentBrightness.text = [NSString stringWithFormat:@"%0.3f", [[UIScreen mainScreen] brightness]];
}
- (void)brighten{
    double brightness = [[UIScreen mainScreen] brightness];
    brightness += 0.1;
    if(brightness > 1.0)brightness = 1.0f;
    [[UIScreen mainScreen] setBrightness:brightness];
    [self updateBrightness];
}
- (void)darken{
    double brightness = [[UIScreen mainScreen] brightness];
    brightness -= 0.1;
    if(brightness < 0.0)brightness = 0.0f;
    [[UIScreen mainScreen] setBrightness:brightness];
    [self updateBrightness];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
   
    currentBrightness = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 200, 30)];
    [self.view addSubview:currentBrightness];
    [self updateBrightness];
   
    brightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    brightButton.frame = CGRectMake(30,60,200,30);
    [brightButton setTitle:@"Bright" forState:UIControlStateNormal];
    [brightButton addTarget:self action:@selector(brighten) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:brightButton];
   
    darkButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    darkButton.frame = CGRectMake(30,90,200,30);
    [darkButton setTitle:@"Dark" forState:UIControlStateNormal];
    [darkButton addTarget:self action:@selector(darken) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:darkButton];
}

ダウンロード: Brightness

JSONとオブジェクトを相互変換

1月 27th, 2012 by

iOS5以降では、NSJSONSerializationというオブジェクトが実装されており、これを使うとJSON文字列と、Objective-Cのオブジェクトを容易に相互変換できる。NSJSONSerializationに渡すのはNSStringでなくNSDataである点にだけ気をつければ非常に簡単に利用できる。※iOS4.3以前ではNSJSONSerializationは利用できないのでご注意下さい

//文字列をObjective-Cのオブジェクトに変換
NSError* error;
NSDictionary* json = [NSJSONSerialization
    JSONObjectWithData:
        [@"[1,2,3,[\"foo\",\"bar\"]]"
        dataUsingEncoding:NSUTF8StringEncoding]
    options:kNilOptions
    error:&error];
NSLog(@"%@ %@", json, error);

//Objective-CのオブジェクトをJSONに変換
NSData *jsonData =
    [NSJSONSerialization dataWithJSONObject:json
                         options:kNilOptions error:&error];
NSLog(@"%@ %@"
    ,[[NSString alloc]
        initWithData:jsonData
        encoding:NSUTF8StringEncoding]
    ,error);

実行結果

末尾のnullは、エラーが発生しなかったということを表しています。

2012-01-27 16:35:43.813 AppName[22778:fb03] (
    1,
    2,
    3,
        (
        foo,
        bar
    )
) (null)

2012-01-27 16:35:43.815 AppName[22900:fb03] [1,2,3,["foo","bar"]] (null)

指定したURLをSafariで開く方法

1月 26th, 2012 by

http://www.apple.comの部分を開きたいURLに書き換えればOK

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com/"]];

ステータスバーを隠す/表示する

1月 26th, 2012 by

statusBarHiddenをYESにすればステータスバーが隠れます。NOに設定すればステータスバーが表示されます。

[UIApplication sharedApplication].statusBarHidden = NO;
[UIApplication sharedApplication].statusBarHidden = YES;

iPhone投げが2度拒否された理由

12月 11th, 2011 by

iPhoneを投げ上げて高さを競うアプリを作ったのですが、2度拒否されて、ギブアップしました。

1回目:アプリ名にiPhoneが入ってるとだめ

「iPhone投げ」というアプリ名で申請したところ、Appleの商標権を侵害しているとのことでRejectされました。うっかりしてました。

8.1

We found that your app does not comply with the Guidelines for Using Apple’s Trademarks and Copyrights , as required by the App Store Review Guidelines.

Specifically, your app name includes “iPhone” in the short name displayed on the device. It is not necessary or appropriate to incorporate “iPhone” into the short name displayed on the home screen.

2回目:デバイスの破壊を促すようなアプリはだめ

iPhoneを投げ上げるという行為が、デバイスの破壊につながると解釈されました。きっとレビュワーがiPhoneを壊したのだとおもいます。

We found your app encourages behavior that could result in damage to the user’s device, which is not in compliance with the App Store Review Guidelines.

まとめ

 2回目の理由を回避するのは難しそうなので、このアプリは闇に葬ります。2回目の却下理由は主観的は判断も多少はいっていると思いますが、1回目の理由は完全に此方側のミスなので、ガイドラインを熟読して、無駄に時間を潰さないようにします。今回のアプリでは、CoreDataの使い方を習得するというのが目的だったのですが、この目的自体は達成できました。このアプリ自体も、ゲーム性はそこそこ高いと思うので、将来的に、Android版を開発してみたいです。

iPhoneシミュレータの色を変える方法

12月 4th, 2011 by

画面が暗いアプリを作った場合に、どこまでがiPhoneのディスプレイなのか視認しにくい場合の解決方法を見つけたので、実際に変更してみました。「プレビュー」で作成したやっつけ画像ですが、ずいぶん視認性は向上しました。

手順
「/Developer/Platforms/iPhoneSimulator.platform/Developer/Applications」にあるシミュレータを開き、frame.pngを差し替える。

参考
iPhoneシミュレータの色を変える方法

デフォルト

色変更済み

@autoreleasepool

11月 25th, 2011 by

iOS5.0で、メモリの自動管理を選択した場合に@autoreleasepoolが使われます。@autoreleasepool{の部分で、autorelease poolを生成し、}の部分でautoreleasepoolをreleaseします。

@autoreleasepool {
    return UIApplicationMain(argc, argv, nil, NSStringFromClass([QuizAppDelegate class]));
}

アプリ用素材集

11月 24th, 2011 by

効果音

効果音源 高音質 1秒程度の効果音から1分程度の音源まで4000以上配布。ロイヤリティーフリー。mp3形式。

NSTimeIntervalの型

11月 23rd, 2011 by

NSTimeIntervalはdouble型です。

NSTimeInterval
Used to specify a time interval, in seconds.

typedef double NSTimeInterval;

Foundation Data Types Reference

initWithZone

11月 23rd, 2011 by

initWithZoneはZoneを指定してインスタンスを作るメソッド。一緒に使うオブジェクトを隣接したメモリ空間に配置することで、メモリアクセスの局所性を高め、性能向上を実現できる。というような目的で開発されたが、現代のObjective-Cでは、そんな事しなくても問題ないという風に考えられており、過去の遺物になっている。

詳解 Objective-C 2.0 改訂版の305ページ後半に解説があります。