ios – 如何使用parse终止重复的用户会话?

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

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

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

UPDATE

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

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

但是,当我使用其他设备登录同一帐户时,会话被允许相应地生活,这是我不想要的.我该如何解决我的困境?

UPDATE

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

但是我不太熟悉云代码实现,有人可以非常简单地描绘一段类似于他试图转发给我的代码吗?

UPDATE

所以我做了一些更多的研究,并且在我关于如何在解析中使用云代码调用方面被告知,并且我想要销毁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 {

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

解决方法

你应该从查看 cloud code quick start开始
然后,您将定义以下云代码功能
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
}

希望这可以帮助你入门.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...