【本を書きました!】iOSプログラミング逆引きリファレンス108
会社の同僚といっしょに本を書きました。
iOSアプリ開発中に遭遇するよくある疑問をレシピ形式でまとめた本です。iOS4の新機能も満載です。iOSアプリ開発のお供にどうぞ!
2011-04-30
2011-02-04
■[iphone]HTTPリクエストのリクエストヘッダにリファラを設定したい
NSMutableURLRequestクラスのsetValue:forHTTPHeaderField:メソッドを使う。
NSURLRequestではなくNSMutableURLRequestを使うのがポイント。HTTPメソッドやHTTPヘッダをセットしたい場合はプロパティの変更が不可能(Immutable)なNSURLRequestじゃなくて変更可能(Mutable)なNSMutableURLRequestを使う必要があります。
...
NSURL *url = [NSURL URLWithString:@"http://example.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setValue:@"http://referer.example.com" forHTTPHeaderField:@"Referer"];
...
2011-02-02
■[iphone]UIWebView上でJavaScriptを走らせたい
UIWebView上でJavaScriptを走らせたい場合はUIWebViewクラスのstringByEvaluatingJavaScriptFromString:メソッドを利用する。返り値には最後に評価した式の値が格納される。
以下では最後に評価された「a + b」の結果である「3」が返る。
NSString *s = [webView stringByEvaluatingJavaScriptFromString:
@"var a = 1; var b = 2; a + b;"];
NSLog(@"=> %@", s); //=> 3
以下はUIWebView上で選択中の文字列をNSLogで出力する例。
- (IBAction) showSelectedText {
NSString *s;
// 選択中の文字列を取得(なぜかtoString()しないと値が返らない)
s = [webView stringByEvaluatingJavaScriptFromString:
@"document.getSelection().toString()"];
NSLog(@"=> %@", s);
}
2011-01-11
■ SQLiteのスキーマ定義をHTML形式で出力する
SQL形式ので出力なら以下のコマンドでできるんだけど、
$ sqlite3 db_file ".schema"
HTML形式で見たかったのでサックリとシェルスクリプトを書いた。あとでまた使うかもしれないのでメモっておく。
# sqlite_schema.sh sqlitedb=$1 echo "<html><body>" for i in `sqlite3 ${sqlitedb} ".tables"` do echo "<h2>${i}</h2><ul>" for j in `sqlite3 ${sqlitedb} "PRAGMA table_info(${i});" | awk 'BEGIN{FS="|"}{printf "%s(%s)\n", $2, $3}'` do echo "<li>${j}</li>" done echo "</ul>" done echo "</body></html>"
使い方はこんな感じ。
$ sh sqlite_schema.sh db_file
スキーマだけじゃなく中のデータも出力したい場合
中のデータも出力したくなったときはこちらを。
# sh sqlite_dump.sh db_file sqlitedb=$1 echo "<html><body>" for i in `sqlite3 ${sqlitedb} ".tables"` do echo "<h2>${i}</h2>" echo "<table border=1>" echo "<tr>" for j in `sqlite3 ${sqlitedb} "PRAGMA table_info(${i});" | awk 'BEGIN{FS="|"}{printf "%s\n", $2}'` do echo "<th>${j}</th>" done echo "</tr>" sqlite3 -html ${sqlitedb} "select * from ${i};" echo "</table>" done echo "</body></html>"
2010-12-24
■ SQLiteでの「file is encrypted or is not a database」エラー
とあるアプリが作成したsqliteのファイルを開こうと思ったら以下のようなエラーが出て開けなかった。
$ sqlite hoge.sqlite Unable to open database "/opt/services/galaxy/development/current/database/universe.sqlite": file is encrypted or is not a database
調べてみた所、sqlite3で作られたDBをsqlite2で開こうとするとこのエラーが出る事があるらしい。sqliteコマンドを叩いて確認してみたところ確かにこれはsqlite2。
$ sqlite SQLite version 2.8.17 Enter ".help" for instructions sqlite>
ということで、sqlite3をインストールして無事に望むsqliteファイルを開くことができたのでした。
$ sudo aptitude install sqlite3 $ hash -r $ sqlite3 hoge.sqlite SQLite version 3.7.2 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite>
2010-11-15
■[mercurial]指定したディレクトリにMercurialをインストールしたい
指定したディレクトリにMercurialをインストールしたい場合は、以下のようにPREFIXにインストール先のディレクトリを指定してmake installすればOK。
$ make PREFIX=/some/directory/mercurial-1.7 install
以下はおまけ。ダウンロードからインストールして動作確認までの手順。
$ cd /tmp $ wget http://mercurial.selenic.com/release/mercurial-1.7.tar.gz $ tar xzf mercurial-1.7.tar.gz $ cd mercurial-1.7/ $ make PREFIX=/some/directory/mercurial-1.7 install $ /some/directory/mercurial-1.7/bin/hg version
2010-10-10
■[iphone]明日の俺へ
つ http://developer.apple.com/library/ios/#documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/Introduction/Introduction.html
■[iphone][uiview]UIViewのサイズを変更したい
UIViewのサイズの変更のしかたをいつも忘れるので、メモっておく。
// オリジナルの枠を取得 CGRect original = aView.frame; // 枠の高さを半分に CGRect new = CGRectMake(original.origin.x, original.origin.y, original.size.width, original.size.height / 2); // 新しい枠をセットする aView.frame = new;
できれば以下のように書きたいんだけど「lvalue required as left operand of assignment」エラーになっちゃうのがだいぶ罠な感じ。
aView.frame.size.height = 200; //=>「lvalue required as left operand of assignment」エラー!!
aView.frameプロパティの実体はCGRectを返す関数なのと、CGRectはオブジェクトじゃなくてただの構造体なのが合わせ技となってこんなキモイことになってるのかな。ぐぬぬぬ。

