How to prevent the extra view displaying an access

2020-02-08 09:45发布

I followed http://googlemac.blogspot.com/2011/05/ios-and-mac-sign-in-controllers.html to allow users to use Google to login to an iPhone app. After I tap "Allow access" button I get an extra screen that says, "Please copy this code, switch to your application and paste it there: (code in a textbox)."

This is what I have:

- (IBAction)googleLoginTapped:(UIButton *)sender
{
    [self loginToGoogle];
}

- (void)loginToGoogle 
{

    // For Google APIs, the scope strings are available
    // in the service constant header files.
    NSString *scope =@"https://www.googleapis.com/auth/userinfo.profile";

    // Typically, applications will hardcode the client ID and client secret
    // strings into the source code; they should not be user-editable or visible.

    // But for this sample code, they are editable.
    NSString *clientID = @"my clientID";
    NSString *clientSecret = @"my clientSecret";


    // Display the autentication view.
    SEL finishedSel = @selector(viewController:finishedWithAuth:error:);

    GTMOAuth2ViewControllerTouch *viewController;

    viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
                                                              clientID:clientID
                                                          clientSecret:clientSecret
                                                      keychainItemName:nil
                                                              delegate:self
                                                      finishedSelector:finishedSel];

    // For this sample, we'll force English as the display language.
    NSDictionary *params = [NSDictionary dictionaryWithObject:@"en"
                                                       forKey:@"hl"];

    viewController.signIn.additionalAuthorizationParameters = params;

    // Optional: display some html briefly before the sign-in page loads
    NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>";
    viewController.initialHTMLString = html;

    viewController.signIn.shouldFetchGoogleUserProfile = YES;

    [self presentModalViewController:viewController animated:YES];
}

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error
{
    if (error != nil) 
    {
        // Authentication failed (perhaps the user denied 

please see this link it is to good https://developers.google.com/accounts/docs/OAuth2InstalledApp

标签: ios oauth-2.0
8条回答
何必那么认真
2楼-- · 2020-02-08 09:52

It turns out this is pretty straightforward. In the login callback, simply dismiss and remove viewController from the parent view controller.

- (void)viewController:(UIViewController *)viewController
      finishedWithAuth:(GTMOAuth2Authentication *)auth
                 error:(NSError *)error
{
    if (error == nil) {
        // Get rid of the login view.
        // self.parentViewController was saved somewhere else and is the parent
        // view controller of the view controller that shows the google login view.
        [self.parentViewController dismissViewControllerAnimated:NO completion:nil];
        [viewController removeFromParentViewController];

        // Tell the delegate that the user successfully logged in ...
    } else {
        // Error handling code ...
    }
}
查看更多
冷血范
3楼-- · 2020-02-08 09:56

When using gtm-oauth2 to sign in to Google services, be sure the Google API Console project registration shows in the API Access section that the Client ID is issued for an installed application. This is described in the gtm-oauth2 documentation.

查看更多
放荡不羁爱自由
4楼-- · 2020-02-08 10:05

I tried this approach, and work fine, put this in your webViewDidFinishLoad method :

if ([webView.request.URL.absoluteString rangeOfString:@"https://accounts.google.com/o/oauth2/approval?"].location != NSNotFound) {
    webView.hidden=YES;
}
查看更多
Juvenile、少年°
5楼-- · 2020-02-08 10:07

After at least 20 hours of configuring things, I finally got this running. I also previously imported my swift file into my GTMOAuth2ViewControllerTouch.m file. Not sure if that impacted it but I added:

    #import "myBundleId-Swift.h"

Then, in the viewController.swift file I needed to add the final 2 lines:

 // Handle completion of the authorization process, and updates the Drive service
// with the new credentials.
func viewController(viewController: GTMOAuth2ViewControllerTouch , finishedWithAuth authResult: GTMOAuth2Authentication, error:NSError? ) {
    if let error = error
    {
        self.showAlert("Authentication Error", message:error.localizedDescription)
        self.driveService.authorizer = nil
    } else {
        print("Authentication success")
        self.driveService.authorizer = authResult
//This where we need to get rid of the copy the code screen:
        self.parentViewController?.dismissViewControllerAnimated(false, completion:nil)
        viewController.removeFromParentViewController()
    }
}

That got rid of the copy this code screen.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-02-08 10:10

I fixed this by nesting the view controller inside of a UINavigationController. No idea why did that did the trick, but it did.

So instead of

[self presentModalViewController:viewController animated:YES];

... use

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];
[self presentModalViewController:navigationController animated:YES];
查看更多
啃猪蹄的小仙女
7楼-- · 2020-02-08 10:15

while creating Client ID , Choose web Application instead of installed application , this will solve your problem .

happy coding :)

查看更多
登录 后发表回答