GETとPOSTの違い(HTTP)
GETとPOSTの違いを調査
☆HTML specificationsによる定義/推奨
■ GET
データはURLに含まれる。
idempotentな(永続的な効果をもたらさない、副作用をもたらさない)処理を行うときに使うことが推奨されている。 ⇒データを取得するときに使用する。
■ POST
データはmessage body部分に含まれる。
idempotentではない処理を行うときに使うことが推奨されている。 ⇒データのソート、更新、商品の注文、Emailの送信など。
☆まとめ
基本的には、データのREAD処理の場合はGETを使い、CREATE/UPDATE/DELETE処理の場合POSTを使えばよいと思います。あとはセキュリティの観点からセンシティブな情報を送信する場合はPOSTを使うべきです。
→データ取得※検索するキーが重要な情報出ない場合 ○GET
→その他の検索 ○POST
※セキュリティーや、サーバーキャッシュにのるかどうか、ケースバイで考えるといいかも。
【参考】
Tech Tips: GETとPOSTの違い - http://techtipshoge.blogspot.jp/2012/08/getpost.html
フォームデータの送信(GETメソッドとPOSTメソッド) - tachikawa844の日記 - http://d.hatena.ne.jp/tachikawa844/20081017/1224220981
POSTとGETの違い - Perl日誌 - http://d.hatena.ne.jp/okamuuu/20100525/1274800489
以上、猫背が直らない堀でした。
iPhoneアプリのHTTP送信をトレースする方法
必要になったので、調査。
Mac用のHTTPデバッガツール Charles
設定方法
iPhoneアプリをHTTPデバッグする方法 - 中継地点 - http://d.hatena.ne.jp/h_mori/20120201/1328054223
Charles Web Debugging Proxy の使い方 | gaspanik weblog - http://blog.gaspanik.com/how-to-setup-charles-web-debugging-proxy
以上、YouTubeのリンク先を辿っていくとなぜかゲーム動画を見てしまう堀でした。
Objective-CでZipファイルを解凍する方法
zipファイルをダウンロードして、解凍する必要があったため、メモ
まずは、以下を使います。
http://code.google.com/p/ziparchive/
これをダウンロードしたら、プロジェクトに追加して、
libz.1.2.3.dylib
も追加します。
あと、展開するZIPファイルもプロジェクトに入れときます。
NSString *path = [[NSBundle mainBundle] pathForResource:@"wallpaper" ofType:@"zip"]; NSString *outdir = [NSHomeDirectory() stringByAppendingPathComponent:@"tmp"]; NSLog(@"%@",path); ZipArchive *za = [[[ZipArchive alloc] init] autorelease]; [za UnzipOpenFile:path]; BOOL ret = [za UnzipFileTo:outdir overWrite:YES]; if(ret==NO) { // エラー処理 NSLog(@"zip archive 解凍時にエラーが発生"); } [za UnzipCloseFile]; NSString *imagePath = [outdir stringByAppendingPathComponent:@"Dark-Souls_Wallpaper_1280X800.jpg"]; NSLog(@"[%@]",imagePath); NSURL *url = [[NSURL alloc] initFileURLWithPath:imagePath]; NSData *data = [[NSData alloc] initWithContentsOfURL:url]; UIImage *image = [[UIImage alloc] initWithData:data]; UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; [self.view addSubview:imageView]; imageView.image = image; [imageView release]; [url release]; [data release]; [image release];
解凍したファイルは、outdirで指定しているように、そのアプリ内のtmpディレクトリに展開されます。
シミュレータのディレクトリで確認できる。
展開後、ファイル名を指定してURLに変換し、それをデータとして受け取り、UIImageにする、と。
これで完了です。
【参考】
Objective-CでZipファイルを解凍してそれを活用する方法 | ssdkfk - http://ssdkfk.wordpress.com/2011/10/03/objective-c%E3%81%A7zip%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E8%A7%A3%E5%87%8D%E3%81%97%E3%81%A6%E3%81%9D%E3%82%8C%E3%82%92%E6%B4%BB%E7%94%A8%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/
【こっちもある】
iOSアプリ開発達人レシピ100より。
mattconnolly/ZipArchive - https://github.com/mattconnolly/ZipArchive
以上、ピザにはタバスコは付けない派の堀でした。
JSONパーサーについて(SBJSON vs NSJSONSerialization)
JSON使おうと思って調べました。
nativeなJSONパーサー「NSJSONSerialization」をつかうと良い。
※パースする速度、今後のiOSアップデートを考えて
ios - Serialize JSON string SBJSON vs NSJSONSerialization vs anything else? - Stack Overflow - http://stackoverflow.com/questions/15891825/serialize-json-string-sbjson-vs-nsjsonserialization-vs-anything-else
※アップルのJSONパーサーは早くなってるから、サードパーティ製のJSONパーサーはいらないよとの記事
SBJSON ios 5 or native apple JSON - Stack Overflow - http://stackoverflow.com/questions/14159917/sbjson-ios-5-or-native-apple-json
※SBJSONよりNSJSONSerializationの方が早いよって話らしい。
以上、映画見る時はポップコーンいるだろ!とついつい言ってしまう堀でした。
ロード中のメッセージ表示
ロード中のメッセージを表示するのは何が楽かを調べたら、出来てきたので、メモ
メッセージのローディング処理のメッセージ
【参考】
必要に迫られたので超シンプルなiOS用ローディング表示ライブラリ作ってみた - tobioka.net - You Ain't Gonna Need It - http://tobioka.net/438
以上、最近ロードバイクに乗っていないので、趣味とは言えなくなりつつある堀でした。
iOSのNativeからJqueryと連携する方法
nativeからjqueryを呼び出すと気に悩んだので、メモ
①Jquery→native
JqueryのイベントからNativeのイベントを呼び出す。
//JS //座標LOG出力処理 $.get('native://getSystemInfo', function (systemInfo) { $('#placeholder').text(mouse.x + "|" + mouse.y); });
//iOS(ViewController) #pragma mark - UIWebViewからNativeの処理を呼び出す処理 // NativeProtocolの処理を行う -(void)invokeNativeMethod : (NSNotification *)notification { NativeProtocol *protocol = notification.object; NSURLRequest *request = protocol.request; // native://closeWebViewが指定された場合 if ([request.URL.host isEqualToString:@"closeWebView"]) { [ self performSelectorOnMainThread:@selector(closeWebView) withObject:nil waitUntilDone:NO]; } // native://getSystemInfoが指定された場合 else if ([request.URL.host isEqualToString:@"getSystemInfo"]) { NSString *systemInfo = [ self getSystemInfo ]; // 値を返す [protocol sendResponse: systemInfo]; } }
// システムの情報をWebView内に表示する。 -(NSString *)getSystemInfo { // システム情報を取得する UIDevice *device = [ UIDevice currentDevice]; NSString *systemInfo = [ NSString stringWithFormat:@"name=%@, version=%@", device.systemName, device.systemVersion ]; return systemInfo; }
//iOS(NativeProtocol)※カスタム + (BOOL)canInitWithRequest:(NSURLRequest *)request { return [[[request URL] scheme] isEqualToString:@"native"]; }
+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request { return request; }
- (void)startLoading { NSNotificationCenter *center = [ NSNotificationCenter defaultCenter ]; [ center postNotificationName:@"invokeNativeMethod" object:self userInfo:nil]; }
- (void)stopLoading { /* nothing to do */ }
// レスポンスを返す - (void) sendResponse :(NSString *)body { NSData *data = [body dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys: @"text/plain", @"Content-Type", [NSString stringWithFormat:@"%d", [data length]], @"Content-Length", nil]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] initWithURL:[self.request URL] statusCode:200 HTTPVersion:@"1.1" headerFields:headers]; [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageAllowedInMemoryOnly]; [self.client URLProtocol:self didLoadData:data]; [self.client URLProtocolDidFinishLoading:self]; }
②native→Jquery 引数あり 戻り値なし
xPointData = 0,yPointData = 0; //jQueryの実行文字列を作成 //render([x軸],[y軸]); NSString *str1 = @"render(["; NSString *str2 = @"],["; NSString *str3 = @"]);"; [_mapWebView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"%@%d%@%d%@",str1,xPointData,str2,yPointData,str3]];
③native→Jquery 引数なし 戻り値あり
NSString *str = [_mapWebView stringByEvaluatingJavaScriptFromString:@"render();"]; NSLog(@"回数は%@", str);
【参考URL】
※実践済iOS:objective-cとjavaScriptの連携(1) | Design Drill Diary - http://designdrill.jp/wordpress/?p=6569
※実践済iPhoneのネイティブ機能をWebViewから呼び出す方法(2):ADWAYS ENGINEERS BLOG - http://blog.engineer.adways.net/archives/13813985.html
UIWebViewでWebとネイティブを相互連携させる方法について - プログラミングノート - http://d.hatena.ne.jp/ntaku/touch/20111103/1320288456
【iOS】UIWebView Hacks 〜ブラウザ開発テクニック〜 (フェンリル | デベロッパーズブログ) - http://blog.fenrir-inc.com/jp/2013/07/uiwebview-hacks.html
以上、犬派か猫派と聞かれると、即答できない堀でした。
iosでローカルHTML、javascriptを動かす方法
HTMLはリソースファイルのである為、プログラム内で、ファイルパスの取得が可能(ファイルが存在するから)だが、
javascriptファイル(jsファイル)については、コンパイル時、リソースファイル対象外の為、リソースファイルにコピーされない。
従って、ファイルが存在しないこととなり、ファイルパスが取得出来ない。
【HTML】※これでOK!
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"default_control" ofType:@"html"]; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
【JavaScript】※これだけじゃ、取得出来ない。
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"ajax_handler" ofType:@"js"]; NSString *js = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
以下の操作を実行
XCODEプロジェクトでプロジェクトを選択し、「Build Phases」タブを開きます。
「Copy Bundle Resources」にリソースとして読み込みたいJSを指定します。
※JavaScriptがリソースとしてコピーされるようになる。
【参考URL】
[XCODE] JavaScriptファイルをXCODEプロジェクトにResourceファイルとして読み込む方法 - YoheiM .NET - http://www.yoheim.net/blog.php?q=20130405
以上、先日の地震に気づかないことに焦りを覚えた堀でした。