我在创建共享扩展时遇到问题,例如 Pinterest 应用程序的共享扩展。当用户未登录到包含的应用程序时,共享扩展仅显示带有登录
和取消
选项的警报。
在代码的哪个位置决定在我的共享扩展中显示哪个 View Controller 。我看到这就像我需要检查共享容器的授权状态,如果这个状态是 not logged
我需要提供警报 Controller 。如果状态为 logged
,我需要显示我的主视图 Controller ShareViewController
,它是 SLComposeServiceViewController
我的问题与 UI 无关,而是将此检查代码放在哪里。我没有找到任何启动应用程序扩展的方法,因此我可以根据某些状态选择一些初始 View Controller 进行扩展。
在 Pinterest 扩展中,当用户从其包含的应用程序注销时,我看不到他们的主视图 Controller 。我只看到带有选项的警报。
第二个问题:如何以编程方式从共享扩展切换到包含应用。当用户需要进行身份验证时,此 Pinterest 共享扩展程序如何执行此操作?
我正在开发最新的 iOS SDK 10.2
最佳答案
因为我没有收到任何反馈,所以我弄清楚了如何做到这一点。这就是答案。
为了控制哪个 View 加载,我使用了 UIViewController
生命周期中的 loadView
方法。当应用程序首先需要从 UIViewController
获取 view
属性时会触发此方法。所以这是延迟加载。当用户从 UIViewController
api 调用 loadViewIfNeeded()
时,也会触发此方法。在此方法的主体中,您需要非常小心,不要读取 view
属性,因为这将再次调用 loadView
并且您将有递归循环。
我对此方法的实现如下。我需要判断用户是否已登录包含应用程序,并根据此选择要加载的 View 。
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
}
}
如果用户未登录,我会显示警报
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)
}
}
这是从共享扩展中打开包含应用程序的方法。我希望它有用,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
和实现。
#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
关于iOS 共享扩展流程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858487/