Facebook login fails in UIWebView - iphone

In my app I have to load a web page in UIWebView where user can login with Facebook. The problem is that when user clicks login, blank page appears.
Is it possible to get back to first page and be logged in?
Web page works perfectly in Safari and opens new window and after login it closes it and gets back to where it left.
EDIT: wrong URL was provided to UIWebView

I think the sample of facebook SDK for iOS does the log in way you want.
Please refer to the link:
facebook-iOS-SDK-sample: https://github.com/facebook/facebook-ios-sdk/tree/master/sample/Hackbook
Especially this part in https://github.com/facebook/facebook-ios-sdk/blob/master/sample/Hackbook/Hackbook/RootViewController.m
/**
* Show the authorization dialog.
*/
- (void)login {
HackbookAppDelegate *delegate = (HackbookAppDelegate *)[[UIApplication sharedApplication] delegate];
if (![[delegate facebook] isSessionValid]) {
[[delegate facebook] authorize:permissions];
} else {
[self showLoggedIn];
}
}
When you click on log in button, it will open a login dialog, and ask for permission. This is what it looks like: https://developers.facebook.com/docs/mobile/screenshots/ios/#iphone-native
I think the best way to implement the "facebook-related" app is to start with the Hacbook. It is really a nice and detailed sample.

If you want to reload a specific url after the facebook login popup is closed to prevent being stuck on a blank page you can use this code.
First include the UIWebViewDelegate and set your delegate somewhere in your code.
[yourwebView setDelegate:self];
Then use the shouldStartLoadWithRequest method to check request url for close_popup.php which is used by Facebook to close their popup.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *urlString = [NSString stringWithFormat:#"%#", request];
if ([urlString rangeOfString:#"close_popup.php"].location == NSNotFound) {
//The close_pop.php is not found. (Do nothing)
} else {
//Facebook closed the popup so we should load the page we now want the user to see.
NSURL*url=[NSURL URLWithString:#"http://www.example.ca/thepageyouwanttoreload.html"];
NSURLRequest*request=[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
return YES;
}

SSL issue? As per Joanne use the facebook SDK and you get loads of extra functionality for free, including logging in via the facebook native app if its installed.

Related

facebook login on iphone leaves blank page

I created a simple test html page that contains a facebook like box. For testing purpose I uploaed the test page to my server.
On the desktop (being log out of facebook) there is the following situation:
Click on like
A popup appears with a form for login
After login the popup is closed and the like count is raised
However, using it on my iphone in chrome browser and being logged out of facebook the following happens:
Click on the like button
Redirect to the facebook login page
After login I'm redirected to a blank page.
The screenshot below shows the problem:
Is there a possibility to avoid this?
EDIT: The same happens when I try this in an iOS app using a UIWebView.
Okay, I found a solution for doing this in my iOS app for an integrated UIWebView.
When someone is not logged in to facebook a popup opens and requests a login. After logging in the user is redirected back to the page from where he hit the like button. However in the UIWebView this redirect URL leaves the user with a blank page. The solution to the problem is to intercept the redirect URL and reload the page with the like button.
So, with UIWebView one can intercept the requests before the actual webpage is loaded and react on certain URLs. The code for this look like this:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webview.delegate = self;
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com/facebook_like_box.html"]]];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if( [[request.URL description] rangeOfString:#"lugins/close_popup.php"].location != NSNotFound) {
// We intercepted the redirect URL of facebook after logging in. Instead of loading a blank page recall the URL of the like box
// manually on the uiwebview.
[self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.example.com/facebook_like_box.html"]]];
//returning NO indicates the the page is not further loaded.
return NO;
}
//Any other page should be loaded
return YES;
}

Reverse back to app from safari [duplicate]

This question already has answers here:
Return to app from safari
(4 answers)
Closed 9 years ago.
In my application I am calling an url in safari on a button click,I am using the following code
In safari I am using pay pal payment.Is there any chance to reverse back to my app after donate button click.
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO;
}
return YES;
}
NO That is not possible in case of websites that you dont have control. Instead, you can open the link in your webview.
If you have developed website, you can redirect it to your app using custom URL. For details refer this tutorial
For your case i.e. Paypal, it is not possible
If you are trying to navigate from Safari to your app, you would need to have control of the web page that you send the user to. Create a button on the webpage that links to the custom URL scheme of your application. Then when the user clicks on the button, your app will open on the device.
Instead of using [[UIApplication sharedApplication] openURL:] method, you can open your website into website inside your viewcontroller, with back button.Thus you can go back into your app from safari..
Follow this snippet,
`NSURL *url = [NSURL URLWithString:<YOUR_URL>];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
webViewBrowser.delegate = self;
[webViewBrowser loadRequest:request];`
Enjoy Programming!!

Google + iPhone API sign in and share without leaving app

I recently integrated the Google + API in my App, it was a breeze, my only problem with it, is that everything requires you to leave the app and then come back (it uses URL schemes for this). This is not the behavior I would like, is there a way to directly call their services and do whatever I want with the responses just like in LinkedIn API?.
I really want to avoid going back and forth between safari and my app. Any suggestions/documentation is appreciated.
Thank you,
Oscar
UPDATE FROM GOOGLE
today, we released a new Google Sign In iOS SDK with full built-in
support for Sign In via WebView:
developers.google.com/identity/sign-in/ios The SDK supports dispatch
to any of a number of Google apps handling Sign In when present, with
WebView fallback after. In all cases, the Safari switch is avoided,
which we've seen to be the key element in avoiding app rejection.
We're looking forward to getting feedback from people using the new
SDK, and hope its use can replace the (ingenious and diligent)
workarounds people have implemented in the meantime.
THE METHOD BELLOW IS NO LONGER NEEDED
THIS METHOD HANDLES THE LOGIN INTERNAL WITH A CUSTOM UIWebView
THIS WORKS AND WAS APPROVED BY APPLE
My app got kicked from review cause of this
"The app opens a web page in mobile Safari for logging in to Google plus,
then returns the user to the app. The user should be able log in without opening
Safari first."
See this link https://code.google.com/p/google-plus-platform/issues/detail?id=900
I did solved it by following steps
1) create a subclass of UIApplication, which overrides openURL:
.h
#import <UIKit/UIKit.h>
#define ApplicationOpenGoogleAuthNotification #"ApplicationOpenGoogleAuthNotification"
#interface Application : UIApplication
#end
.m
#import "Application.h"
#implementation Application
- (BOOL)openURL:(NSURL*)url {
if ([[url absoluteString] hasPrefix:#"googlechrome-x-callback:"]) {
return NO;
} else if ([[url absoluteString] hasPrefix:#"https://accounts.google.com/o/oauth2/auth"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
return NO;
}
return [super openURL:url];
}
#end
this will basically prevent anything to be opened from Chrome on iOS
we catch the auth call and redirect it to our internal UIWebView
2) to info.plist, add the Principal class, and for it Application (or whatever you named the class)
Add plist key "NSPrincipalClass" and as the value the class of your main application (class which extends UIApplication, in this case Application (see code above))
3) catch the notification and open an internal webview
When your custom Application class sends ApplicationOpenGoogleAuthNotification, listen for it somewhere (in the AppDelegate maybe) and when you catch this notification, open a UIWebView (use the URL passed by the notification as the url for the webview) (in my case the LoginViewController listens for this notification and when received, it opens a view controller containing only a webview hooked up to delegate)
4) inside the webview
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] hasPrefix:#"com.XXX.XXX:/oauth2callback"]) {
[GPPURLHandler handleURL:url sourceApplication:#"com.google.chrome.ios"n annotation:nil];
// Looks like we did log in (onhand of the url), we are logged in, the Google APi handles the rest
[self.navigationController popViewControllerAnimated:YES];
return NO;
}
return YES;
}
or simmilar code, that handles the response
com.XXX.XXX:/oauth2callback from code above, replace with your company and app identifier, like "com.company.appname:/oauth2callback"
you might want to use #"com.apple.mobilesafari" as sourceApplication parameter
So, it depends what you want to do.
Sign-In: this will always call out to another application. If the Google+ application is installed it will call out to that, else it will fall back to Chrome and Safari.
Sharing/Interactive Posts: right now this always uses Chrome or Mobile Safari.
Retrieving friends, writing app activities, retrieving profile information: All this is done with the access token retrieved after sign in, so does not require leaving the application.
It is possible, though rather unsupported, to skip the SDK and pop up a UIWebView, construct the OAuth link dynamically and send the user to that (take a look at GTMOAuth2ViewControllerTouch in the open source libraries that ship with the SDK). Below is the a very rough example of the kind of thing you could do to plumb it back into the GPPSignIn instance.
However, you would be guaranteeing that the user has to enter their username and password (and maybe 2nd factor). With the Google+ app you're pretty much guaranteed to be already signed in, and with the Chrome/Safari route, there is a chance the user is already signed in (particularly if they're using other apps with Google+ Sign-In).
This also doesn't address sharing, so I would strongly recommend using the existing SDK as far as possible. Filing a feature request for the way you would prefer it to work would be a good thing to do as well: https://code.google.com/p/google-plus-platform/issues/list
#interface ViewController() {
GTMOAuth2ViewControllerTouch *controller;
}
#end;
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
GPPSignIn *signIn = [GPPSignIn sharedInstance];
signIn.clientID = #""; // YOUR CLIENT ID HERE.
signIn.delegate = self;
}
- (IBAction)didTapSignIn:(id)sender {
void (^handler)(id, id, id) =
^(GTMOAuth2ViewControllerTouch *viewController,
GTMOAuth2Authentication *auth,
NSError *error) {
[self dismissViewControllerAnimated:YES completion:^{
[controller release];
}];
if (error) {
NSLog(#"%#", error);
return;
} else {
BOOL signedIn = [[GPPSignIn sharedInstance] trySilentAuthentication];
if(!signedIn) {
NSLog(#"Sign In failed");
}
}
};
controller = [[GTMOAuth2ViewControllerTouch
controllerWithScope:kGTLAuthScopePlusLogin
clientID:[GPPSignIn sharedInstance].clientID
clientSecret:nil
keychainItemName:[GPPSignIn sharedInstance].keychainName
completionHandler:handler] retain];
[self presentViewController:controller animated:YES completion:nil];
}
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error {
if (!error) {
UIAlertView * al = [[UIAlertView alloc] initWithTitle:#"Authorised"
message:#"Authorised!"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[al show];
[al release];
}
}
The only real trick to this code is that it uses the [GPPSignIn sharedInstance].keychainName - this means that the auth tokens get stored in the same keychain entry as the GPPSignIn button would, which in turn means we can use [[GPPSignIn sharedInstance] trySilentAuthentication] once it has been populated, and keep the same callback based flow as the main library.
#PeterLapisu approach works good if the Google Plus App is not installed.
Then outgoing url prefixes from app are as follows:
#"googlechrome-x-callback:"
#"https://accounts.google.com/o/oauth2/auth"
However if the Google App is installed there is one more outgoing url and the prefix list looks as follows:
#"com.google.gppconsent.2.4.1:"
#"googlechrome-x-callback:"
#"https://accounts.google.com/o/oauth2/auth"
So, if the Google App is installed, it will be launched simultaneously with our app UIViewController that contains webview.Then if user sucessfully logs in with Google App he will be directed back to our app and the ViewController will be visible.
To prevent this Google App should be allowed to login user and direct him back to our app. According to this discussion: https://code.google.com/p/google-plus-platform/issues/detail?id=900 it is allowed by Apple.
So in my implementation firstly I am checking if the Google App is installed:
- (BOOL)openURL:(NSURL*)url {
NSURL *googlePlusURL = [[NSURL alloc] initWithString:#"gplus://plus.google.com/"];
BOOL hasGPPlusAppInstalled = [[UIApplication sharedApplication] canOpenURL:googlePlusURL];
if(!hasGPPlusAppInstalled)
{
if ([[url absoluteString] hasPrefix:#"googlechrome-x-callback:"]) {
return NO;
} else if ([[url absoluteString] hasPrefix:#"https://accounts.google.com/o/oauth2/auth"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:ApplicationOpenGoogleAuthNotification object:url];
return NO;
}
}
return [super openURL:url];
}
EDIT:
Now I can confirm that my app was finally approved with this solution.
I hope it will help somebody.
It merges Google+ and Gmail samples and completely avoids using Google SignIn Button, i.e you do not leave the app.
Add both Google+ and Gmail API to you Google project, in your app login to google as you would to gmail using GTMOAuth2ViewControllerTouch.xib from OAuth2 and set scope to Google+:
-(IBAction)dologin{
NSString *scope = kGTLAuthScopePlusLogin;//Google+ scope
GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:kKeychainItemName
clientID:kClientID
clientSecret:kClientSecret];
if ([auth refreshToken] == nil) {
GTMOAuth2ViewControllerTouch *authController;
authController = [[GTMOAuth2ViewControllerTouch alloc]
initWithScope:scope
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
delegate:self
finishedSelector:#selector(viewController:finishedWithAuth:error:)];
[[self navigationController] pushViewController:authController animated:YES];
}else{
[auth beginTokenFetchWithDelegate:self didFinishSelector:#selector(auth:finishedRefreshWithFetcher:error:)];
}
}
and RETAIN the authentication object if signed in successfully, then use that auth object when using google plus services:
GTLServicePlus* plusService = [[[GTLServicePlus alloc] init] autorelease];
[plusService setAuthorizer:self.auth];//!!!here use our authentication object!!!
No need for GPPSignIn.
Full write up is here: Here is Another Solution
Use the (new) Google Sign In iOS SDK.
The SDK natively supports Sign In through WebView when no Google app is present to complete the Sign In process. It also supports potential dispatch to several Google apps for this purpose.

iPhone: After UIWebView opens external browser, then returning to app opens it a second time

I have a UIWebView which loads an HTML page which has some links to the App Store (<a herf="http://itunes/bla/bla/bla"> type of links).
Clicking a link, opens external Safari, then opens external App Store app and successfully goes to correct page.
If you then click Home button and return to springboard and click on app again, it will briefly open to the the UIWebView that was displayed and then jump to external Safari and external App Store app again.
If you go back to app one more time it stays in the app.
So what is happening is that returning to app after opening external browser link from UIWebView HTML page, will then jump back to the same link a second time.
Anybody have any ideas what might be causing this?
Am using Xcode 4.2.1 and it happens in simulator as well as on an actual device (iPad 1 with iOS 4.3).
EDIT - SOLUTION:
Ok, here is what I had to do to solve the problem (Thanks to Ben's response below who got me looking at the right areas):
This was my original method:
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didLoad) { //Do not jump to URL when view first loads
return YES;
}
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
And this is my fixed method that does not keep jumping to URL:
- (BOOL)webView:(UIWebView *)_webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (didShowURL) { //Do not jump to URL when returning to app
didShowURL = 0;
return NO;
}
if (didLoad) { //Do not jump to URL when view first loads
return YES;
}
didShowURL = 1;
[[UIApplication sharedApplication] openURL:[request URL]];
return YES;
}
I would suggest using the itms-apps:// protocol for your link. This will send the user directly to the application in the App Store without the need for a redirect.
NSURL *appStoreLink = [NSURL URLWithString:#"itms-apps://itunes.apple.com/us/app/instagram/id389801252?mt=8"];
[[UIApplication sharedApplication] openURL:appStoreLink];
Your webview is being reloaded to its last page (the app store page) and is automatically redirecting the user away again. You just need to make sure that when the app is reloaded it is not trying to load the app store link again.
Depending on your app setup you could use
viewWillAppear
And reload the original HTML page with the links every time the view is brought to the front.

Call 'action' on HTML web form in a UIWebView

I'm trying to skip the login page of a website, so I can give the user a more aesthetically appealing login page, and save their login credentials for automatic login in the future. So far I have two UITextFields, and a UIButton. The user enters their username and password, and upon pressing the UIButton their details are entered into the actual webform on the sites login page, and the subsequent 'logged-in' page is shown in a UIWebView. The webView will be hidden while the app logs the user in, and then presented when the 'logged-in' page has loaded.
I've managed to populate the webform, but simply can't actually submit it! I'm sure there's a very simple solution!
This is the code I have so far, and if there's a better way I should be doing this let me know!
-(IBAction)login:(id)sender
{
// Create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:#"var username = document.getElementsByName('username')[0]; username.value='%#';", username.text];
NSString *loadPasswordJS = [NSString stringWithFormat:#"var password = document.getElementsByName('password')[0]; password.value='%#';", password.text];
// Autofill the form - this works, and I can see the values being put into the webform
[self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
// This is the method I have tried to submit the form, but nothing happens (ct100 is the ID of the form).
[self.webView stringByEvaluatingJavaScriptFromString:#"document.ct100.submit();"];
}
Below Approach might be helpful to you
Put this code inside the didFinishLoading Method Of UIWebView Protocol
Fill The USerId
[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('username').value = 'userId'"];
Fill The Password
[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('password').value = 'password'"];
Call Submit Button as below
[webView
stringByEvaluatingJavaScriptFromString:#"document.forms['login_form'].submit();"];
Herelogin_form is the Name of You Form
Note: You have to manage one thing once you have filled the Credential clicked the Submit button. you need to make some BOOL or something else for avoiding the loading repetition of UIWebView.
Try:
[webView stringByEvaluatingJavaScriptFromString:#"document.getElementById(\"ct100\").submit()"];
Your view controller needs to implement the UIWebViewDelegate protocol and set itself as the UIWebView's delegate. Returning YES from webView:shouldStartLoadWithRequest (see code below) will allow the web view to navigate to any url the your form is submitting to.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(#"Allow navigation to request = %#", request);
return YES;
}