勉強会force#2 html5によるモバイルアプリ開発

26
勉強会force#2 HTML5によるモバイルアプリ開発 ISV Architect 中嶋一樹

Upload: kazuki-nakajima

Post on 18-Nov-2014

2.519 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 勉強会force#2 HTML5によるモバイルアプリ開発

勉強会force#2

HTML5によるモバイルアプリ開発

ISV Architect 中嶋一樹

Page 2: 勉強会force#2 HTML5によるモバイルアプリ開発

Safe HarborSafe harbor statement under the Private Securities Litigation Reform Act of 1995: This presentation may contain forward-looking statements that involve risks, uncertainties, and assumptions. If any such uncertainties materialize or if any of the assumptions proves incorrect, the results of salesforce.com, inc. could differ materially from the results expressed or implied by the forward-looking statements we make. All statements other than statements of historical fact could be deemed forward-looking, including any projections of product or service availability, subscriber growth, earnings, revenues, or other financial items and any statements regarding strategies or plans of management for future operations, statements of belief, any statements concerning new, planned, or upgraded services or technology developments and customer contracts or use of our services. The risks and uncertainties referred to above include – but are not limited to – risks associated with developing and delivering new functionality for our service, new products and services, our new business model, our past operating losses, possible fluctuations in our operating results and rate of growth, interruptions or delays in our Web hosting, breach of our security measures, the outcome of intellectual property and other litigation, risks associated with possible mergers and acquisitions, the immature market in which we operate, our relatively limited operating history, our ability to expand, retain, and motivate our employees and manage our growth, new releases of our service and successful customer deployment, our limited history reselling non-salesforce.com products, and utilization and selling to larger enterprise customers. Further information on potential factors that could affect the financial results of salesforce.com, inc. is included in our annual report on Form 10-Q for the most recent fiscal quarter ended April 30, 2011. This documents and others containing important disclosures are available on the SEC Filings section of the Investor Information section of our Web site. Any unreleased services or features referenced in this or other presentations, press releases or public statements are not currently available and may not be delivered on time or at all. Customers who purchase our services should make the purchase decisions based upon features that are currently available. Salesforce.com, inc. assumes no obligation and does not intend to update these forward-looking statements.

Page 3: 勉強会force#2 HTML5によるモバイルアプリ開発

アーキテクチャ

Page 4: 勉強会force#2 HTML5によるモバイルアプリ開発

3タイプのアプリケーション

Native• オンラインストアからインストール• モバイル端末のH/W機能を活用• リッチなユーザエクスペリエンス• 特殊な開発能力が必要

Page 5: 勉強会force#2 HTML5によるモバイルアプリ開発

Web• ブラウザからアクセス• 端末を問わないシングルコードベース• 限定的なH/Wアクセス• 非互換なイベント• 開発者を確保し易いWebベースの開発

3タイプのアプリケーション

Page 6: 勉強会force#2 HTML5によるモバイルアプリ開発

Web + HTML5• ブラウザからアクセス• 端末を問わないシングルコードベース• 限定的なH/Wアクセス• Nativeのようなモバイル用UI• 開発者を確保し易いWebベースの開発

3タイプのアプリケーション

Page 7: 勉強会force#2 HTML5によるモバイルアプリ開発

Hybrid• オンラインストアからインストール• モバイル端末のH/Wを活用• リッチなユーザエクスペリエンス• 開発者を確保し易いWebベースの開発

3タイプのアプリケーション

Page 8: 勉強会force#2 HTML5によるモバイルアプリ開発

Database

Apex

Visualforce

HTML5

Force.comでのWeb + HTML5アプリ提供形態

Force.com 3rd Party

js / css

Page 9: 勉強会force#2 HTML5によるモバイルアプリ開発

Database

API

HTML5

Database.comでのWeb + HTML5アプリ提供形態

Database.com 3rd Party

js / cssOpen Web Platform

Apex

Page 10: 勉強会force#2 HTML5によるモバイルアプリ開発

サンプルアプリ

Page 12: 勉強会force#2 HTML5によるモバイルアプリ開発

User Interface

Page 13: 勉強会force#2 HTML5によるモバイルアプリ開発

Visualforce PageにjQuery Mobileをロード<apex:page sidebar="false" showHeader="false" standardStylesheets="false" standardController="Session__c" extensions="sugoiSurvey" contentType="text/html" doctype="HTML-5.0"><apex:stylesheet value="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" /><apex:includescript value="http://code.jquery.com/jquery-1.7.1.min.js" /><apex:includescript value="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js" /><head>

<title>{!$label.sugoiSurvey}</title><meta name="viewport" content="width=device-width, initial-scale=1" /><script type="text/javascript">j$ = jQuery.noConflict();</script>

• <apex:stylesheet>,<apex:includescript>でライブラリ(jQuery, jQuery Mobile)をロード• <meta name="viewport">で表示サイズをモバイル端末画面に最適化• jQuery.noConfilict();で$を変更

Page 14: 勉強会force#2 HTML5によるモバイルアプリ開発

jQuery Mobileでのページ構造<div id="session_list" data-role="page"> <div data-role="header"> </div> <div data-role="content">

<a href="#session_detail">Session 1</a> </div> <div data-role="footer"> </div></div>

<div id="session_detail" data-role="page" data-add-back-btn="true"> <div data-role="header"> </div> <div data-role="content"> </div> <div data-role="footer"> </div></div>

• <div data-role="page">で1つの画面を表示• 1htmlファイルに複数の画面を記述可能。デフォルトでは最初のpageを表示。• ページ内リンク<a href="#{ID}">にて画面遷移。各<div>にid="{ID}"を設定しておく• data-add-back-btn="true"でヘッダにbackボタンを自動表示可能

Page 15: 勉強会force#2 HTML5によるモバイルアプリ開発

リスト<div id="session_list" data-role="page" > <div data-role="header"><h1>セッションリスト</h1></div> <div data-role="content"> <ul data-role="listview" data-inset="true" data-filter="true"> <li><a href="#session1">Session 1</a></li> <li><a href="#session2">Session 2</a></li> <li><a href="#session3">Session 3</a></li> </ul> </div></div><div id="session1" data-role="page" data-add-back-btn="true"> <div data-role="header"><h1>セッション詳細</h1></div> <div data-role="content"> <ul data-role="listview"> <li data-role="list-divider">Session 1</li> <li>セッション 1の詳細</li> </ul> </div></div>

• <ul data-role=”listview”>でTouchに適したリストを表示• <ul data-filter=”true”>でリスト絞り込み用サーチボックスを表示• <li data-role=”list-divider”>でリストの見出を設定

Page 16: 勉強会force#2 HTML5によるモバイルアプリ開発

サンプルアプリのトップページ<div id="session_list" data-role="page" data-theme="d"> <div data-role="header"> <h1>{!$label.sessionList}</h1> <a href="#create_session" class="ui-btn-right" data-icon="plus" data-transition="slideup">{!$label.new}</a> </div> <div data-role="content"> <ul data-role="listview" data-inset="true" data-divider-theme="d" data-filter="true" data-split-icon="gear"> <apex:repeat var="session" value="{!sessions}"> <li class="session">

<a href="/apex/sugoiSurveyM_detail?id={!session.id}" data-ajax="false"><h3>{!session.name}</h3><p>

{!session.presenter__r.company__c} {!session.presenter__r.title__c} {!session.presenter__r.name}<br /> {!YEAR(session.Date__c)}.{!MONTH(session.Date__c)}.{!DAY(session.Date__c)} #{!session.hash_tag__c}

</p> <span class="ui-li-count">{!session.rate_avg__c}pt</span>

</a><a href="/apex/sugoiSurveyM_edit?id={!session.id}" data-ajax="false"></a>

</li> </apex:repeat> </ul> </div></div>

Page 17: 勉強会force#2 HTML5によるモバイルアプリ開発

データベース操作

Page 18: 勉強会force#2 HTML5によるモバイルアプリ開発

JavaScript RemotingでApexを実行

j$('#submit_survey').tap(function(){ var rate = j$('input:radio:checked').val(); sugoiSurvey.submitSurveyJs( "{!session__c.id}", rate, function(result, event){ alert(event.message); }, {escape:true} );});

• sugoiSurvey.submitSurveyJs() => {Class名}.{Method名}()で対応するApexを実行• resultでクラスからの返り値を参照• eventでApexからの実行結果を参照

@RemoteActionpublic static void submitSurveyJs(id session, string rate){ survey__c survey = new survey__c(session__c = session, rate__c = rate); insert survey;}

Visulaforce Page

Apex

• @RemoteActionアノテーションを対応Methodに付加• global/publicかつstaticのMethodを定義

Page 19: 勉強会force#2 HTML5によるモバイルアプリ開発

Database.com

Page 20: 勉強会force#2 HTML5によるモバイルアプリ開発

認証

Page 21: 勉強会force#2 HTML5によるモバイルアプリ開発

OAuth (PHP + REST)OAuth Toolkit for PHPをドキュメントルートに設置https://github.com/nkjm/Force.com-OAuth-Toolkit-for-PHP

•認証されているセッションは常に$_SESSION[‘access_token’]にTokenを保持• Tokenがなければ、

1.ローカルのOAuthライブラリにリダイレクト。2.Salesforceの認証サーバにリダイレクトされ、認証 => 「コード」取得。3.ローカルのOAuthライブラリにリダイレクト。SalesforceのTokenサーバからTokenを取得。4.$_SESSION[‘access_token’]にTokenを保存し認証下のコンテンツにリダイレクト。

<?phpsession_start();if (!isset($_SESSION['access_token']) || !isset($_SESSION['instance_url'])) { $_SESSION['oauth_return'] = $_SERVER['PHP_SELF'] . '?' . htmlspecialchars(SID); header( 'Location: /oauth/oauth.php?' . htmlspecialchars(SID));}

認証下に置くphpファイルの先頭

Page 22: 勉強会force#2 HTML5によるモバイルアプリ開発

データベース操作

Page 23: 勉強会force#2 HTML5によるモバイルアプリ開発

OAuth (PHP + REST)

$query = ‘select name from session__c’;$url = $_SESSION[‘instance_url’] . “/services/data/" . VERSION . "/query?q=" . urlencode($query);$curl = curl_init($url);curl_setopt($curl, CURLOPT_HEADER, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth “ . $_SESSION[‘access_token’]));$json_response = curl_exec($curl);$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);if ($status != 200) { if ($status == 401){ $response = json_decode($json_response); if ($response[0]->errorCode == 'INVALID_SESSION_ID') { header( 'Location: oauth/oauth.php' ); } } $curl_error = curl_error($curl); $curl_errno = curl_errno($curl); die("Status Code: $status <br/>Response: $json_response <br/> Curl Error: $curl_error <br/>Curl Errno: $curl_errno");}curl_close($curl);$response = json_decode($json_response, true);return($response);

Tokenを用いたREST APIアクセス

Page 24: 勉強会force#2 HTML5によるモバイルアプリ開発

OAuth (PHP + JavaScript + REST)Force.com JavaScript REST API Toolkit

forcetk.js

https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit

Page 25: 勉強会force#2 HTML5によるモバイルアプリ開発

OAuth (PHP + JavaScript + REST)Force.com JavaScript REST API Toolkitをドキュメントルートに設置[root@server]# ls ${DOCUMENT_ROOT}/forcetk.jsproxy.phpindex.phpoauth/

HTMLから読み込み<!DOCTYPE html><html><head> <title><?php echo LABEL_SUGOISURVEY; ?></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link href="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.min.js"></script> <script type="text/javascript" src="http://code.jquery.com/mobile/1.0.1/jquery.mobile-1.0.1.min.js"></script> <script type="text/javascript" src="forcetk.js"></script></head>

Page 26: 勉強会force#2 HTML5によるモバイルアプリ開発

OAuth (PHP + JavaScript + REST)POSTでレコードを作成<script type=”text/javascript”> $(document).ready(function(){ var $client_id = "[あなたのConsumer ID]"; var $proxy_url = "https://[あなたのWebサーバ]/proxy.php?mode=native";

// REST APIアクセスを抽象化するWrapperを初期化 var client = new forcetk.Client($client_id, null, $proxy_url);

// セッション情報をセット client.setSessionToken('<?php echo $_SESSION["access_token"]; ?>', null, '<?php echo $_SESSION["instance_url"]; ?>');

$('#account_form :button').click(function(){ var $account_name = $('#account_form :text').attr('value');

// createメソッドでAccountレコードを作成。第一引数はオブジェクト名。第二引数はフィールド。第三引数は成功時のコールバック。第四引数はエラー時のコールバック。 client.create( "Account", {"Name":$account_name}, function(response){ alert('Success!'); }, function(jqXHR){ alert(jqXHR.responseText); } ); }); });</script>