更改google plus,fb注册流量更改(af网络)

时间:2016-01-13 08:26:45

标签: ios objective-c facebook afnetworking-2

我已经问过这个问题,但没有更多解决方案。我挣扎了超过1天。我是初学者。请帮帮我。

我有两个屏幕。一个用于登录,其中有fb, google plus个登录。另一个屏幕是注册,它具有相同的fb, google plus注册。现在,当用户第一次使用fbgoogle plus按钮登录我的注册屏幕时。

当用户退出并再次转到sign up屏幕时,如果他们尝试登录,则UIAlert将显示他们已经注册。

请参阅以下代码以供参考。

@interface PCSignUpTVC () <FBSDKLoginButtonDelegate, UITextFieldDelegate, UIAlertViewDelegate, GIDSignInDelegate, GIDSignInUIDelegate>

@property (weak, nonatomic) IBOutlet FBSDKLoginButton *fbSignUpButton;
@property (weak, nonatomic) IBOutlet UIButton *gPlusSignUpButton;


@end

@implementation PCSignUpTVC

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [self forceLogout];
    GIDSignIn *gidSharedInstance = [GIDSignIn sharedInstance];

    gidSharedInstance.delegate = self;
    gidSharedInstance.uiDelegate = self;
}

- (void)forceLogout {
    [[GIDSignIn sharedInstance] signOut];
    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
    [loginManager logOut];
}

-(IBAction)didTapGooglePlusSignup:(id)sender{
    GIDSignIn *gidSharedInstance = [GIDSignIn sharedInstance];
    [gidSharedInstance signIn];
}





- (void)signUpWithParams:(NSDictionary *)params {
    [[PCUtilities sharedUtilities] showActivityControllerWithMessage:nil inController:self];

    [[PCWebServiceManager sharedWebServiceManager] signUpWithParameters:params completionHandler:^(id result, NSError *error) {
        [[PCUtilities sharedUtilities] hideActivityIndicator];

        if (error) {
            [PCUtilities showAlertWithTitle:@"Oops" message:error.localizedDescription cancelButtonTitle:@"ok"];
        }else {
            APP_DELEGATE.loginObject = (PCLoginObject*)result;
            [PCSharedClass sharedInstance].userID =[NSString stringWithFormat:@"%ld",(long)APP_DELEGATE.loginObject.userId];
            [self performSelector:@selector(showSignUpSuccess) withObject:nil afterDelay:0.5];
        }
    }];
}

// for google+, fb sign up method alone not for signin
- (void)signUp:(NSDictionary *)params {
    [[PCUtilities sharedUtilities] showActivityControllerWithMessage:nil inController:self];

    [[PCWebServiceManager sharedWebServiceManager] signUpWithParameters:params completionHandler:^(id result, NSError *error) {
        //[[PCUtilities sharedUtilities] hideActivityIndicator];

            if(APP_DELEGATE.loginObject == (PCLoginObject*)result)
            {
            [PCSharedClass sharedInstance].userID =[NSString stringWithFormat:@"%ld",(long)APP_DELEGATE.loginObject.userId];
            [self performSelector:@selector(showSignUpSuccess) withObject:nil afterDelay:0.5];
        }
    }];
}





- (void)showSignUpSuccess {

    //Redirect the user to About me screen.
    if(APP_DELEGATE.loginObject.firstName)
        [[NSUserDefaults standardUserDefaults] setObject:APP_DELEGATE.loginObject.firstName forKey:@"firstName"];

    if(APP_DELEGATE.loginObject.emialId)
        [[NSUserDefaults standardUserDefaults] setObject:APP_DELEGATE.loginObject.emialId forKey:@"emailId"];

    if(APP_DELEGATE.loginObject.phoneNumber)
        [[NSUserDefaults standardUserDefaults] setObject:APP_DELEGATE.loginObject.phoneNumber forKey:@"phoneNumber"];

    if (APP_DELEGATE.loginObject.facilityStatus == eUserIncompleteStatus ) {

        // Push About me screen
        PCAboutMeVC *aboutMeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PCAboutMeVC"];
        aboutMeVC.firstSignUp = YES;
        [self.navigationController pushViewController:aboutMeVC animated:YES];

    }else {
        /// Uncomment to push home view

        KYDrawerController *drawerController = [self.storyboard instantiateViewControllerWithIdentifier:@"KYDrawerController"];
        [self presentViewController:drawerController animated:YES completion:nil];
        APP_DELEGATE.drawerController = drawerController;

    }


}



#pragma mark FBSDKLoginButtonDelegate


- (void)loginButton:(FBSDKLoginButton *)loginButton didCompleteWithResult:(FBSDKLoginManagerLoginResult *)result
              error:(NSError *)error {
    if (error) {
        [PCUtilities showAlertWithTitle:@"Oops!" message:error.localizedDescription cancelButtonTitle:@"OK"];
    }else {
        NSString *fbId = result.token.userID;
        NSString *accessToken = result.token.tokenString;

        if (accessToken && fbId) {
            NSDictionary *param =  @{@"facebookId":fbId, @"loginType":@"Facebook", @"accessToken":accessToken};
            [self signInWithParameters:param];
        }

        if (accessToken && fbId) {

            NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:1];
            [params setObject:@"Facebook" forKey:@"loginType"];
            [params setObject:fbId forKey:@"facebookId"];
            [params setObject:accessToken forKey:@"accessToken"];

            FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, email, name, first_name, last_name, gender,birthday,location,picture"}];
            [request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                if (!error) {
                    NSString *firstName = [result objectForKey:@"first_name"];
                    if (firstName) {
                        [params setObject:firstName forKey:@"firstName"];
                    }

                    NSString *lastName = [result objectForKey:@"last_name"];
                    if (lastName) {
                        [params setObject:lastName forKey:@"lastName"];
                    }

                    NSString *gender = [result objectForKey:@"gender"];
                    if (gender) {
                        if ([gender isEqualToString:@"male"]) {
                            gender = @"M";
                        }else if ([gender isEqualToString:@"female"]) {
                            gender = @"F";
                        }else {
                            gender = @"O";
                        }
                        [params setObject:gender forKey:@"gender"];
                    }

                    //                    NSString *dob = [result objectForKey:@"birthday"];
                    NSString *email = [result objectForKey:@"email"];
                    NSString *picture = nil;
                    if([result objectForKey:@"picture"] && [[result objectForKey:@"picture"] objectForKey:@"data"])
                        picture = [[[result objectForKey:@"picture"] objectForKey:@"data"] objectForKey:@"url"];
                    //                    NSString *location = [result objectForKey:@"location"];

                    if(email){
                        [params setObject:email forKey:@"emailId"];
                    }

                    if(picture){
                        [params setObject:picture forKey:@"profilePicturePath"];
                    }
                    [self signUp:params];
                }
            }];
        }
    }
}

- (void)loginButtonDidLogOut:(FBSDKLoginButton *)loginButton {

}


#pragma mark GPPSignInDelegate


- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error {

    NSString *googlePlusId = user.userID;
    NSString *accessToken = user.authentication.accessToken;
    NSString *userId = user.userID;
    NSString *idToken = user.authentication.accessToken;
    NSString *firstName = nil;
    NSString *lastName = nil;

    NSArray *array = [user.profile.name componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if(array.count > 0)
        firstName = [array objectAtIndex:0];

    if(array.count > 1)
        lastName = [array objectAtIndex:1];

    NSString *emailId = user.profile.email;

    NSString *profilePicturePath = [user.profile imageURLWithDimension:100].absoluteString;

    if (accessToken && googlePlusId) {
        NSDictionary *param =  @{@"googleId":googlePlusId, @"loginType":@"Google", @"accessToken":accessToken};
        [self signInWithParameters:param];
    }

    if (userId && idToken) {
        NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:1];
        [params setObject:@"Google" forKey:@"loginType"];
        [params setObject:userId forKey:@"googleId"];
        [params setObject:idToken forKey:@"accessToken"];

        if (firstName) {
            [params setObject:firstName forKey:@"firstName"];
        }
        if (lastName) {
            [params setObject:lastName forKey:@"lastName"];
        }

        if (profilePicturePath) {
            [params setObject:profilePicturePath forKey:@"profilePicturePath"];
        }

        if(emailId){
            [params setObject:emailId forKey:@"emailId"];
        }

        [self signUp:[NSDictionary dictionaryWithDictionary:params]];
    }



}
- (void)signInWithParameters:(NSDictionary *)param {
    [[PCUtilities sharedUtilities] showActivityControllerWithMessage:nil inController:self];

    [[PCWebServiceManager sharedWebServiceManager] signInWithParameters:param completionHandler:^(id responseObject, NSError *error) {

        //        [[PCUtilities sharedUtilities] hideActivityIndicator];

        if (error) {
            [self forceLogout];

            [PCUtilities showAlertWithTitle:nil message:error.localizedDescription cancelButtonTitle:@"OK"];
            [[PCUtilities sharedUtilities] hideActivityIndicator];

        }else {
            PCLoginObject *loginObject = (PCLoginObject *)responseObject;
            [self dismissViewControllerAnimated:NO completion:^{
                [self performSelector:@selector(handleSignInResponse:) withObject:loginObject afterDelay:0.2];
            }];
        }
    }];
}


- (void)handleSignInResponse:(PCLoginObject *)loginObject {

    [[PCUtilities sharedUtilities] hideActivityIndicator];

    if (loginObject.statusCode == eSuccess) {


        [PCSharedClass sharedInstance].userID = [NSString stringWithFormat:@"%ld",(long)loginObject.userId];
        [PCSharedClass sharedInstance].userStatus = loginObject.facilityStatus;

        [[NSUserDefaults standardUserDefaults] setObject:loginObject.firstName forKey:@"firstName"];
        [[NSUserDefaults standardUserDefaults] setObject:loginObject.emialId forKey:@"emailId"];
        [[NSUserDefaults standardUserDefaults] setObject:loginObject.phoneNumber forKey:@"phoneNumber"];

        APP_DELEGATE.loginObject = loginObject;

        switch (loginObject.facilityStatus) {
            case eUserIncompleteStatus: {
                //Redirect the user to About me screen.
                PCAboutMeVC *aboutMeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PCAboutMeVC"];
                aboutMeVC.firstSignUp = YES;
                [self.navigationController pushViewController:aboutMeVC animated:YES];

                break;
            }

            case eUserInactiveStatus: {
                //Message, confirm and cancel button
                break;
            }

            case eUserActiveStatus:
            case eUserLoggedOffStatus: {
                //Go to home screen
                KYDrawerController *drawerController = [self.storyboard instantiateViewControllerWithIdentifier:@"KYDrawerController"];
                [self presentViewController:drawerController animated:YES completion:nil];
                APP_DELEGATE.drawerController = drawerController;

                break;
            }

            case eUserPendingStatus: {
                UIAlertView *dialog = [[UIAlertView alloc] initWithTitle: nil
                                                                 message: @"You can not access your account untill we will verify your facility details. Thank you."
                                                                delegate:self
                                                       cancelButtonTitle: @"Ok"
                                                       otherButtonTitles: nil, nil];
                [dialog show];

                break;
            }

            default:
                break;
        }

    }else {
        [PCUtilities showAlertWithTitle:nil message:loginObject.failurereason cancelButtonTitle:@"OK"];
    }

}



@end

需要!

但我需要的是,当新用户或注册用户在我的注册屏幕中按fb,google plus时,他们应该转到我的项目。

对于已经注册的新用户或旧用户,不应该说出警告&#34;在我的注册屏幕中。

当任何已注册或未注册的用户在注册屏幕中按下fb, google登录按钮时,他们应该已登录并应该进入我的应用程序。

我尝试过更改UIAlert或删除错误消息,但是当我输入时,我收到的错误为internal server error。此代码使用了一些AFnetworking

请帮帮我,我挣扎了3个多小时。如果我的解释不清楚,请发表评论,以便我重写。

每次用户通过sign in屏幕登录都没有问题。但是,当用户注册或新用户按下fb, google plus按钮进行登录时,他们应该可以在我的应用中输入...就像我的sign in屏幕

感谢。

网络服务代码:

- (void)signInWithParameters:(NSDictionary *)param completionHandler:(void(^)(id responseObject, NSError *error))completion {

    NSString *requestPath = [self requestURLStringForOperationType:eSignInOperation];
    self.requestSerializer = [AFJSONRequestSerializer serializer];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:requestPath
       parameters:param
          success:^(AFHTTPRequestOperation *operation, id responseObject) {
              if ([[responseObject objectForKey:@"status"] isEqualToString:@"SUCCESS"]) {
                  PCLoginObject *loginObject = [[PCLoginObject alloc] initWithData:responseObject];
                  completion(loginObject, nil);

                  //                  NSLog(@"loginObject=%@",loginObject.status);

              }else {
                  NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[responseObject objectForKey:@"failurereason"] forKey:NSLocalizedDescriptionKey];
                  NSError *error = [NSError errorWithDomain:@"Error!" code:200 userInfo:userInfo];
                  completion(nil, error);
              }

          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
              completion(nil, error);

          }];
}

- (void)signUpWithParameters:(NSDictionary *)params completionHandler:(void(^)(id result, NSError *error))completion {
    NSString *requestPath = [self requestURLStringForOperationType:eSignUpOpertion];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:requestPath
       parameters:params
          success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
              if ([[responseObject objectForKey:@"status"] isEqualToString:@"FAIL"]) {
                  NSDictionary *userInfo = [NSDictionary dictionaryWithObject:[responseObject objectForKey:@"failurereason"] forKey:NSLocalizedDescriptionKey];
                  NSError *serError = [NSError errorWithDomain:@"Error!" code:200 userInfo:userInfo];
                  completion(nil, serError);

              }else {
                  PCLoginObject *loginObject = [[PCLoginObject alloc] initWithData:responseObject];

                  completion(loginObject, nil);
              }
          } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
              completion(nil, error);

          }];
}

0 个答案:

没有答案