iOS stringWithContentsOfURL gives HTML error - iphone

I am trying to Parse Google Maps api with SBJSon but before parsing when I use the below code gives me the Access Denied errors in the HTML this error comes only when I run on the Device and If I run the same in the simulator ..it gives me the google maps api string..what should be the error for Device running.
NSString *String = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
NSLog(#"string %#",string);
error
<HTML><HEAD>
<TITLE>Access Denied</TITLE>
</HEAD>....
full Html error is similar of
Setting Proxy Username & password using Objective-C - iPhone

The network your device is connected to does not allow access to the URL. Since you are able to access the URL from your MAC, connect your device to the same network as your MAC. It should work.
Alternate solution, first access some URL from the device Safari browser. If asked for authentication, provide correct username and password. Once connected, do not close the tab or Safari app. Run your application and the URL should now work.
EDIT
If you still face authentication problem, may be you are better off implementing asynchronous downloader which implements NSURLConnectionDelegate. Provide the authentication parameters in method,
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
The API you are using stringWithContentsOfURL:encoding:error: will not give you much flexibility.
Hope that helps!

Related

UIWebView not loading https link

I am trying to load one secure HTTP url into UIWebView, but it throws me an "untrusted certificate error" i.e error -1202. NSURLDomainError.
I searched on stackoverflow and found out this solution and implemented the same:
UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?
As per this solution after receiving a callback to :
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
We again load the secure http url to webview, and as we have already done the authentication pass,
it loads the data.Also, the credential the server uses are my personal crendetials, so i am mentioning the NSURL
NSURLCredential *newCredential = [NSURLCredential credentialWithUser:#"username"
password:#"password"
persistence:NSURLCredentialPersistenceForSession];
But my problem here is, it just goes into some kind of loop, and i am not able to get any of the callback of UIWebView. Any solution or any idea what the problem will be?

stringWithContentsOfURL different result with wifi

I have the following code:
NSString *url = [NSString stringWithContentsOfURL:myURL encoding:NSStringEncodingConversionAllowLossy error:&error];
and I get a different string using Wifi than with my mobile network.. Basically it's the same, but I get it without the spaces.. just like this in wifi:
<html> hello </html>
would be this with my mobile network:
<html>hello</html>
how can this be?
I am using iPhone SDK 6.0 and Xcode 4.5.
I think this is because, when you try to get data using mobile data, the information in the header is sent that the request has been with mobile data, so it tries to minimize the response, and thats not the case with wifi, it doesn't try to minimize the response while using wifi.

Google+ integration in iPhone [duplicate]

I have been searching for tutorials on google for posting some text on google plus. But it seems like there are none.
I have also tried to go through the docs provided by google for developers of mac and iPhone but can't find anything that will solve my problem. Also there are little information on how to get the user login to there google plus account.
I am not sure what to do for the user login, do I have to use some GTLObject or use UIWebView like foursquare for user login.
Here is a list of docs that I had went through.
http://code.google.com/p/google-api-objectivec-client/wiki/Introduction
http://code.google.com/p/google-api-objectivec-client/wiki/BuildingTheLibrary
http://code.google.com/p/gtm-oauth2/
As it turns out there are only a limited number of api's available for google + developers and that also only as GET calls according to the developer of google + page my question will not get any definite answers as google is in the process of creating new api's for accessing user information on google plus.
https://developers.google.com/+/api/
Also you can use the google client sdk provided by google but it is much easier to show a webview for user login. I managed to get the people list from google plus.
The steps are same as for getting the access token as in foursquare. Just with some small changes.
in viewdidload method.
NSString *authStr = #"https://accounts.google.com/o/oauth2/auth?client_id=client_id&redirect_uri=http://somevalidurl.com&scope=https://www.googleapis.com/auth/plus.me&response_type=token";
as a url for loading request in webview. People should note one thing here that you need to create a client id in api console for your app that is web based and not as installed for this purpose as you wont we getting any option to enter any website url for callback which is very important in this case.
and in webview delegate method webViewDidFinishLoad:
NSString *urlStr = [[webView.request URL] absoluteString];
NSArray *array = [urlStr componentsSeparatedByString:#"access_token="];
if(array.count > 1)
{
NSString *oauth_token = [[[array objectAtIndex:1] componentsSeparatedByString:#"&"] objectAtIndex:0];
//do something with the oauth token after this.
}
I wrote a code snippet to signin and post simple text to Google Plus with URL and thumb Image URL.
Try it
Take a look here:
http://code.google.com/p/google-api-objectivec-client/source/browse/#svn%2Ftrunk%2FExamples%2FBooksSample
http://code.google.com/p/google-api-objectivec-client/

ASIHTTPRequest problem with parameters using 3G conneciton (Fine with Wifi)

I have one user of my iPhone application who is complaining it does not work when using his mobile data connection (but works fine via Wifi). My application make a request to a third party REST API and I use the ASIHTTPRequest library for this.
One parameter of my HTTP request is a username (which is an email address), so I encode the username using the following code:
-(NSString *) encodeString:(NSString *) string
{
NSString * returnString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef) string,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 );
return [returnString autorelease];
}
This then gets built into a URL as follows:
// Setup the url we need to make the request to the sharkscope API
NSString * urlString = [NSString stringWithFormat: #"%SERVICEADDRESSHERE?Username=%#&Password=%#",
encodedUserName, m_md5Key];
NSURL * url = [NSURL URLWithString:urlString];
request = [[ASIHTTPRequest requestWithURL:url] retain];
What get's sent out of my application is a request with the following Username:
Username=Fred.Bloggs%40wanadoo.fr
For this one user, what is received at the server when using his mobile data connection is:
Username=Fred.Bloogs%2540wanadoo.fr
So clearly the % character is being encoded somewhere between me making the call to requestWithURL and it arriving at the server. I can't understand the reason because I have hundreds of other users who can run the requests fine via their data connection.
Do I even need to encode the values passed into requestWithURL?
Does anyone have any idea at what stage the % character is being re-encoded. I'm guessing it must be in ASIHTTP library, but can't work out what circumstances would trigger it in this case.
The corruption is probably happening because of a proxy or transparent proxy on the 3G connection, put in place by the carrier.
Possibly workounds:
Use POST instead of GET
Use https instead of http
Use http on a non-standard port
To prove this theory, you could try asking him to enter the same / similar request in Safari on iOS when he's using the 3g connection.

Tracking iPhone on Yahoo Web Analytics using ASIHTTPRequest

I'm trying to track an event in my app using Yahoo Web Analytics. The code I am using looks like
ASIHTTPRequest *yahooTrack = [ASIHTTPRequest requestWithURL:
[NSURL URLWithString:#"http://s.analytics.yahoo.com/p.pl?a=xxxxxxxxxxxxx&js=no&b=yyyyyyyyyyyy&cf6=zzzzzzzzzzz"]];
yahooTrack.didFinishSelector = #selector(statisticsFinished:);
yahooTrack.delegate = self;
[yahooTrack startAsynchronous];
Then the statisticsFinished looks like:
NSLog(#"Cookies: %#", request.requestCookies);
NSLog(#"Redircount: %d", [request redirectCount]);
NSLog(#"Responsecode %d %#\nMsg: %#", request.responseStatusCode,
request.responseStatusMessage, [request responseString]);
And all the information I get back looks correct. Cookies are set, redirectcount is 1 the first time (as it redirects to s.analytics.yahoo.com/itr.pl?.... a normal browser does). Then the redirectcount is 0 for subsequent request until the app is restarted and session cleared. The responseString returns GIF89a.
Even if the data looks correct, Yahoo still won't track. As soon as I call the tracking url directly in my browser it works as expected.
I realize Flurry is a better option, but I'm forced to use Yahoo in this case. Also, using a UIWebView probably would work, but I'm against putting in a webview just for tracking purposes.
Is there any difference in how ASIHTTPRequest and Safari would handle a call to a simple URL as this? Or do you see anything else that could explain why the tracking isn't working?
I finally found the problem. ASIHTTPRequest creates a user-agent based on your applications name, and requests from this user agent is ignored by Yahoo somehow (bug?). As stated in the documentation, you can override the user-agent as follows:
[request addRequestHeader:#"User-Agent" value:#"My-User-Agent-1.0"];
I used the user-agent string of Safari on iPhone, and it worked immediately! BTW; the same problem applies for Android, and the same fix works.