SSCheckBoxView

SSCheckBoxView for iOS

さまざまなパターンのチェックボックス コントロールが利用できます。
iPhone ではテーブル ビュー(行チェック)を利用すれば、チェックボックス コントロールを利用する局面はあまりないかと思いますが、iPad では結構重宝するかと思います。
リソースを作らなくて良いことだけでも嬉しいです。(^^)v

github

グリッド コントロール風のテーブル ビュー

– iPad で表形式のデータ表示にベストなコントロール

いろいろ表形式のコントロールは紹介されているようですが、単にデータを表示するだけならこのコントロールが扱いやすかったです。

MultiColumnTableViewForiOS for iOS
紹介記事
github

シングルトン パターン オブジェクトの解放

– シングルトン パターンの生成と解放

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
static hoge *sInstance = nil;
 
@implementation hoge
 
+ (id)sharedInstance 
{
    @synchronized(self) {   // スレッドセーフとする
	if (!sInstance) {
            sInstance = [[self alloc] init];
        }
    }
 
    return sInstance;
}
 
+ (void)releaseInstance
{    
    [sInstance release];
    sInstance = nil;        // インスタンスにアクセスされても問題がないように nil をセットする
}
 
@end

xcode4 で新規デバイスへアプリインストールが簡単になっている

以前は突発的に普段開発に利用していないデバイスへアプリをインストールすることはとても大変な作業でした。

理由は、
1) 新規デバイスの UDID を把握する
2) iOS Developer Program へアクセスし、新規 UDID を登録し、新たにプロビジョニング ファイルを作成する
3) 新規デバイスに新たに作成したプロビジョニング ファイル、アプリをインストールする
上記の作業が大変であったからだと思います。

ですが、xcode4 では、上記の作業が2つのボタン押下で行えるようになっています。

開発中アプリを新たなデバイスへアプリをインストールする場合は、
1) デバイスを mac に接続し、オーガナイザを起動する
2) デバイスが認識されている場合、”Devices”に表示されているので、”Use for Development”ボタンを押下する
“Sign in with your Apple ID” といわれるので、Username と Password を入力します。
3) 画面下部の “Add to Portal”ボタンを押下します。

新規デバイスが iOS Developer Program の開発用デバイスとして登録され、プロビジョニング ファイルがインストールされているはずです。

UITableView が reloadData されない場合に確認する点

UITableView に reloadData メソッドを発行しても cellForRowAtIndexPath がコールされない場合は、テーブルビューの高さが 0 に設定されていないか?確認すること。

テーブルビューの高さが 0 に設定されていると、
– (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

上記のメソッドがコールされても、
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
が、コールされない。

iOS で JSON 形式データを作成する方法

json-framework → SBJson
TouchJSON
などが有名のようである。(2011/09時点)

ー SBJson を使用して、JSON 形式データを出力する
値に数値を指定したい場合、NSNumber をセットすればよい。

1
2
3
4
5
6
7
8
9
10
11
12
NSDictionary *dict0, *dict1;
dict0 = [NSDictionary dictionaryWithObjectsAndKeys:@"value00", @"key00", 
                                                   @"value01", @"key01", nil];
dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"value10", @"key10", 
                                                   @"value11", @"key11", nil];
NSArray *array = [[NSArray alloc] initWithObjects:dict0, dict1, nil];
 
SBJsonWriter *writer = [[[SBJsonWriter alloc] init] autorelease];
writer.humanReadable = YES;
writer.sortKeys = YES;
 
NSLog(@"%@", [writer stringWithObject:array]);

– 出力結果

1
2
3
4
5
6
7
8
9
10
[
  {
    "key00" : "value00",
    "key01" : "value01"
  },
  {
    "key10" : "value10",
    "key11" : "value11"
  }
]

iOS デバイスから WCF サーバへ POST でパラメータ (JSON形式) を送信する方法

– ASIFormDataRequest の場合

ASIFormDataRequest クラスを使用して POST する場合、Content-Type は以下のものが指定される。
‘application/x-www-form-urlencoded’
‘multipart/form-data’

上記以外の Content-Tpye を明示的に指定し、POST する場合は以下の様にする。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Http通信 postでパラメータ(json形式)をサーバへ渡す
NSString *interfaceURL;
interfaceURL = [NSString stringWithFormat:@"http://(サーバ アドレス)"];    
 
NSURL *url = [NSURL URLWithString:interfaceURL];
request_ = [[ASIFormDataRequest alloc] initWithURL:url];
request_.delegate = self;
[request_ addRequestHeader:@"Content-Type" value:@"application/json; charset=utf-8"];
 
// WCFインタフェースへ渡すパラメータを json 形式に変換する
// NSDictionary → (json文字列) NSString
NSDictionary *dict;
dict = [NSDictionary dictionaryWithObjectsAndKeys:@"(値1)", @"(キー1)",  
                                                  @"(値2)", @"(キー2)", 
                                                  nil];
 
SBJsonWriter *writer = [[[SBJsonWriter alloc] init] autorelease];
writer.humanReadable = YES;
writer.sortKeys = YES;
NSLog(@"送信パラメータ:%@", [writer stringWithObject:dict]);
 
NSString *postParameterString;
postParameterString = [NSString stringWithFormat:@"%@", [writer stringWithObject:dict]];
 
// パラメータをボディにセットし、WCF サーバへ通信する
[request_ appendPostData:[postParameterString dataUsingEncoding:NSUTF8StringEncoding]];    
[request_ startAsynchronous];

NSDate, time_t の相互変換

1
2
3
4
5
// NSDate から time_t へ変換する
time_t now = (time_t) [[NSDate date] timeIntervalSince1970];
 
// time_t から NSDate へ変換する
NSDate *someDate = [NSDate dateWithTimeIntervalSince1970:now];

iOS デバイスからWCFサーバへファイルをアップロードする方法

WCFサーバ側 C#プログラム
WCF でのエンドポイント、バインディング、コントラクトなどは適当に設定しておく。

公開インタフェース定義

1
2
3
4
5
6
7
  [ServiceContract(Namespace = "http://My.WCF.Samples")]
  public interface IService1
  {
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadFile/{fileName}")]
    string UploadFile(string fileName, Stream fileContents);
  }

アップロードされたファイルデータを保存する
クライアントから受信したファイルデータを c:¥ 直下に保存する

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
namespace HelloWCF2
{
  public class Service1 : IService1
  {
    public string UploadFile(string fileName, Stream fileContents)
    {
        Console.WriteLine("received file name : {0}", fileName);
        byte[] buffer = new byte[1000000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileContents.Read(buffer, 0, buffer.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);
        Console.WriteLine("Uploaded file {0} with {1} bytes", fileName, totalBytesRead);
 
        System.IO.FileStream fs = new System.IO.FileStream(@"C:\myphoto.png",
                                                            System.IO.FileMode.Create,
                                                            System.IO.FileAccess.Write);
        fs.Write(buffer, 0, totalBytesRead);
        fs.Close();
 
        return "画像を受信しました";
    }
  }
}

iOS デバイス側 objective-c プログラム
iOS デバイスから .png 画像をアップロードするパターン1 (ASIFormDataRequest を使用する場合)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    // PNG画像を WCF サーバへ送信する
    // 1. ASIFormDataRequest を使用する場合
    NSURL *url = [NSURL URLWithString:@"http://192.168.x.x:8000/WCFSampleService/HelloWCF/UploadFile/myphoto.png"];
    request_ = [ASIFormDataRequest requestWithURL:url];
 
    [request_ setPostValue:@"myphoto1.png" forKey:@"fileName"];
 
    UIImage *image = [UIImage imageNamed:@"myphoto1.png"];
    NSData* imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
    [request_ appendPostData:imageData];
 
    [request_ setRequestMethod:@"POST"];
 
    [request_ setDidFinishSelector:@selector(requestFinished:)];
    [request_ setDidFailSelector:@selector(requestFailed:)];
    [request_ setDelegate:self];
    [request_ startAsynchronous];

iOS デバイスから .png 画像をアップロードするパターン2 (NSURLConnection を使用する場合)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    // 2. NSURLConnection を使用する場合
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://192.168.0.8:8000/WCFSampleService/HelloWCF/UploadFile/myphoto.png"]];
    [request setHTTPMethod:@"POST"];
 
    NSMutableData *postBody = [NSMutableData data];
    UIImage *image = [UIImage imageNamed:@"myphoto1.png"];
    NSData* imageData = [[NSData alloc] initWithData:UIImagePNGRepresentation(image)];
    [postBody appendData:[NSData dataWithData:imageData]];
    [request setHTTPBody: postBody];
 
    // 非同期通信
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if (conn == nil) {
        // エラー処理
    }

cocos2d

1. cocos2d 公開ページ
cocos2d

2. インストール方法
$ cd (cocos2dフォルダ)
$sudo ./install-templates.sh

xcodeでプロジェクトの新規作成時に cocos2d が選択できるようにプロジェクト テンプレートの登録が行われる

3. cocos2dで作る iPhone&iPadゲームプログラミング
インプレス社のサポートページ
著者のサポートページ