如何在iOS共享扩展程序中正式处理未经身份验证的用户? [英] How to officially handle unauthenticated users in an iOS share extension?

查看:94
本文介绍了如何在iOS共享扩展程序中正式处理未经身份验证的用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,Apple规定的惯用方法是什么? 对于任何建议,请说明应如何做和/或提供指向官方指南的链接.这应该是一个足够常见的情况,但我只能找到解决方法.

That is, what is an idiomatic way to do this that is prescribed by Apple? For any suggestion, please explain HOW that should be done and/or provide a link to official guides. This should be a common enough scenario, but I was only able to find workarounds.

从另一端解决这个问题,我知道 UserDefaults(suiteName:) Keychain services 可以在包含应用程序中用于与扩展程序共享有关已通过身份验证的用户的信息,但是如果用户安装了该应用程序,而直接跳入尝试使用其扩展程序共享内容而又未登录(或未登录)怎么办?

Approaching this from the other end, I know that UserDefaults(suiteName:) and Keychain services can be used from the containing app to share information about the authenticated user with the extension, but what if the user installs the app and just jumps right into trying to share content using its extension without ever signing in (or up)?

  1. 要求用户登录包含的应用程序吗?(在自定义视图中?

  1. Ask user to sign in in the containing app? (In a custom view? Extensions are modal by default.)

扩展中的重新实现身份验证?(或通过自定义框架共享吗?这可能吗?)

Re-implement authentication in extension? (Or shared via custom framework? Is this possible?)

要切换到包含应用程序然后再返回吗?除了今日"扩展程序,似乎不支持此功能,但是 1 3 ).

Switch to containing app and then back? This doesn't seem to be supported except in Today extension, but the mechanism described in the docs have been used for workarounds (SO threads: 1, 2, 3).


第2项的(丑陋)示例实现


An (ugly) sample implementation of item 2 in this answer using Firebase.

推荐答案

我找不到任何官方指南,但是以下解决方案确实有效,并且在App Store中也被接受.底线可能恰好是:(1)不应该崩溃,(2)应该能够通过审核过程.

I couldn't find any official guidelines, but the solution below did work and also got accepted in App Store. Probably the bottom line is exactly that: (1) it shouldn't crash and (2) should be able to go through the review process.

具有[FirebaseUI身份验证[( https://github.com/firebase/FirebaseUI-iOS的解决方案):

The solution with [FirebaseUI authentication[(https://github.com/firebase/FirebaseUI-iOS):

相关代码部分:

import UIKit
import Social
import Firebase
import FirebaseAuthUI

class ShareViewController: SLComposeServiceViewController {

    var authUI: FUIAuth?

    /* Using shared container to communicate between extension
       and containing app. Keychain would probably work too.
    */
    let defaults = UserDefaults.init(suiteName: "your-app-group")!

    override func presentationAnimationDidFinish() {

        /* https://stackoverflow.com/questions/37910766/
        */
        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }

        self.authUI = FUIAuth.defaultAuthUI()
        self.authUI?.delegate = self

        if self.defaults.bool(forKey: "userLoggedIn") == false {
            let fuiSignin     = 
                FUIPasswordSignInViewController(
                    authUI: FUIAuth.defaultAuthUI()!,
                    email: nil)
            let navController = 
                UINavigationController(rootViewController: fuiSignin)

            self.present(navController, animated: true)
        }
    }

/* FirebaseAuthUI delegate to handle sign-in
*/
extension ShareViewController: FUIAuthDelegate {
    func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?) {
        if error != nil {
            fatalError()
        }
        if user != nil {
            self.defaults.set(true, forKey: "userLoggedIn")
        }
    }
}

成功登录也会通过共享容器记住(即,打开包含应用程序不会要求登录).

Successful sign in also gets remembered via the shared container (i.e., opening the containing app won't ask for login).

github项目中的相关提交: https://github.com/society-对于盲人/Access-News-Reader-iOS/commit/e752b1c554f79ef027818db35c11fceb1ae817e0

The relevant commit in the github project: https://github.com/society-for-the-blind/Access-News-Reader-iOS/commit/e752b1c554f79ef027818db35c11fceb1ae817e0

问题

我第一次运行它,表格出现了,但是不接受任何输入.做了Product > CleanProduct > Clean Build Folder ...,重新启动了Xcode和Simulator,它开始工作了.它还可以在旧的iPad(iOS 10.3.3)上运行.

The first I ran it, the forms appeared, but wouldn't accept any input. Did Product > Clean and Product > Clean Build Folder ..., restarted Xcode and the Simulator, and it worked. It also worked on an old iPad (iOS 10.3.3).

这篇关于如何在iOS共享扩展程序中正式处理未经身份验证的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆