NSURLRequest with url containing special characters - objective-c

I'm having the same problem as:
NSURL with special characters
But tried their solution. Can't get my NSURLRequest to work with åöä characters. If the variable "string" contains åöä the request return null. Also tried with NSISOLatin1StringEncoding.
NSString *encodedString = [string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://suggestqueries.google.com/complete/search?output=firefox&q=%#", encodedString];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
This works:
http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%B6%C3%A4 (åöä)
Any ideas?
EDIT:
Using the debugger the NSURL looks correct:
string __NSCFString * #"åäö" 0x0a77cd60
encodedString __NSCFString * #"%C3%A5%C3%A4%C3%B6" 0x0a77fc40
url NSURL * #"http://suggestqueries.google.com/complete/search?output=firefox&q=%C3%A5%C3%A4%C3%B6" 0x0a79d1f0
Solved: The problem was not the NSURL it was how the return NSDATA was interpreted.

Instead of using
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
try using
NSString *encoded = [urlString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSURLRequest* request = [NSURLRequest requestWithURL: [NSURL URLWithString: encoded]];

Related

replace in NSURL

How can I do a string replace in NSURL?
I tried stringByReplacingOccurrencesOfString but it works with NSString.
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
I want to replace image url
http://example.com/image1.jpg
to
http://example.com/img1.jpg
NSURL has an absoluteString method that you could use like so
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
You could also directly operate on the NSString from the dataList as well:
NSString *urlString = [dataList[indexPath.item][#"image"] stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
imageURL = [NSURL URLWithString:urlString];
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSString *urlString = [imageURL.absoluteString stringByReplacingOccurrencesOfString:#"image" withString:#"img"];
NSString *webStringURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
imageURL = [NSURL URLWithString:webStringURL];
Hope This will help you.
You are going to replace the last path component of the URL so use the dedicated API of NSURL:
NSURL *imageURL = [NSURL URLWithString:dataList[indexPath.item][#"image"]];
NSURL *newURL = [[imageURL URLByDeletingLastPathComponent] URLByAppendingPathComponent:#"img.jpg"];
NSData *imageData = [NSData dataWithContentsOfURL: newURL];
stringByReplacingOccurrencesOfString could cause unexpected behavior.

How to append a variable to a string in xcode

Im trying to to append a variable to a string but its showing an error. Im missing something ridiculously easy here but my mind is shot.
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSURL *urla = [NSURL URLWithString:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=",eID];
You can't concat 2 string variables just by putting a comma between them. Try this instead:
NSURL *urla = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#",eID]];
If all you are doing is appending, you have several options:
A. Use NSString & stringWithFormat:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *urlString = [NSString stringWithFormat:#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=%#", eID];
NSURL *urla = [NSURL URLWithString:urlString];
B. Use NSString & stringByAppendingString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSString *urlString = [baseUrl stringByAppendingString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
C. Use NSMutableString & appendString:
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *baseUrl = #"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSMutableString *urlString = [NSMutableString stringWithString:baseUrl];
[urlString appendString:eID];
NSURL *urla = [NSURL URLWithString:urlString];
Try like this:-
NSString *eID = [entertainmentArticle objectForKey:#"eID"];
NSString *url=#"http://www.mydomain.com/iostest/appPHPs/schedule.php?eID=";
NSURL *urla = [NSURL URLWithString:[url stringByAppendingString:eID];

About xmlrpc of objective-c

Recently I have been working about an iOS project.I want to send my picture to an server.I use xmlrpc to do this ,but when I write codes like this:
- (void)uploadImage:(UIImage *)sourceImage ForName:(NSString *)imageName
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
I give "imageName" to params,it will fail.But if I write code like this
- (void)uploadImage:(UIImage *)sourceImage
{
NSData *imageData = UIImagePNGRepresentation(sourceImage);
NSString *imageString = [imageData base64EncodedString];
NSURL *URL = [NSURL URLWithString: SERVER_URL];
XMLRPCRequest *request = [[XMLRPCRequest alloc]
initWithURL:URL];
NSString *imageName = #"hello.png";
NSArray *params = [NSArray arrayWithObjects:imageName,imageString,nil];
[request setMethod:#"upload_image" withParameter:params];
It will be ok and I can upload the picture to the server.
I don't know why,can someone explain it ?

NSData to NSString with åöä

I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.
NSString *username = #"my_username";
NSString *password = #"my_password";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://www.mypage.com/login.php?username=%#&password=%#", username, password]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnPage = [NSString stringWithFormat:#"%.*s", [returnData length], [returnData bytes]];
This works fine except for special chars like åäö etc. Is there any better way?
Yes, use the following:
NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
That uses UTF8 instead of ASCII.

UIWebView doesn't load content

I don't understand why if I load the content of a UIWebView in XCode this way:
NSString *string = #"http://www.dummyurl.org/dummy.pdf";
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
everything works fine, but if a build the original string from a php script I wrote:
NSString *strURL = [NSSTring stringWithFormat:#"www.myserver.php"];
NSString *string = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:strURL]];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
nothing works. I checked my script and it returns EXACTLY the original string (http://www.dummyurl.org/dummy.pdf) that works fine with the first method.
I build from a PHP script the content of a UITextView too, but that works fine.
I don't understand why this method works with the UITextView but not with the UIWebView to load the .pdf.
It may be useful
NSCharacterSet *characterSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *tempString = [string stringByTrimmingCharactersInSet:characterSet];
NSURL *url = [NSURL URLWithString:string];
[my_view loadRequest:[NSURLRequest requestWithURL:url]];
Make sure you linked the webview (IBOutlet) and the delegate.
With this lines it should load a url:
webView.delegate = self;
NSURLRequest *urlRequest;
NSString *urlToOpen = [NSString stringWithFormat:#"http://www.stackoverflow.com"];
urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlToOpen]];
[self.webView loadRequest:urlRequest];
Hope this helps...
I had this and it was a cache problem:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];