小智 8
快速回答 - 是的!
我们刚刚将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
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
3)在Cake/PHP中,您的Users控制器将在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);
}
}
?>
Run Code Online (Sandbox Code Playgroud)
这就是它.您现在已经过CakePHP的身份验证.
您不需要使用"api_",您可以使用您想要的任何功能名称,但这有助于我们处理我们允许移动用户与网络用户进行的操作.
现在,这些只是构建块.您基本上必须使用HTML和Javascript在手机上创建整个版本的网站,因此根据您的应用程序,可能更容易为您的网站创建响应式设计并允许移动浏览.
HTH!
归档时间: |
|
查看次数: |
3789 次 |
最近记录: |