how to trim thi url for iphone - iphone

how to remove degrre symbol form this URL
NSString *s= http://www.com/appfeed/default.php?app=1.31223°|103.865**°**|56a76d
i want to trim that degree symbol( lat and long ) from this string plz help
Thanks

You should take a look at the NSString documentation.
You can use the stringByReplacingOccurrencesOfString:withString method.
s = [ s stringByReplacingOccurrencesOfString: #"°" withString: #"" ];

You can do it using bitly.Click here to get example in which url is trim using bitly api.

Related

Parsing URL Using Regular Expression

I need to parse a URL in the following format:
http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue
All I need is the value of 1893736 within &id=1893736.
I need to do the parsing in Objective-C for my iPhone project. I understand it must have something to do with regular expression. But I just have no clue how to do it.
Any suggestions would be appreciated. :)
You don't need a regex for this. You can try something like this
NSString *url = #"http://www.example.com/?method=example.method&firstKey=firstValue&id=1893736&thirdKey=thirdValue";
NSString *identifier = nil;
for (NSString *arg in [[[url pathComponents] lastObject] componentsSeparatedByString:#"&"]) {
if ([arg hasPrefix:#"id="]) {
identifier = [arg stringByReplacingOccurrencesOfString:#"id=" withString:#""];
}
}
NSLog(#"%#", identifier);
Don't use regular expressions. Use NSURL to reliably extract the query string and then use this answer's code to parse the query string.
Use this:
.*/\?(?:\w*=[^&]*&)*?(?:id=([^&]*))(?:&\w*=[^&]*)*
And grap first group: \1. You will obtain 1893736.
Simplifying
If the id can consist of only digits:
.*/\?(?:\w*=[^&]*&)*?(?:id=(\d*))(?:&\w*=[^&]*)*
If you don't care about capturing uninterested groups (use \3 or id in this case):
.*/\?(\w*=.*?&)*?(id=(?<id>\d*))(&\w*=.*)*
More simpler version (use \3):
.*/\?(.*?=.*?&)*(id=(\d*))(&.*?=.*)*
Instead of using regex, you can split the string representation of your NSURL instance. In your case, you can split the string by the appersand (&), loop the array looking for the prefix (id=), and get the substring from the index 2 (which is where the = ends).

iphone string replace only the first occurrence with another string

I have a string in iphone that looks like the following one:
my.whatever.string.with.unknown.length=something whatever something = something .... = whatever
I want to replace only the FIRST "=" with something else.
If I use :
[myString stringByReplacingOccurrencesOfString:#"=" withString:#"somethingHere" ];
It will replace all occurences of "=" . Any suggestions on how to achieve this?
You can use the range argument to specify how far into the string you want it to search. Assuming you can find where the first = is, you can use that to do it. The method definition looks like this:
replaceOccurrencesOfString:withString:options:range:
You can replace string like this..
NSString *mystring=FIRST;
NSString *trimmedString = [mystring stringByReplacingOccurrencesOfString:#" " withString:#""];

Junk character in Webview

I am getting html content as a string in my webservice response which contains "&nbsp" in it. When I display that data in webview, "&nbsp" is converted in junk character. Please let me know how to solve it.
You have to read the string as NSUTF8 encoded string and then pass the string to web view using "loadHTML" method mentioned in UIWebView.
Not only that, if you want to display special characters like copy right, double quotes etc or other language characters in the HTML, you have to use UTF8 encoding.
Use the stringByReplacingPercentEscapesUsingEncoding: method of NSString
like :
NSString *decoded = [yourString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Also to remove use
[yourString stringByReplacingOccurrencesOfString:#" " withString:#" "];
stringByReplacingOccurrencesOfString is deprecated from ios 9
let decoded = yourString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())

Problem in downloading image from server in iPhone

I have an Image named "John and Tony.png".When I tried to download the image from my code with this name via an http request no image is shown.But if I remove the space between the words like "JohnandTony.png" then there is no problem to download the image.But I can't want to remove the spaces.Is there any way to do that?
Thanx
you need to escape the spaces with "%20"s.
NSString *imageStr = #"http://whatever.com/John and Tony.png";
NSString *imageStrEscaped = [urlStr stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
Then use this string as the url.
I think you are creating the imagestring the wrong way. Try the following:
NSString *imagestring = #"28.162.10.2/images/John and Tony.png";
imagestring = [imagestring stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
Your URL doesn't must contain spaces.
If you want let this name, you must use the %20 to replace the spaces like that:
"John%20and%20Tony.png"

How do I encode "&" in a URL in an HTML attribute value?

I'd like to make a URL click able in the email app. The problem is that a parameterized URL breaks this because of "&" in the URL. The body variable below is the problem line. Both versions of "body" are incorrect. Once the email app opens, text stops at "...link:". What is needed to encode the ampersand?
NSString *subject = #"This is a test";
NSString *encodedSubject =
[subject stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *body = #"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>"; //original
NSString *body = #"This is a link: <a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>"; //have also tried &
NSString *encodedBody =
[body stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *formattedURL = [NSString stringWithFormat: #"mailto:myname#somedomain.com?subject=%#&body=%#", encodedSubject, encodedBody];
NSURL *url = [[NSURL alloc] initWithString:formattedURL];
[[UIApplication sharedApplication] openURL:url];
the ampersand would be %26 for HEX in URL Encoding standards
I've been using -[NSString gtm_stringByEscapingForURLArgument], which is provided in Google Toolbox for Mac, specifically in GTMNSString+URLArguments.h and GTMNSString+URLArguments.m.
You can use a hex representation of the character, in this case %26.
you can simply use CFURLCreateStringByAddingPercentEscapes with CFBridgingRelease for ARC support
NSString *subject = #"This is a test";
// Encode all the reserved characters, per RFC 3986
// (<http://www.ietf.org/rfc/rfc3986.txt>)
NSString *encodedSubject =
(NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)subject,
NULL,
(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8));
You use stringByAddingPercentEscapesUsingEncoding, exactly like you are doing.
The problem is that you aren't using it enough. The format into which you're inserting the encoded body also has an ampersand, which you have not encoded. Tack the unencoded string onto it instead, and encode them (using stringByAddingPercentEscapesUsingEncoding) together.
<a href='http://somewhere.com/two.woa/wa?id=000&param=0'>click me</a>
Is correct, although ‘&’ is more commonly used than ‘&’ or ‘,’.
If the ‘stringByAddingPercentEscapesUsingEncoding’ method does what it says on the tin, it should work(*), but the NSString documentation looks a bit unclear on which characters exactly are escaped. Check what you are ending up with, the URL should be something like:
mailto:bob#example.com?subject=test&body=Link%3A%3Ca%20href%3D%22http%3A//example.com/script%3Fp1%3Da%26amp%3Bp2%3Db%22%3Elink%3C/a%3E
(*: modulo the usual disclaimer that mailto: link parameters like ‘subject’ and ‘body’ are non-standard, will fail in many situations, and should generally be avoided.)
Once the email app opens, text stops at "...link:".
If ‘stringByAddingPercentEscapesUsingEncoding’ is not escaping ‘<’ to ‘%3C’, that could be the problem. Otherwise, it might not be anything to do with escapes, but a deliberate mailer-level restriction to disallow ‘<’. As previously mentioned, ?body=... is not a reliable feature.
In any case, you shouldn't expect the mailer to recognise the HTML and try to send an HTML mail; very few will do that.
Example of use of %26 instead of & without this attributes arrived in PHP as an array!
var urlb='/tools/lister.php?type=101%26ID='+ID; // %26 instead of &
window.location.href=urlb;