快速回答——是的!
我们刚刚完成了将一款 Android 应用推向市场,该应用可以做到这一点。我们是这样做的:
1) 下载并学习在 Eclipse 中使用 Cordova PhoneGap(2.2.0 为最新版本)。只需一些 HTML 和大量的 Javascript,这使得整个事情变得更加容易。
2) 在您的 JS 中,创建使用 AJAX 参数推送登录信息的方法。示例:
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
$("#login").click(function() {
$email = $("#UserEmail").val();
$pass = $("#UserPassword").val();
$.ajax({
url : yourURL + 'api/users/login',
async : false,
data : {
'email' : $email,
'password' : $pass
},
dataType : 'json',
type : 'post',
success : function(result) {
/**
* do your login redirects or
* use localStorage to store your data
* on the phone. Keep in mind the limitations of
* per domain localStorage of 5MB
*/
// you are officially "logged in"
window.location.href = "yourUrl.html";
return;
},
error : function(xhr, status, err) {
// do your stuff when the login fails
}
});
}
}
3) 在 Cake / PHP 中,您的用户控制器将在 AJAX 调用中获取用户名和密码数据并将其用于身份验证。
<?php
class UsersController extends AppController {
public $name = 'Users';
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('api_login');
}
public function api_login() {
$this->autoRender = false;
if ($this->request->data && isset($this->request->data['email']) && isset($this->request->data['password'])) {
$arrUser = $this->User->find('all',array(
'conditions'=>array(
'email'=> $this->request->data['email'],
'password' => $this->Auth->password($this->request->data['password']),
)
)
);
if (count($arrUser) > 0) {
$this->Session->write('Auth.User',$arrUser[0]['User']);
// Do your login functions
$arrReturn['status'] = 'SUCCESS';
$arrReturn['data'] = array( 'loginSuccess' => 1,'user_id' => $arrUser[0]['User']['id'] );
} else {
$arrReturn['status'] = 'NOTLOGGEDIN';
$arrReturn['data'] = array( 'loginSuccess' => 0 );
}
} else {
$arrReturn['status'] = 'NOTLOGGEDIN';
$arrReturn['data'] = array( 'loginSuccess' => 0 );
}
echo json_encode($arrReturn);
}
}
?>
差不多就是这样。您现在已通过 CakePHP 的身份验证。
您不需要使用“api_”,您可以使用任何您想要的函数名称,但这有助于我们掌握我们允许移动用户与网络用户执行的操作。
现在,这些只是构建块。您基本上必须使用 HTML 和 Javascript 在手机上创建网站的完整版本,因此根据您的应用程序,为您的网站创建响应式设计并允许移动浏览可能会更容易。
HTH!