facebook profile picture url not passing in NSURL - objective-c

i am trying to pass profile pic url which is in the formate
https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042
but when i pass it like
NSString *fbpicurl = #"https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042";
NSString *post =[[NSString alloc] initWithFormat:#"profilepic=%#",fbpicurl];
NSURL *url=[NSURL URLWithString:#"http://URL.com"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
when i pass it this way url do not pass completely it just cut of where if founds & right after oh=6fda2708e5c1a5de47263 please help me how can i pass complete url string.

First encode your url string.
+ (NSString *) addPercentEscapesAndReplaceAmpersand: (NSString *) yourUrlString
{
NSString *encodedString = [yourUrlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
return [encodedString stringByReplacingOccurrencesOfString: #"&" withString: #"%20"];
}
Then used string for url by using NSURL URLWithString

Related

Contents from url to string

NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.

URL from string is null

I am trying to create a string from a URL but the URL is null. The string does certainly contain something but the URL is still null.
Here is where I create the URL:
NSData *stringData = feed.urlString;
NSString *stringForURL = [[NSString alloc] initWithData:stringData encoding:NSUTF8StringEncoding];
NSLog(#"Here is the string before being turned into a URL: %#", stringForURL);
NSURL *finalURL = [[NSURL alloc] initWithString:[stringForURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
finalURL = details.finalURL;
NSLog(#"Here is the final URL: %#", finalURL);
Change your final url code to below:-
Nstring *finalURL = [NSURL URLWithString:
[stringForURL
stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding]];

Bad URL Error With NSURLRequest

I have a NSURLRequest created next way:
NSString *url=[NSString stringWithFormat:#"http://myserveradress.com/api/users/register?register_password=%#&register_password_confirm=%#&register_email=%#&register_terms=1&register_user=%#&process_registration=1&register_name=%#"
,
[password stringByReplacingOccurrencesOfString:#" " withString:#"%"],[confirm stringByReplacingOccurrencesOfString:#" " withString:#"%"],[email stringByReplacingOccurrencesOfString:#" " withString:#"%"],[user stringByReplacingOccurrencesOfString:#" " withString:#"%"],[realName stringByReplacingOccurrencesOfString:#" " withString:#"%"]
];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *error;
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *result_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return result_string;
The result string is http://myserveradress.ru/api/users/register?register_password=password&register_password_confirm=password&register_email=myname#mail.com&register_terms=1&register_user=myname&process_registration=1&register_name=Artem%Kulikov and the error states
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x71e62b0 {NSUnderlyingError=0x71e6470 "bad URL", NSLocalizedDescription=bad URL}
In browser the link works fine. The problem is only in the app.
That is not the proper way to escape the strings, there is a method for that, stringByAddingPercentEscapesUsingEncoding:. So change it to:
NSString *url = [NSString stringWithFormat:#"http://myserveradress.com/api/users/register?register_password=%#&register_password_confirm=%#&register_email=%#&register_terms=1&register_user=%#&process_registration=1&register_name=%#"
, password, confirm, email, user, realName];
NSString *properlyEscapedURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:properlyEscapedURL]];
Not to mention the proper escape code for a space is %20 not %.
#user1506704, you are not use encoding method like in register_name, Are there spaces or other special characters in the register_name? Use NSString method stringByAddingPercentEscapesUsingEncoding:.
You can try and escape the URL:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding`

encode a NSString setHttpPostBody for nsurlconnection

String content = "username="+ URLEncoder.encode(username, UTF_8_TYPE);
want to do a similar in objectiveC
NSString *body = [NSString stringWithFormat:#"username=%#", [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURL returns Nil Value

Here Below is my code
NSString *string = [NSString stringWithFormat:#" http://abc.com /Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#& ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
Here in string there is value but url returns nil.
Can Anyone tell why this happened.
Thanks ....
"This won't work, so here's what I did instead"
NSString *string = [NSString stringWithFormat:#"http://abc.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];
NSURL will return nil for URLs that contain illegal chars, like spaces.
Before using your string with [NSURL URLWithString:] make sure to escape all the disallowed chars by using [NSString stringByAddingPercentEscapesUsingEncoding:].
Here is the class reference for NSString.
Hi All Now My Problem Is solved here I did a mistake left a blank space before "http".
The correct code is
NSString *string = [NSString stringWithFormat:#"http://myserver.com/Demo/View.php?drinkId=%#&name=%#&comment=%#&date=%#&rating=%#&ReqestType=SubmitComment",DrinkId,Name,Comment,Date,Rating];
NSURL *url = [[NSURL alloc] initWithString:string];