7

我希望我的 PFSessions 是独占的,这意味着,如果用户已经在某个位置的某个设备上登录,如果另一个设备使用相同的凭据登录,我希望上一个会话终止,并显示一条消息当然是警报视图。有点像旧的 AOL 即时消息格式。那么有谁知道我将如何迅速做到这一点?它需要CoreLocation吗?

我在当前用户位置上找到了这篇文章,如何将其用作我问题解决方案的一部分?

https://www.veasoftware.com/tutorials/2014/10/18/xcode-6-tutorial-ios-8-current-location-in-swift

更新

所以我刚刚阅读了这篇关于他们可撤销会话设置的解析文章

http://blog.parse.com/announcements/announcing-new-enhanced-sessions/

但是,当我用不同的设备登录同一个帐户时,会话被允许相应地存在,这是我不想要的。我该如何解决我的困境?

更新

我已经得到了关于如何实现我试图实现的整体方法的非常详细的描述:

在此处输入图像描述

但是我不太精通云代码实现,有人可以非常简要地描述一段类似于他试图传递给我的代码吗?

更新

所以我做了一些更多的研究,并与我被告知如何在解析中使用云代码调用有关,并且我想销毁 currentUser 以前的会话,我在我的登录“成功”中编写了以下代码“逻辑:

            PFUser.logInWithUsernameInBackground(userName, password: passWord) {
        (user, error: NSError?) -> Void in
        if user != nil || error == nil {
            dispatch_async(dispatch_get_main_queue()) {
                self.performSegueWithIdentifier("loginSuccess", sender: self)

                 PFCloud.callFunctionInBackground("currentUser", withParameters: ["PFUser":"currentUser"])
                    //..... Get other currentUser session tokens and destroy them

            }

        } else {

如果那甚至是正确的格式或代码,但我确定这是正确的方向,对吗?任何人都可以编辑或扩展我想要实现的代码吗?

4

1 回答 1

2

您应该首先查看云代码快速入门
然后定义以下云代码函数:

Parse.Cloud.define('destroyUserSessions', function(req, res) {
    //the user that sends the request
    var currentUser = req.user;
    //send from client 
    var currentUserInstallationId = req.param.installationId;
    var Session = Parse.Object.extend('Session');
    var userSessionQuery = new Parse.Query(Session);
    //all sessions of this user
    userSessionQuery.equalTo('user', currentUser);
    //except the session for this installation -> to not log the request performing user out
    userSessionQuery.notEqualTo('installationId', currentUserInstallationId);

    userSessionQuery.find({
        success: function(userSessionsToBeRevoked) {
            Parse.Object.destroyAll(userSessionsToBeRevoked, {
                success: function() {
                    //you have deleted all sessions except the one for the current installation
                    var installationIds = userSessionsToBeRevoked.map(function(session) {
                        return session.installationId;
                    }); 
                    //you can use the installation Ids to send push notifications to installations that are now logged out
                },
                error: function(err) {
                    //TODO: Handle error
                }
            });
        }, 
        error: function(err) {
            //TODO: Handle error
        }
    });
});

注意:此代码未经测试,并做了几个假设,例如您启用了可撤销会话,并且在执行请求时有用户登录

你会这样调用函数:

let installationId = PFInstallation.currentInstallation().installationId

PFCloud.callFunctionInBackground("destroyUserSessions", withParameters: ["installationId": installationId]) { success, error in 
    //TODO: Handle success or errors
}

希望这可以让你开始。

于 2015-10-18T14:20:53.030 回答