我最近将我的应用程序从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
}
}
答案 0 :(得分:1)
Private Documents/Parse
中可用的唯一文件是:
另一方面,sessionToken存储在钥匙串中
密钥应为$APP_BUNDLE_ID.com.parse.sdk
(Official info和source code,here's是用于保存数据的函数的来源。)
如果您遇到任何问题,here's会出现关于服务器迁移和会话的官方回购