且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在iOS共享扩展程序中正式处理未经身份验证的用户?

更新时间:2023-12-01 15:39:16

我找不到任何官方指南,但是以下解决方案确实有效,并且在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://***.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).