菜鸟教程小白 发表于 2022-12-11 19:49:39

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


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

<p>从另一端接近这个,我知道 <a href="https://developer.apple.com/documentation/foundation/userdefaults/1409957-init" rel="noreferrer noopener nofollow"><code>UserDefaults(suiteName:)</code></a>和 <a href="https://developer.apple.com/documentation/security/keychain_services?language=swift" rel="noreferrer noopener nofollow"><code>Keychain services</code></a>可以从包含的应用程序中用于与扩展程序共享有关经过身份验证的用户的信息,但是如果用户安装了应用程序并直接尝试使用其扩展程序共享内容而无需登录(或注册)怎么办?</p >

<ol>
<li><p><strong>要求用户登录包含的应用程序?</strong>(在自定义 View 中?<a href="https://developer.apple.com/ios/human-interface-guidelines/extensions/sharing-and-actions/" rel="noreferrer noopener nofollow">Extensions are modal by default.</a>)</p></li>
<li><p><strong>在扩展中重新实现身份验证?</strong>(或者通过自定义框架共享?这可能吗?)</p></li>
<li><p><strong>切换到包含应用程序然后返回?</strong> 这似乎不支持,除了 Today 扩展,但 <a href="https://developer.apple.com/library/content/documentation/General/Conceptual/ExtensibilityPG/Today.html#//apple_ref/doc/uid/TP40014214-CH11-SW13" rel="noreferrer noopener nofollow">docs</a> 中描述的机制已用于解决方法(SO 线程:<a href="https://stackoverflow.com/questions/26188931/ios-8-share-extension-and-authentication-flows" rel="noreferrer noopener nofollow">1</a>、<a href="https://stackoverflow.com/questions/25254700/communicating-with-opening-containing-app-from-share-extension" rel="noreferrer noopener nofollow">2</a>、<a href="https://stackoverflow.com/questions/27506413/share-extension-to-open-containing-app" rel="noreferrer noopener nofollow">3</a>)。</p></li>
</ol>

<hr/>

<p>第 2 项的(丑陋的)示例实现 <a href="https://stackoverflow.com/questions/46442299/user-not-signed-into-firebase-in-share-app-extension/49178091#49178091" rel="noreferrer noopener nofollow">in this answer</a>使用 Firebase。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我找不到任何官方指南,但下面的解决方案确实有效,并且也被 App Store 接受。可能底线正是:(1)它不应该崩溃,(2)应该能够通过审查过程。</p>

<p>[FirebaseUI 身份验证[( <a href="https://github.com/firebase/FirebaseUI-iOS" rel="noreferrer noopener nofollow">https://github.com/firebase/FirebaseUI-iOS</a> ) 的解决方案:</p>

<p> <img src="https://raw.githubusercontent.com/society-for-the-blind/Access-News-Reader-iOS/master/presentations/accessnewsuploader-with-signin.gif" alt="animation"/> </p>

<p>相关代码部分:</p>

<pre><code>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: &#34;your-app-group&#34;)!

    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: &#34;userLoggedIn&#34;) == 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: &#34;userLoggedIn&#34;)
      }
    }
}
</code></pre>

<p>成功的登录也会通过共享容器被记住(即,打开包含的应用程序不会要求登录)。</p>

<p>github项目中的相关commit:
<a href="https://github.com/society-for-the-blind/Access-News-Reader-iOS/commit/e752b1c554f79ef027818db35c11fceb1ae817e0" rel="noreferrer noopener nofollow">https://github.com/society-for-the-blind/Access-News-Reader-iOS/commit/e752b1c554f79ef027818db35c11fceb1ae817e0</a> </p>

<hr/>

<p>问题</p>

<p>我第一次运行它,表单出现了,但不接受任何输入。 <code>Product > Clean</code> 和 <code>Product > Clean Build Folder ...</code> 是否,重新启动了 Xcode 和 Simulator,它工作正常。它也适用于旧 iPad (iOS 10.3.3)。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 共享扩展中正式处理未经身份验证的用户?,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/49134868/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/49134868/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 共享扩展中正式处理未经身份验证的用户?