无法在iOS Obj-C上使用AWS用户池进行身份验证。注册工作

时间:2016-09-20 16:44:01

标签: ios objective-c amazon-web-services amazon-cognito

使用示例应用程序。在我的应用中使用示例应用的SignInViewController不起作用。我已将大部分示例应用复制到我的AppDelelgate

我已经修改了SignInViewController,因为我正在浏览它以登录。

当我使用用户getDetails时,我的委托代码(startPasswordAuthentication)可以正常工作。但是当我使用signInPressed时:没有任何反应。

这是我的AppDelegate:

#import "AppDelegate.h"
#import "AWSConstants.h"
#import "PeepVC.h"
#import "WelcomeViewController.h"
//#import <GoogleMaps/GoogleMaps.h>
@import GoogleMapsBase;
@import GooglePlaces;

@interface AppDelegate ()

@property (nonatomic,strong) AWSTaskCompletionSource<NSNumber *>* rememberDeviceCompletionSource;

@end

@implementation AppDelegate
{
    GMSPlacesClient *_placesClient;
    UIBackgroundTaskIdentifier bgTask;
}
@synthesize cognitoId,locationManager,awsUser,awsPW;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];


    // new pool info for users
    //setup service config
    AWSServiceConfiguration *serviceConfiguration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionUSEast1 credentialsProvider:nil];

    AWSCognitoIdentityUserPoolConfiguration *configuration = [[AWSCognitoIdentityUserPoolConfiguration alloc] initWithClientId:@"--omitted--"
                                                                                                                  clientSecret:@"--omitted--"
                                                                                                                        poolId:@"--omitted--"];


    [AWSCognitoIdentityUserPool registerCognitoIdentityUserPoolWithConfiguration:serviceConfiguration userPoolConfiguration:configuration forKey:@"UserPool"];
    AWSCognitoIdentityUserPool *pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
    pool.delegate = self;
    NSLog(@"Pool:%@",pool);

    NSLog(@"AWS configuration:%@",configuration);
    NSLog(@"AWS service configuration:%@",serviceConfiguration);
    NSLog(@"AWS Cognito Identity Pool:%@",pool.userPoolConfiguration);

    NSString *deviceInfo = [NSString stringWithFormat:@"Name:%@ Model:%@ (%@:%@)", [UIDevice currentDevice].name, [UIDevice currentDevice].model, [UIDevice currentDevice].systemName, [UIDevice currentDevice].systemVersion];

    NSLog(@"Device Info:%@",deviceInfo);

    [GMSPlacesClient provideAPIKey:@"--omitted--"];

    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupMainView) name:@"registerForNotifications" object:nil];
    }

    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];

    locationManager.delegate = self;

    self.venueDataModel = [[NSMutableDictionary alloc] init];
    self.venueDataModelIndices = [[NSMutableDictionary alloc] init];
    self.venueUserDict = [[NSMutableDictionary alloc] init];

    //Set signedUp to Yes or something else in order to skip the tutorial
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"accountStatus"]) {
        [self initializeTutorial];
    } else {
        [self setupMainView];
    }


    self.storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    return YES;
}

这是我的SignInViewController:

#import "SignInViewController.h"

@interface SignInViewController ()
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (nonatomic, strong) AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails*>* passwordAuthenticationCompletion;

@property (nonatomic,strong) AWSCognitoIdentityUserGetDetailsResponse * response;
@property (nonatomic, strong) AWSCognitoIdentityUser * user;
@property (nonatomic, strong) AWSCognitoIdentityUserPool * pool;
@end

@implementation SignInViewController

- (void) viewWillAppear:(BOOL)animated {
    self.password.text = nil;
    self.username.text = self.usernameText;
    [self.navigationController setNavigationBarHidden:YES];
    self.pool = [AWSCognitoIdentityUserPool CognitoIdentityUserPoolForKey:@"UserPool"];
    NSLog(@"Pool:%@",self.pool);
    if(!self.user)
        NSLog(@"//SignInViewController:!self.user");
        self.user = [self.pool currentUser];
    [self refresh];
}
-(void) refresh {
    NSLog(@"//SignInViewController:refresh");

    [[self.user getDetails] continueWithSuccessBlock:^id _Nullable(AWSTask<AWSCognitoIdentityUserGetDetailsResponse *> * _Nonnull task) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.response = task.result;
            self.title = self.user.username;
            NSLog(@"//UserDetailTableViewController:reloadData");
            //[self.tableView reloadData];
        });
        return nil;
    }];
}

- (IBAction)signInPressed:(id)sender {
    NSLog(@"SignInPressed:%@",self.username.text);
    self.passwordAuthenticationCompletion.result = [[AWSCognitoIdentityPasswordAuthenticationDetails alloc] initWithUsername:self.username.text password:self.password.text];
}


-(void) getPasswordAuthenticationDetails: (AWSCognitoIdentityPasswordAuthenticationInput *) authenticationInput  passwordAuthenticationCompletionSource: (AWSTaskCompletionSource<AWSCognitoIdentityPasswordAuthenticationDetails *> *) passwordAuthenticationCompletionSource {
    NSLog(@"getPasswordAuthenticationDetails:");

    self.passwordAuthenticationCompletion = passwordAuthenticationCompletionSource;
    NSLog(@"self.passwordAuthenticationCompletion:%@",passwordAuthenticationCompletionSource);

    dispatch_async(dispatch_get_main_queue(), ^{
        if(!self.usernameText)
            self.usernameText = authenticationInput.lastKnownUsername;
            NSLog(@"self.usernameText:%@",self.usernameText);
    });

}

-(void) didCompletePasswordAuthenticationStepWithError:(NSError*) error {
    NSLog(@"didCompletePasswordAuthenticationStepWithError:");

    dispatch_async(dispatch_get_main_queue(), ^{
        if(error){
            [[[UIAlertView alloc] initWithTitle:error.userInfo[@"__type"]
                                        message:error.userInfo[@"message"]
                                       delegate:nil
                              cancelButtonTitle:nil
                              otherButtonTitles:@"Retry", nil] show];
        }else{
            self.usernameText = nil;
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    });
}

@end

以下是日志中的输出:

2016-09-01 22:20:48.323 PS[84018:4547153] Pool:<AWSCognitoIdentityUserPool: 0x7fda09c2f0d0>
2016-09-01 22:20:48.324 PS[84018:4547153] AWS configuration:<AWSCognitoIdentityUserPoolConfiguration: 0x7fda09c3b2a0>
2016-09-01 22:20:48.324 PS[84018:4547153] AWS service configuration:<AWSServiceConfiguration: 0x7fda09c3b520>
2016-09-01 22:20:48.324 PS[84018:4547153] AWS Cognito Identity Pool:<AWSCognitoIdentityUserPoolConfiguration: 0x7fda09c3b2a0>
2016-09-01 22:20:48.324 PS[84018:4547153] Device Info:Name:iPhone Simulator Model:iPhone (iPhone OS:9.3)
2016-09-01 22:20:48.330 PS[84018:4547153] appDidBecomeActive
2016-09-01 22:20:50.315 PS[84018:4547153] Pool:<AWSCognitoIdentityUserPool: 0x7fda09c2f0d0>
2016-09-01 22:20:50.315 PS[84018:4547153] //SignInViewController:!self.user
2016-09-01 22:20:50.317 PS[84018:4547153] //SignInViewController:refresh
2016-09-01 22:20:50.317 PS[84018:4547153] startPasswordAuthentication
2016-09-01 22:20:50.318 PS[84018:4547153] //startPasswordAuthentication:self.signInViewController:<SignInViewController: 0x7fda09f1e920>
2016-09-01 22:20:50.319 PS[84018:4547153] getPasswordAuthenticationDetails:
2016-09-01 22:20:50.319 PS[84018:4547153] self.passwordAuthenticationCompletion:<AWSTaskCompletionSource: 0x7fda09c5b800>
2016-09-01 22:20:50.327 PS[84018:4547153] self.usernameText:(null)
2016-09-01 22:21:04.557 PS[84018:4547153] SignInPressed:testsamp

在演示中,一旦调用了“signInPressed”,您就会看到下面的日志条目,以及更多的服务器响应。

2016-09-01 14:27:31.848 CognitoYourUserPoolsSample[77236:4278261] AWSiOSSDK v2.4.7 [Debug] AWSURLSessionManager.m line:526 | -[AWSURLSessionManager printHTTPHeadersAndBodyForRequest:] | Request headers:
{
    "Content-Type" = "application/x-amz-json-1.1";
    Host = "cognito-idp.us-east-1.amazonaws.com";
    "User-Agent" = "aws-sdk-iOS/2.4.7 iPhone-OS/9.3 en_US";
    "X-Amz-Date" = 20160901T182731Z;
    "X-Amz-Target" = "AWSCognitoIdentityProviderService.InitiateAuth";
}

我必须遗漏一些细节,但我无法弄明白。提前感谢您的指导。

0 个答案:

没有答案