我正在尝试实施酷炫的Firebase电子邮件链接登录功能,但尝试失败。我已成功设置发送电子邮件链接。但是,我无法通过电子邮件链接打开该应用程序。它只是打开预览页面,就像无法打开应用程序一样。
我已经测试了我设置的动态链接,可以在设备中打开该应用程序。我只是无法获得电子邮件链接来执行相同的操作。
我的应用中的代码:
func sendFirebaseEmailLink() {
let actionCodeSettings = ActionCodeSettings.init()
// userEmail comes from a textField
let email = userEmail
actionCodeSettings.url = URL.init(string: String(format: "https://<myappname>.firebaseapp.com/?email=%@", email))
// The sign-in operation has to always be completed in the app.
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
Auth.auth().sendSignInLink(toEmail: email,
actionCodeSettings: actionCodeSettings) { error in
if let error = error {
print(error.localizedDescription)
return
}
else {
UserDefaults.standard.set(email, forKey: "Email")
print("email sent to user")
}
}
}
当我说我已成功获取动态链接以打开该应用程序时,我的意思是当我在安装了该应用程序的设备上跟随我创建的链接(mylinkname.page.link/emaillogin)时,它将打开应用程式。因此,[此有用的Firebase视频] [1]设置了动态链接,似乎我已正确设置了这些详细信息,而问题出在代码中,但是我对此并不陌生,所以我不确定
我花了几天时间来解决这个问题,并尝试解析密集的Firebase文档,所以任何想法都将不胜感激。
答案 0 :(得分:0)
我终于明白了。代码很好。这是与动态链接有关的问题。我在Firebase中设置了几个链接,因为我必须一次创建一个新的Bundle ID。当我在Firebase中删除旧版本时,电子邮件链接开始起作用。
它以这种方式显示在我的应用程序关联站点中,即使我删除了旧链接,它仍然奇怪地起作用,但至少现在可以了!
{“ applinks”:{“ apps”:[],“ details”:[{“ appID”:“ TEAMID.com.OLDBUNDLEIDENTIFIER.APPNAME”,“路径”:[“ NOT / / *” ,“ / *”]},{“ appID”:“ TEAMID.com.NEWBUNDLEIDENTIFIER.APPNAME”,“路径”:[“ NOT / / ”,“ / ”]} ]}}
更新:我用于实现无密码电子邮件登录的完整代码如下。使用文档整理起来使我很痛苦,因此希望可以省去您的麻烦。
假设您了解Firebase安装基础知识的关键步骤。
1)使用Firebase Video tutorial设置动态链接。
2)View Controller中的代码:
var userEmail: String?
var link: String?
func sendFirebaseEmailLink() {
let actionCodeSettings = ActionCodeSettings.init()
let email = userEmail
actionCodeSettings.url = URL.init(string: String(format: "https://<myappname>.page.link/emaillogin/?email=%@", email!))
// The sign-in operation has to always be completed in the app.
actionCodeSettings.handleCodeInApp = true
actionCodeSettings.setIOSBundleID(Bundle.main.bundleIdentifier!)
Auth.auth().sendSignInLink(toEmail: email!,
actionCodeSettings: actionCodeSettings) { error in
if let error = error {
print(error.localizedDescription)
return
}
else {
UserDefaults.standard.set(email, forKey: "Email")
print("email sent to user")
}
// TODO: Notify user to check email and click the link.
}
}
// Sign in user after they clicked email link called from AppDelegate
@objc func signInUserAfterEmailLinkClick() {
// Get link url string from the dynamic link captured in AppDelegate.
if let link = UserDefaults.standard.value(forKey: "Link") as? String {
self.link = link
}
// Sign user in with the link and email.
Auth.auth().signIn(withEmail: userEmail!, link: link!) { (result, error) in
if error == nil && result != nil {
if (Auth.auth().currentUser?.isEmailVerified)! {
print("User verified with passwordless email")
// TODO: Do something after user verified like present a new View Controller
}
else {
print("User NOT verified by passwordless email")
}
}
else {
print("Error with passwordless email verfification: \(error?.localizedDescription ?? "Strangely, no error avaialble.")")
}
}
}
3)AppDelegate中的代码
// For Passwordless Email Login to Handle Dynamic Link after User Clicks Email Link
func application(_ application: UIApplication, continue userActivity: NSUserActivity,
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
if let incomingURL = userActivity.webpageURL {
print("Incoming URL is \(incomingURL)")
// Parse incoming
let linkHandled = DynamicLinks.dynamicLinks().handleUniversalLink(incomingURL) { (dynamicLink, error) in
guard error == nil else {
print("Found an error: \(error!.localizedDescription)")
return
}
if let dynamicLink = dynamicLink {
self.handleIncomingDynamicLink(dynamicLink)
}
}
if linkHandled {
return true
}
else {
// Maybe do other things with dynamic links in future?
return false
}
}
return false
}
// Handles the link and saves it to userDefaults to assist with login.
func handleIncomingDynamicLink(_ dynamicLink: DynamicLink) {
guard let url = dynamicLink.url else {
print("My dynamic link object has no url")
return
}
print("Incoming link parameter is \(url.absoluteString)")
let link = url.absoluteString
if Auth.auth().isSignIn(withEmailLink: link) {
// Save link to userDefaults to help finalize login.
UserDefaults.standard.set(link, forKey: "Link")
// Send notification to ViewController to push the First Time Login VC
NotificationCenter.default.post(
name: Notification.Name("SuccessfulPasswordlessEmailNotification"), object: nil, userInfo: nil)
}
}
答案 1 :(得分:0)
iOS 13以上版本的提示:
如果您支持iOS 13+,则可能还需要将动态链接处理添加到SceneDelegate
方法中:
func scene(_ scene: UIScene, continue userActivity: NSUserActivity)
在我只支持13岁以上的情况下,我的切入点是SceneDelegate
,而我的func application(_ application: UIApplication, continue userActivity:
并未受到打击。