encode a NSString setHttpPostBody for nsurlconnection - objective-c

String content = "username="+ URLEncoder.encode(username, UTF_8_TYPE);
want to do a similar in objectiveC

NSString *body = [NSString stringWithFormat:#"username=%#", [username stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Related

facebook profile picture url not passing in NSURL

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

Conversion of NSData to NSString?

I am converting NSData to NSString with following code
NSData *videodata=-----?
NSString *urlstring = [[NSString alloc] initWithData:videodata encoding:NSUTF8StringEncoding] ;
I also tried
NSString *urlstring= [NSString stringWithUTF8String:[videodata bytes]];
But both returns null value.Is there any other process.Thanks
NSData *somedata = [NSData data];
NSString *str = [[NSString alloc] initWithData:somedata encoding:NSUTF8StringEncoding];
Or replace the encoding with the encoding of the data

UItextFields Data to JSON

i have two UItextFields, userNameTextField and passwordTextField and a UIbutton connection. I would like to have what the user typed in the userNametextField and the passwordTextField, How i can do this please ?? thanks for your answer.
#pragma mark - User auhtentification
-(IBAction)userAuthentificate{
BOOL loginValid = YES;
BOOL passwordValid = YES; // Not specified yet
// contrioll d'interfac__ on test login et password
if (loginValid && passwordValid) {
NSString *jsonRequest = #"{\"login\":\"Samir\",\"password\":\"test\",\"mail\":\"test#gmail.com\"}";
// NSString *jsonRequest = #"{\"login\":\"Samir\",\"password\":\"test\",\"mail\":\"samir#gmail.com\",\"editor\":\"1\"}";
NSString *request_url =#"http:.../register"; // Url WS
NSURL *url = [NSURL URLWithString:request_url];
self.currentRequest = [ASIFormDataRequest requestWithURL:url];
currentRequest.delegate =self;
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];
[self.currentRequest appendPostData:requestData];
[self.currentRequest setRequestMethod:#"POST"];
[self.currentRequest startSynchronous];
}
}
You can put something like this in the place of your jsonRequest string (the code uses the SBJson framework):
NSDictionary *container = [[NSDictionary alloc] initWithObjectsAndKeys:
[loginTextField text], #"login",
[passwordTextField text], #"password",
[mailTextField text], #"mail"];
NSString *jsonString = [container JSONRepresentation];
// don't forget to release container if not using ARC AFTER creating the NSData object
How about a simple string replacement?
NSString *jsonRequest = [NSString stringWithFormat:#"{\"login\":\"%#\",\"password\":\"%#\",\"mail\":\"%#\"}", userNameTextField.text, passwordTextField.text, emailtextField.text];
Use
NSString *jsonRequest = [NSString stringWithFormat:#"{\"login\":\""%#"\",\"password\":\""%#"\",\"mail\":\"test#gmail.com\"}",userNameTextField.text,passwordTextField.text];

String won't url encode in iOS

I'm seriously having a brain fart here, but I can't figure out why this isn't encoding for the life of me. Been searching all over, and I can't even get the code samples to encode. Any ideas?
NSString *searchString = #"waffl&es";
NSString *encodedSearchString = [searchString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://en.wikipedia.org/?search=%#", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
For future reference, this is what I found to work (i.e. encode everything properly)
+ (NSString*)encodeURL:(NSString *)string
{
NSString *newString = (__bridge_transfer NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)string, NULL, CFSTR(":/?#[]#!$ &'()*+,;=\"<>%{}|\\^~`"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
if (newString)
{
return newString;
}
return #"";
}

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];