iOS 共享扩展流程

Mar*_*sta 5 ios ios-extensions share-extension swift3

我在创建共享扩展(如 Pinterest 应用程序的共享扩展)时遇到问题。当用户没有登录到包含应用程序的份额扩展只呈现一个选项,以警示log incancel

在代码中的哪个位置决定在我的共享扩展中显示哪个视图控制器。我看到这就像我需要检查共享容器的授权状态,如果这个状态是not logged我需要呈现警报控制器。如果状态是logged我需要显示我的主视图控制器ShareViewController,它是SLComposeServiceViewController

我的问题与 UI 无关,而是将此检查代码放在哪里。我没有找到任何应​​用程序扩展启动的方法,因此我可以根据某些状态为扩展选择一些初始视图控制器。

在 Pinterest 扩展中,当用户从其包含的应用程序注销时,我看不到他们的主视图控制器。我只看到带有选项的警报。

第二个问题:如何以编程方式从共享扩展程序切换到包含应用程序。当用户需要进行身份验证时,这个 Pinterest 共享扩展是如何做到这一点的?

我正在开发最新的 iOS SDK 10.2

Mar*_*sta 5

由于我没有收到任何反馈,我想出了如何做到这一点。这是答案。

为了控制我使用生命周期中的loadView方法加载哪个视图UIViewController。当应用程序首先需要viewUIViewController. 所以这是延迟加载。当用户loadViewIfNeeded()UIViewControllerapi调用时也会触发此方法。在此方法的主体中,您需要非常小心不要读取view属性,因为这将loadView再次调用并且您将有递归循环。

我对此方法的实现如下。我需要判断用户是否已登录包含应用程序,并基于此选择要加载的视图。

override func loadView() {
    // check in shared Keychain if user is authenticated
    self.userAuthenticated = userService.isAuthenticated()

    if self.userAuthenticated {
        // load default view of the ShareViewController
        super.loadView()
    } else {
        // if user is not logged in show only alert view controller with transparent dummy view
        let view = UIView()
        self.view = view
    }
}
Run Code Online (Sandbox Code Playgroud)

如果用户未登录,我会在其中显示警报

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    let context = self.extensionContext!
    if !self.userAuthenticated {
        let alert = UIAlertController(title: "Error", message: "User not logged in", preferredStyle: .alert)
        let cancel = UIAlertAction(title: "Cancel", style: .cancel) { _ in
            context.completeRequest(returningItems: nil, completionHandler: nil)
        }
        let login = UIAlertAction(title: "Log In", style: .default, handler: { _ in
            let url = URL(string: "fashionapp://login")!
            // This is utility method written in Objective-C.
            // I don't know yet if it passes Apple Review process or not.
            // We will see ;)
            self.open(url, options: [:], completionHandler: nil)
            context.completeRequest(returningItems: nil, completionHandler: nil)
        })

        alert.addAction(cancel)
        alert.addAction(login)
        present(alert, animated: true, completion: nil)
    }
}
Run Code Online (Sandbox Code Playgroud)

这是从共享扩展打开包含应用程序的方法。我希望它会有用,Apple 会毫无问题地审查它。它被写入是Objective-C因为Swift其中没有NSInvocation类,因此您只能使用最多两个参数执行选择器。

#import <UIKit/UIKit.h>

@interface UIViewController (OpenURL)

- (void)openURL:(nonnull NSURL *)url
        options:(nonnull NSDictionary<NSString *, id> *)options
completionHandler:(void (^ __nullable)(BOOL success))completion;

@end
Run Code Online (Sandbox Code Playgroud)

和实施。

#import "UIViewController+OpenURL.h"

@implementation UIViewController (OpenURL)


- (void)openURL:(nonnull NSURL *)url
        options:(nonnull NSDictionary<NSString *, id> *)options
completionHandler:(void (^ __nullable)(BOOL success))completion {

    SEL selector = NSSelectorFromString(@"openURL:options:completionHandler:");

    UIResponder* responder = self;
    while ((responder = [responder nextResponder]) != nil) {
        if([responder respondsToSelector:selector] == true) {
            NSMethodSignature *methodSignature = [responder methodSignatureForSelector:selector];
            NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];

            [invocation setTarget: responder];
            [invocation setSelector: selector];
            [invocation setArgument: &url atIndex: 2];
            [invocation setArgument: &options atIndex:3];
            [invocation setArgument: &completion atIndex: 4];
            [invocation invoke];
            break;
        }
    }
}

@end
Run Code Online (Sandbox Code Playgroud)