直接在iOS Swift上获取解析服务器会话令牌

时间:2017-07-17 14:03:04

标签: ios swift parse-platform parse-server

我最近将我的应用程序从Parse Server迁移到我自己的自定义服务器。我试图找到一种方法来访问会话令牌,而无需在我的项目中导入Parse SDK。我需要会话令牌,所以我不必重新验证我当前登录的用户。

到目前为止,我使用以下方法访问它,这需要导入解析sdk:

        PFUser.current()?.saveInBackground(block: { [weak self] (success, error) in
            if let error = error {
                Log.error?.message(error.localizedDescription)
                return
            }
            guard let sessionToken = PFUser.current()?.sessionToken else {
                Log.error?.message("Did not get session token even after saving user")
                return
            }
        })

我看到的另一个解决方案是parse的私有文档目录,但我只能从那里获得applicationId,installationId和config:

class ParseHelper {    
    var parsePrivateDocumentsDirectory: URL? {
        let filemanager = FileManager.default
        guard let libraryDirectory = filemanager.urls(for: .libraryDirectory, in: .userDomainMask).last else {
            return nil
        }

        guard let parseDirectory = URL(string: "Private%20Documents/Parse/", relativeTo: libraryDirectory) else {
            return nil
        }
        return parseDirectory
    }

    func parseFile(_ filename: String) -> URL? {
        let filemanager = FileManager.default
        guard let parseDirectory = self.parsePrivateDocumentsDirectory else {
            return nil
        }

        guard let installationIdUrl = URL(string: filename, relativeTo: parseDirectory) else {
            return nil
        }

        if filemanager.fileExists(atPath: installationIdUrl.path) {
            return installationIdUrl
        } else {
            return nil
        }
    }

    var installationId: String? {
        guard let fileURL = self.parseFile("installationId"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }

    var applicationId: String? {
        guard let fileURL = self.parseFile("applicationId"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }

    var config: String? {
        guard let fileURL = self.parseFile("config"), let data = try? Data(contentsOf: fileURL) else {
            return nil
        }
        let string = String(data: data, encoding: String.Encoding.utf8)
        return string
    }
}

1 个答案:

答案 0 :(得分:1)

Private Documents/Parse中可用的唯一文件是:

  • 的applicationID
  • currentInstallation
  • currentUser
  • installationId

另一方面,sessionToken存储在钥匙串中 密钥应为$APP_BUNDLE_ID.com.parse.sdkOfficial infosource codehere's是用于保存数据的函数的来源。)

如果您遇到任何问题,here's会出现关于服务器迁移和会话的官方回购