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
以上、犬派か猫派と聞かれると、即答できない堀でした。