2017-03-17 92 views
3

我在创建共享扩展程序(如Pinterest应用程序的共享扩展程序)时遇到问题。当用户未登录到包含应用程序时,共享分机只会显示警报,其选项为log incanceliOS共享扩展程序流程

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

我想问一个子类没有UI相关的,但在那里把这个校验码。我没有找到任何应​​用程序扩展启动的方法,所以我可以根据某些状态选择一些初始视图控制器进行扩展。

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

第二个问题:如何以编程方式从共享扩展切换到包含应用程序。当用户需要认证时,这个Pinterest共享扩展是如何实现的?

我工作的最新的iOS SDK 10.2

回答

1

由于我没有收到这方面的任何反馈,我弄清楚如何做到这一点。这是答案。

为了控制我使用loadView方法从UIViewController生命周期中查看哪个视图。当应用程序首先需要从UIViewController获得view属性时,会触发此方法。所以这是懒加载。当用户从UIViewController api调用loadViewIfNeeded()时,也会触发此方法。在这个方法的主体中,你需要非常小心,不要读取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 
    } 
} 

如果用户没有登录我

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 
+0

你能解释一下如何使用这段代码吗?我坚持这个共享的东西,并没有帮助在网上找到。 –