Facebook page in UIWebView - iphone

I have a UIWebView in my application where I 'm loading the facebook profile of my application. I am using facebook's single sign on SDK(Graph API). And I logged in into facebook in the Safari in background using my application. But When I try loadmy game profile in my application web view still I could see "Log In" and "Sign Up" buttons.
I tried setting cookies, main document URL and relative URL too but no use.
I am adding here my code too here,
NSURL *url = [NSURL URLWithString:#"http://www.facebook.com/myapp" relativeToURL:[NSURL URLWithString:#"http://login.facebook.com"]];
NSMutableURLRequest *request = nil;
if(url)
request = [NSMutableURLRequest requestWithURL:url];
NSArray * availableCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:#"http://login.facebook.com"]];
NSDictionary * headers = [NSHTTPCookie requestHeaderFieldsWithCookies:availableCookies];
if(request)
{
[request setHTTPShouldHandleCookies:YES];
[request setMainDocumentURL:[NSURL URLWithString:#"http://login.facebook.com"]];
[request setAllHTTPHeaderFields:headers];//login.facebook.com
[mWebView loadRequest:request];
}

You may be running into the situation I describe in the answer on this page:
Facebook iOS SDK not storing cookies for access

Related

open browser from xcode program with post parameter prefilled

I am developing an application that integrates a payment gateway. This works by opening the page "https://sis.sermepa.es/sis/realizarPago" passing some parameters by POST, from there the customer can make the payment.
My problem is that I can not find how to open the browser giving a URL and parameters. I've seen that open the browser using Intents but does not allow parameters and I have seen that you can send POST parameters to a URL and wait for the response, but not open the URL itself.
Does anyone have the solution to my problem?
Thank you very much.
I've solved the problem, here you have the solution in case anyone else needs the answer.
NSString *code=codeTF.text;
NSString *price=priceTF.text;
NSString *email=emailTF.text;
NSString *body = [NSString stringWithFormat:Code=%#&Price=%#&E-mail=%#", code, price, email];
NSURL *url = [NSURL URLWithString:#"https://***/index.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
//initialize the webViem in the *.h
// #property (nonatomic, retain) UIWebView *webView;
[webView loadRequest:request];

Can I use username and password information from UITextFields to log onto a website in a UIWebView automatically?

I am currently trying to find a solution to this task. Essentially I have an initial UIView that asks for the users username and password for a specific site, this information would then get past onto the next view which has a UIWebView inside. I would like to know if its possible to automatically fill in the username and password details on the site and send the user directly to their account page on the site.
Thanks.
assuming your website has div tags you can inject using stringByEvaluatingJavsScriptFromString.
This example injects a username into the twitter sign up page. You can test it by loading this in your viewDidLoad:
NSString *urlString = #"https://mobile.twitter.com/signup";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
and then in webViewDidFinishLoad inject the username:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
[webView stringByEvaluatingJavaScriptFromString:#"var field = document.getElementById('oauth_signup_client_fullname');"
"field.value='yourUserName';"];
}
sorry, just realised you already had the solution but were missing the implementation. Hope the above helps.
btw to add a dynamic variable into the injected javascript use:
- (void)webViewDidFinishLoad:(UIWebView *)webView;
{
NSString *injectedVariable = #"userNameHere";
NSString *injectedString = [NSString stringWithFormat:#"var field = document.getElementById('oauth_signup_client_fullname'); field.value='%#';", injectedVariable];
[webView stringByEvaluatingJavaScriptFromString:injectedString];
}
on solution is to "fake" the login request using the username and password and feed this into the web view.
I.e. you have a page index.php which has a username and password field.
if you fill them in, the page login.php is called with those parameters.
you could build an
NSString *urlString = #”http://www.yoursite.com/login.php?username=user&password=pass”;
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
that should do the trick

How to post on friend wall using Graph Api in iPhone

i am implementing a facebook application , i have used Graph Api and have successfully logged into facebook and got the friend list with the id in UITableView, now i have one string, how should i post on friend wall, Suppose if i click on any of my friend in UITableView, a message should be post,plz help me
EDIT: Code
NSString *urlString = [NSString stringWithFormat:#"graph.facebook.com/%#/feed",key];
NSURL *url = [NSURL URLWithString:urlString];
ASIFormDataRequest *newRequest = [ASIFormDataRequest requestWithURL:url];
[newRequest setPostValue:#"I'm inviting you to see download the OGC app" forKey:#"message"];
[newRequest setPostValue:fbGraph forKey:#"access_token"];
[newRequest setDidFinishSelector:#selector(postToWallFinished:)];
[newRequest setDelegate:self];
[newRequest startAsynchronous];
i am implementing this in function when clicked on tableview where key is id number for the friends name clicked in and in other function
NSString *responseString = [request responseString];
NSMutableDictionary *responseJSON = [responseString JSONValue];
NSString *postId = [responseJSON objectForKey:#"id"];
NSLog(#"Post id is: %#", postId);
but i am getting the post id nil plz help me
Are you using the Facebook iOS SDK?
Assuming you are, use requestWithGraphPath e.g.-
[_facebook requestWithGraphPath:#"uid/feed"
andParams:[NSMutableDictionary dictionaryWithObject:#"Post on wall" forKey:#"message"]
andHTTPMethod:#"POST"
andDelegate:self];
Where uid is the user id of the user, naturally.
You'll need to make sure you have the relevant permissions when signing in to Facebook and handle the delegate call back to test for success.
If you're not using the SDK, you can do a similar thing with the Graph API directly, perhaps using your own NSURLConnection's or a third party library like ASIHTTPRequest. The SDK is pretty good and straightforward to use mind.

could not authenticate OAuth while using twitter from sharekit in iphone app

I m implementing facebook & twitter share using sharekit in iphone app. Facebook share is working fine but twitter share gives error "could not authenticate OAuth".
I was having the same problem - this fixed it:
https://github.com/ideashower/ShareKit/issues/229
Summary:
Change SHKTwitter.m in lines 54-56 from
https://twitter.com/
to
https://api.twitter.com/
like this:
self.authorizeURL = [NSURL URLWithString:#"https://api.twitter.com/oauth/authorize"];
self.requestURL = [NSURL URLWithString:#"https://api.twitter.com/oauth/request_token"];
self.accessURL = [NSURL URLWithString:#"https://api.twitter.com/oauth/access_token"];
and in line 323, from http to https:
OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.twitter.com/1/statuses/update.json"]
consumer:consumer
token:accessToken
realm:nil
signatureProvider:nil];`

user credential persistence in iPhone

Since i'm new to iPhone programming, I want to do something like that: In the main screen of my app, i ask for username and password and authorize user using these info calling .net web service. In this main screen, there is a switch controller "Remember me" so that app will remember the user next time he run the app. My questions are:
1. how can i call .net web services in iPhone?
2. how can i persist these username and password information (like cookie in web app), so that user will not be asked for credential info?
i would really appreciate, thanks.
Persisting user credentials
If you are storing a username and password you should use the Keychain. Using the Keychain on iPhone is analogous to Keychain on the Mac.
To use Keychain import "Security/Security.h".
Apple has an example here of adding and retrieving a username and password in their documentation. The example methods
- (void)mySetObject:(id)inObject forKey:(id)key;
- (id)myObjectForKey:(id)key;
- (void)resetKeychainItem;
will enable you to persist and retrieve your user credentials almost without modifying the example code.
Calling webservices that require authentication
Depending on you authentication scheme you can either
provide username and password directly as parameters in the URL using NSURLConnection
provide a didReceiveAuthenticationChallenge method in your NSURLConnection delegate if your webservice use the "Basic Authentication" scheme
you use nsmutableurlrequest to generate a web services request
use nsuserdefaults to persist the username and password
Here's an example of the first:
-(void)sendUpdate {
User *user = [User sharedManager];
NSData *parkedLocation = [[NSString stringWithFormat:#"<location><latitude>%f</latitude><longitude>%f</longitude><userid>%#</userid><udid>%#</udid></location>",
coordinate.latitude, coordinate.longitude, userid, user.udid] dataUsingEncoding:NSASCIIStringEncoding];
//NSLog("parkedlocation = %#", parkedLocation);
NSString *URLstr = LOCATIONS_URL;
if (controller.put_url)
URLstr = controller.put_url;
NSURL *theURL = [NSURL URLWithString:URLstr];
//NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSLog(#"IN SEND UPDATE put_url = %#", controller.put_url);
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
if (controller.put_url) {
[theRequest setHTTPMethod:#"PUT"];
} else {
[theRequest setHTTPMethod:#"POST"];
}
[theRequest setValue:#"text/xml" forHTTPHeaderField:#"Content-type"];
[theRequest setHTTPBody:parkedLocation];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (!theConnection) {
NSLog(#"COuldn't get a connection to the iParkNow! server");
}
}
and an example of the second:
[[NSUserDefaults standardUserDefaults] setObject:self.userName.text forKey:#"Username"];
[[NSUserDefaults standardUserDefaults] setObject:self.password.text forKey:#"Password"];
You have two choices: User Preferences or Core Data. Both are capable - Core Data might be overkill.
NSURLRequest can be used to retrieve URLs such as your authentication service. And NsUserDefaults can be used to save settings in between app invocations.
Note: Check out ennuikiller's awesome example.