Handle popups / tabs in SFSafariViewController

I am embedding SoundCloud input in my application. The application opens https://soundcloud.com/connectin SFSafariViewControllerwith a help redirect_urithat uses my own URL scheme to get a response. It works great for direct login to SoundCloud, but doesn’t work when trying to use the "Sign in with Google" button.

In Safari, this button opens a new tab (pop-up window on the desktop) using the Google login page, which then passes back to the SoundCloud tab through postMessage. This input stream works fine if you use the iOS Safari application but it crashes in SFSafariViewController(clicking the button goes to a white page with the Google URL:) https://accounts.google.com/o/oauth2/auth?....

My current workaround is to advise users using Google to click the Safari icon on SFSafariViewControllerto terminate the input stream in the Safari application, but I am wondering if there is a way to handle this without leaving my application.

+4
source share
1 answer

Well, if I use UIWebView, you can get the final oauth response from Google after the user authenticates with Google. However, how do we turn it back to SoundCloud (for example, soundcloud.com/connect/via/google_plus/returning)

- (void) webViewDidFinishLoad:(UIWebView*)inWebView {

    NSString* str = [inWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('response-form-encoded').value"];
    if ( str.length > 0 ) {
        NSDictionary* params = parseURLParams( str );
        NSLog( @"%@", params );
    }
}

Conclusion:

{
    "access_token" = "ya2...<removed>...4g";
    authuser = 0;
    code = "4/sU_g7....<removed>...fnRELA";
    "expires_in" = 3600;
    "id_token" = "eyJhb...<removed>...DA";
    prompt = none;
    scope = "https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/plus.login+https://www.googleapis.com/auth/userinfo.profile+https://www.googleapis.com/auth/plus.moments.write+https://www.googleapis.com/auth/plus.me+https://www.googleapis.com/auth/plus.profile.agerange.read+https://www.googleapis.com/auth/plus.profile.language.read+https://www.googleapis.com/auth/plus.circles.members.read";
    "session_state" = "c8703a085b885bbe8c8d0e29277b0c2fdf9c6c87..65aa";
    state = "392921654%7C0.288515904";
    "token_type" = Bearer;
}

The URL that initiates the Google login step is as follows: I have decrypted the escape code encoding below for convenience:

    https://accounts.google.com/o/oauth2/auth?client_id=984739005367.apps.googleusercontent.com&response_type=code token id_token gsession&scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile&state=425511939|0.2912748339&access_type=offline&request_visible_actions=http://schemas.google.com/AddActivity http://schemas.google.com/ListenActivity http://schemas.google.com/CreateActivity&after_redirect=keep_open&cookie_policy=single_host_origin&prompt=none&include_granted_scopes=true&proxy=oauth2relay751149297&redirect_uri=postmessage&origin=https://soundcloud.com&gsiwebsdk=1&authuser=0&jsh=m;/_/scs/apps-static/_/js/k=oz.gapi.en.TA32fes-0yU.O/m=__features__/am=AQ/rt=j/d=1/rs=AGLTcCOuWXPCbMjoOmrZx_gBsAG8J20NLA
0

Source: https://habr.com/ru/post/1654609/


All Articles