首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >1 1Password应用程序扩展不能正常工作

1 1Password应用程序扩展不能正常工作
EN

Stack Overflow用户
提问于 2015-01-11 17:02:41
回答 1查看 367关注 0票数 1

我正试图在我的UIWebView中实现1 1Password应用程序扩展。不过我运气不好。在下面您可以看到Controller of UIWebView。当我按下Google,然后点击1 1Password按钮,我可以选择我的谷歌帐户。但是,一旦我按下它,UIWebView就会重新加载初始页面,仅此而已。我做错了什么?

代码语言:javascript
复制
#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

我一直在犯这个错误:

代码语言:javascript
复制
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}>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-01-11 19:08:40

事实证明,问题是我在viewWillAppear中创建了视图。扩展是一个覆盖,因此--在扩展完成后--调用viewWillAppear

我解决这个问题的方法是把它移到viewDidLoad。瞧,这很管用。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27889690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档