2014年3月19日水曜日

ios 同期通信でpostする

iosで同期通信でpostします。

いきなりcodeです。

     
    // Send a synchronous request
    NSURL *url = [NSURL URLWithString:@"url名"];
    // post情報をくっつけるので、NSMutableURLRequestを使用
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // Set the method(HTTP-POST)
    [request setHTTPMethod:@"POST"];
    // Set the request-body.
    NSString *reqBody = [NSString stringWithFormat:@"login[account]=%@&login[password]=%@",account名,password];
    // utf8にする
    [request setHTTPBody:[reqBody dataUsingEncoding:NSUTF8StringEncoding]];
    
    // 吐き出すエラー
    NSError * error = nil;
    NSURLResponse* response = nil;
    
    
    
    NSData* data = [NSURLConnection sendSynchronousRequest:request
                                          returningResponse:&response
                                                      error:&error];
    // 通信が行なわれた後に実行されます。
   // 電波が悪く通信できなかった(errorStringにinternet can't useとかデル)
    NSString *errorString = [error localizedDescription];
    if(0 < [errorString length])
    {
       // error処理
    }
    // 通信成功
    else
    {
       // utf8で返った情報をとってくる
       NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
       // jsonデーターをとってこよう
       NSDictionary* jsonObj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
       // 返った情報を表示
       NSLog(@"result: %@", result);
    }


解説は、コメントに書いたので、特にないっすね。

2014年3月18日火曜日

ios Bundle Identifierの初期値を設定する方法

ios Bundle Identifierの初期値を設定する方法です。

githubから参考したいプロジェクトをダウンロードした時に、
bundle identifierに元で設定してあった値が代入されてしまっていたので、
それを直したいと思って調べました。


まず、プロジェクトのplistファイルをクリックします。


すると、次の画面が出てきます。


Bundle identifierを変えてください。

2014年3月3日月曜日

ios xibで作ったカスタムセルをTableViewに登録する方法

ios xibで作ったカスタムセルをTableViewに登録する方法

まずは、対象となるUITableViewにxib部品を登録します。


    UINib *nib = [UINib nibWithNibName:@"ButtonCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"PagingCell"];
登録するxibファイル名をnibWithNibNameに登録し、 xibで作成した、セル名を登録します。
そして、呼び出す時は、次のように記述します。

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    

    PagingCell *cell = [_tableView dequeueReusableCellWithIdentifier:@"PagingCell"];
    // セル特有の処理

    return cell;
}



cxc