簡體   English   中英

1密碼應用程序擴展無法正常工作

[英]1Password app Extension not working correctly

我正在嘗試在UIWebView實現1Password App Extension。 我雖然沒有運氣。 在下面,您可以看到UIWebViewController 當我按下Google並單擊1Password按鈕時,我可以選擇我的Google帳戶。 但是,一旦按下它, UIWebView只會重新加載初始頁面,僅此而已。 我究竟做錯了什么?

#import "LoginWebViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+AFNetworking.h"
#import "OnePasswordExtension.h"
#import "UIColor+CustomColors.h"

@interface LoginWebViewController ()

@property (nonatomic, strong) NSString *photoURLString;

@property (weak, nonatomic) IBOutlet UIButton *onepasswordFillButton;

@property (strong, nonatomic) WKWebView *webView;

@end

@implementation LoginWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self.onepasswordFillButton setHidden:![[OnePasswordExtension sharedExtension] isAppExtensionAvailable]];
}

- (IBAction)fillUsing1Password:(id)sender {
    [[OnePasswordExtension sharedExtension] fillLoginIntoWebView:self.webView forViewController:self sender:sender completion:^(BOOL success, NSError *error) {
        if (!success) {
            NSLog(@"Failed to fill login in webview: <%@>", error);
       }
    }];
}

- (void)viewWillAppear:(BOOL)animated  {
    UIToolbar *toolbar = [[UIToolbar alloc] init];
    toolbar.frame = CGRectMake(0, 20, self.view.frame.size.width, 44);
    toolbar.barTintColor = [UIColor customBlueColor];

    UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                        target:nil
                                                                        action:nil];

    #define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

    // choose whatever width you need instead of 600
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-40, 0, 80, 25)];
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    label.text = @"Log in";
    label.font = [UIFont boldSystemFontOfSize:20.0];
    UIBarButtonItem *toolBarTitle = [[UIBarButtonItem alloc] initWithCustomView:label];

    UIBarButtonItem *onepasswordButton = [[UIBarButtonItem alloc] initWithTitle:@"1P"
                                                               style:UIBarButtonItemStyleDone target:self
                                                              action:@selector(fillUsing1Password:)];

    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Close"
                                                               style:UIBarButtonItemStyleDone target:self
                                                              action:@selector(backButtonPressed:)];

    NSArray *items = [[NSArray alloc] initWithObjects:doneButton, spacer, toolBarTitle, spacer, onepasswordButton, nil];

    [toolbar setItems:items];

    self.webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 24, self.view.frame.size.width, self.view.frame.size.height-24)];

    NSString *url=@"https://sandbox.feedly.com/v3/auth/auth?client_id=sandbox&redirect_uri=http://localhost&response_type=code&scope=https%3A%2F%2Fcloud.feedly.com%2Fsubscriptions";
    NSURL *nsurl=[NSURL URLWithString:url];
    NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
    [self.webView loadRequest:nsrequest];
    [self.view addSubview:self.webView];

    [self.view addSubview:toolbar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *URLString = [[request URL] absoluteString];

    NSString *urlStart = [URLString substringToIndex:23];

    if ([urlStart isEqualToString:@"http://localhost/?code="])
    {
        NSLog(@"Successful login.");
    }
}

- (IBAction) backButtonPressed:(id) sender {
    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}

@end

我不斷收到此錯誤:

Failed to fill login in webview: <Error Domain=OnePasswordExtension Code=0 "1Password Extension was cancelled by the user" UserInfo=0x17427be00 {NSLocalizedDescription=1Password Extension was cancelled by the user}>

事實證明,問題在於我在viewWillAppear創建了視圖。 該擴展是一個覆蓋,因此-在擴展完成后-將調用viewWillAppear

為了解決這個問題,我將其移至viewDidLoad 瞧,行得通。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM