为什么finishedWithAuth:方法没有调用?

时间:2015-10-30 09:30:54

标签: ios objective-c google-plus

enter image description here

我在我的示例应用上完成了google集成,而我正在尝试在我的项目中实现finishedWithAuth方法没有调用。

0.进入谷歌+登录界面。

1.i完成了创建Oauth客户端密钥生成。

2.添加了gppsignin按钮。

3.in appdelegate.m文件

   - (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
   if([FBSDKApplicationDelegate sharedInstance])
   {
        return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                              openURL:url
                                                    sourceApplication:sourceApplication
                                                           annotation:annotation];
   }
   else
   {
       NSLog(@"entering");
        return [GPPURLHandler handleURL:url sourceApplication:sourceApplication annotation:annotation];
   }


}

实施

  1. url也添加了.plist。
  2. 这是我的代码

          signIn.delegate = self;
        signIn = [GPPSignIn sharedInstance];
        signIn.shouldFetchGooglePlusUser = YES;
        signIn.shouldFetchGoogleUserEmail = YES;
        //signIn.shouldFetchGoogleUserEmail = YES;  // Uncomment to get the user's email
    
        // You previously set kClientId in the "Initialize the Google+ client" step
        signIn.clientID = kClientId;
        //    signIn.clientID=APIID;
        signIn.homeServerClientID = kClientId;
        // Uncomment one of these two statements for the scope you chose in the previous step
        //    signIn.scopes = @[ kGTLAuthScopePlusLogin ];  // "https://www.googleapis.com/auth/plus.login" scope
    //    signIn.scopes = @[ @"profile" ];            // "profile" scope
            signIn.scopes=[NSArray arrayWithObjects:kGTLAuthScopePlusLogin,kGTLAuthScopePlusMe, nil];
        // Optional: declare signIn.actions, see "app activities"
    
    
        [signIn trySilentAuthentication];
    }
    - (void)signOut {
        [[GPPSignIn sharedInstance] signOut];
    }
    - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                       error: (NSError *) error {
        NSLog(@"Received error %@ and auth object %@",error, auth);
        if(error)
        {
    
        }
        else
        {
            NSString *serverCode = [GPPSignIn sharedInstance].homeServerAuthorizationCode;
            NSLog(@"this is server code%@",serverCode);
            NSLog(@"user email%@", signIn.authentication.userEmail);
            NSLog(@"user id%@",signIn.authentication.userID);
            NSLog(@"auth token=%@",auth.accessToken);
            NSString *str =  [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",auth.accessToken];
            NSString* escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
            NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
            NSData *data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
            id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    
    
            NSLog(@"this is jsonvalues==%@",json);
            if ( auth.userEmail)
            {
                [[[GPPSignIn sharedInstance] plusService] executeQuery:[GTLQueryPlus queryForPeopleGetWithUserId:@"me"] completionHandler:^(GTLServiceTicket *ticket, GTLPlusPerson *person, NSError *error)
                 {
                     // NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",person.image.url]];
                     // NSLog(@"%@",url);
                     //  NSLog(@"Name:%@",person.displayName);
                     //                 NSLog(@"birthday:%@",person.birthday);
                     //
                     //                 NSLog(@"gender:%@",person.gender);
                     NSLog(@"peoplelist%@",person.displayName);
                     NSLog(@"peopleimae=%@",person);
                     //                 NSLog(@"people image%@",person.image);
                     //                 NSLog(@"people emailid%@",person.emails);
                     //                  imgurl = userData[@"picture"][@"data"][@"url"];
                     NSDictionary *dic=(NSDictionary *)person;
                     if(person.image)
                     {
                         NSLog(@"this is converted dictionary=%@",dic);
                     }
                 }];
    //            [self disconnect];
            }
        }
    }
    - (void)disconnect {
        [[GPPSignIn sharedInstance] disconnect];
    }
    - (void)didDisconnectWithError:(NSError *)error {
        if (error) {
            NSLog(@"Received error %@", error);
        } else {
            // The user is signed out and disconnected.
            // Clean up user data as specified by the Google+ terms.
        }
    }
    

    enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎正在使用FacebookGoogle+登录。因此,请将- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation更改为此:

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {


    NSString *stringURL = [ url absoluteString];
    if([stringURL containsString:@"fb"])
    {

    return [[FBSDKApplicationDelegate sharedInstance] application:application
                                                          openURL:url
                                                sourceApplication:sourceApplication
                                                       annotation:annotation];
    }
    else
    {
        return [GPPURLHandler handleURL:url
                      sourceApplication:sourceApplication
                             annotation:annotation];

    }
}

loginWithGoogle的代码也应如下所示。问题在于您的signIn.scopes

-(IBAction)loginWithGooglePressed:(id)sender
{
    ///
    signIn = [GPPSignIn sharedInstance];
    signIn.clientID = kClientId;

    //signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;  // Uncomment to get the user's email

    signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusUserinfoEmail, nil];

    // Optional: declare signIn.actions, see "app activities"
    signIn.delegate = self;

    [signIn trySilentAuthentication];

}

对我来说很好:

Works fine For Me