我是iOS Swift的新手。我在我的应用程序中添加了LocalAuthentication(TouchID)功能。启用触摸ID时,如果身份验证成功,我将在仪表板页面上重定向用户。现在,当收到推送通知时,如果它是类别类型博客,则我想打开博客页面;如果它是类别类型支出,则我想打开支出页面;如果是新闻,则我想打开通知页面。但是由于启用了触摸ID,我将重定向到仪表板页面,而不是在收到通知的特定ViewController上。不了解如何应对这种情况。
收到通知时我在做什么:
func navigationToView(userInfo:[AnyHashable:Any]){
print("CLICKCATEGORY \(category)")
if(category == "payout_alerts"){ // if notification is payout alert open payout alerts controller
// post a notification
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openPayoutAlerts"), object: nil)
}else if(category == "blog"){ // if notification is blogs alert open blogs controller
print("OPENING BLOGS")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "OpenBlogs"), object: nil)
}else if(category == "chat"){ // if notification is mypost alert open My post controller
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openMypostContoller"), object: nil)
}else if(category == "notification"){ // if notification is ingeneral notification open notification controller
let notificationobj = NotificationObj(userid: userid, notificationType: type, title:title, body:body, link:image, image_url:category, read_status:"1")
notificationLists.append(notificationobj)
saveNewNotificationInBackground(userInfo: userInfo)
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "openNotifications"), object: nil)
}
}
我的身份验证功能如下:
func authenticationWithTouchID() {
let localAuthenticationContext = LAContext()
localAuthenticationContext.localizedFallbackTitle = "Enter Passcode"
var _: AppDelegate? = (UIApplication.shared.delegate as? AppDelegate)
let backgrView = UIView(frame: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(UIScreen.main.bounds.size.width), height: CGFloat(UIScreen.main.bounds.size.height)))
backgrView.backgroundColor = UIColor.black
//backgrView.alpha = 0.9
window?.addSubview(backgrView)
let blurEffect = UIBlurEffect(style: .light)
let blurVisualEffectView = UIVisualEffectView(effect: blurEffect)
blurVisualEffectView.frame = backgrView.bounds
backgrView.addSubview(blurVisualEffectView)
var authError: NSError?
let reasonString = "Please authenticate to use App"
if localAuthenticationContext.canEvaluatePolicy(.deviceOwnerAuthentication, error: &authError) {
localAuthenticationContext.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reasonString) { success, evaluateError in
if success {
DispatchQueue.main.async
{
blurVisualEffectView.removeFromSuperview()
print("Authentication success by the system")
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "Dashboard") as UIViewController
self.window = UIWindow(frame: UIScreen.main.bounds)
self.window?.rootViewController = initialViewControlleripad
self.window?.makeKeyAndVisible()
}
} else {
//TODO: User did not authenticate successfully, look at error and take appropriate action
guard let error = evaluateError else {
return
}
print(self.evaluateAuthenticationPolicyMessageForLA(errorCode: error._code))
//TODO: If you have choosen the 'Fallback authentication mechanism selected' (LAError.userFallback). Handle gracefully
}
}
} else {
guard let error = authError else {
return
}
//TODO: Show appropriate alert if biometry/TouchID/FaceID is lockout or not enrolled
print("APP LOCKED : \(self.evaluatePolicyFailErrorMessageForLA(errorCode: error.code))")
}
}
答案 0 :(得分:0)
以此替换成功博客中的代码,
func goToBlogVC() {
DispatchQueue.main.async {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
if let mainView = storyboard.instantiateViewController(withIdentifier: "blogVC") as? blogVC
{
self.window = UIWindow(frame: UIScreen.main.bounds)
let nav1 = UINavigationController()
nav1.isNavigationBarHidden = true //Show or hide nav bar
nav1.viewControllers = [mainView]
self.window!.switchRootViewController(nav1)
self.window?.makeKeyAndVisible()
}
}
请告诉我您被打到的地方,您的代码看起来很完美,但是流程太庞大了。一旦收到通知,然后成功触发触发authenticationWithTouchID,请转到相关控制器[如果失败,请输入其密码],以下是我的代码
导航流程
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
{
if isblog {
self.jumpToScreen()
completionHandler()
}
}
func jumpToScreen() {
if let vc = self.window?.visibleViewController {
if vc.isKind(of: blogVC.self) {
return
}
else if vc.isKind(of: DashboardVC.self) {
self.goToBlogVC()
}
else {
print("Other Screens")
}
}