What code should I use to get & (not &) in the tweet? - iphone

I use Twitter-OAuth-iPhone to POST tweets by [_engine sendUpdate:myText];, which works fine. However, the letter & in the 'myText' will be changed to & in the tweet shown on Twitter.com. The & is for coordinates link: #"http://maps.google.co.uk/maps?f=q&source=s_q&hl=en&geocode=&q=%f,+%f".
I tried to replace the & with %%26 in the 'myText', which turns out with %26 in the tweet instead of &; and when I replace with %26, the app crash.
What code should I use to get & (not &) in the tweet?

You may need to use a URL shortener, since Twitter may be doing something to avoid SQL injection and the like.
Otherwise, does this work for you?
- (NSString *)stringByReplacingPercentEscapesFromString:(NSString *)inString
{
NSString *outString = (NSString *)CFURLCreateStringByReplacingPercentEscapes(NULL, (CFStringRef)inString, CFSTR(""));
return [outString autorelease];
}

Related

check if an string matches any value in an array?

Preamble: I'm working with some code I didn't write and I have next to no knowledge of iOS.
The app has a webview which handles most of the app; internal links are loaded in the webview and links to other sites are opened in Safari which is expected. However, embedding a google maps also causes safari to open. I found the relevant controller code, I think, and it looks like this:
// a bit higher up
static NSString *const siteURL = #"http://my.example.com"
if (![request.URL.absoluteString hasPrefix:siteUrl])
{
if ([[UIApplication sharedApplication] canOpenURL:request.URL])
{
[[UIApplication sharedApplication] openURL:request.URL];
}
return NO;
}
Now because 'maps.google.com' doesn't start with 'my.example.com' it farms out to safari - and that makes sense to me. However, I tried modifying this:
static NSString *const googleMaps = #"http://maps.google.com"
if (! ([request.URL.absoluteString hasPrefix:siteUrl] || [request.URL.absoluteString hasPrefix:googleMaps]))
{
if ([[UIApplication sharedApplication] canOpenURL:request.URL])
{
[[UIApplication sharedApplication] openURL:request.URL];
}
return NO;
}
This also works. But I need to add some more URLs. Is my only choice to keep adding 'or' and string constants? Or is there a way to make an array and say "if it matches any of these"? Sorry if this question seems simplistic.
In PHP I'd just do something like in_array('string', $array) as my condition, I guess I'm looking for how to do this in iOS
One solution would be to maintain a NSSet of objects, being all the strings that you want to be handled in your internal webview.
NSSet *internalURLSet = [NSSet setWithObjects:#"http://my.example.com", #"http://maps.google.com", nil];
Use a mutable set if you will be adding to the set over time but you will likely know which URLs you want to handle internally as soon as your app starts and this will not change. If you are adding sets as the app runs then add them to the mutable set as they become known.
When you check a URL, use [internalURLSet containsObject:urlToTest] and it will match any of the strings you have added to the set.
Checking against the set is very efficient. Adding objects to the set is less so, but you will very likely add URLs to the set only once while you check many times.
(Doing indexOfObject: against a NSArray is very inefficient...)
You could try using a predicate. Here's a tutorial link with an example (older, but should still work):
http://arstechnica.com/apple/2009/04/cocoa-dev-the-joy-of-nspredicates-and-matching-strings/
I do a lot of .NET development too and always miss LINQ when I go back to iOS. Predicates help a little, more lines of code but handy since you can do all kinds of searches.
I see your problem. In IOS, don't have a function in_array('string',$array) to check a string is contained in array.
I have a suggestion: you create a array with all urls. And implement 'for' or 'while' loop to get value of array check it. If it's same, you call function OpenUrl.
You could do this by creating an NSArray of your URLS and then using the indexOfObjectPassingTest: method to test your URL against each one in the array. It would go something like this:
NSArray *testURLs = #[ #"http://maps.google.com", #"http://www.example.com" ];
NSString *urlString = request.URL.absoluteString;
BOOL urlMatchedTest = NO;
for ( NSString *testURL in testURLs ) {
if [urlString hasPrefix:testURL] ) {
urlMatchedTest = YES;
}
}
if ( urlMatchedTest ){
// the url contains one of the test URLS
} else {
// it didn't match any of the test URLs
}

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).

How to cut out parts of NSString?

#"/News/some news text/"
#"/News/some other news text/"
#"/About/Some about text/"
#"/Abcdefg/Some abcdefg text/some more abcdefg text"
How do I cut out the first part of the strings, so that I end up with the following strings?
#"/News/"
#"/News/"
#"/About/"
#"/Abcdefg/"
Use componentsSeparatedByString: to break the string up:
NSArray *components=[string componentsSeparatedByString:#"/"];
if ([components count]>=2) {
// Text after the first slash is second item in the array
return [NSString stringWithFormat:#"/%#/",[components objectAtIndex:1]];
} else {
return nil; // Up to you what happens in this situation
}
If these are pathnames, you may want to look into the path-related methods of NSString, such as pathComponents and pathByDeletingLastPathComponent.
While it's pretty unlikely that the path separator is ever going to change, it's nonetheless a good habit to not rely on such things and use dedicated path-manipulation methods in preference to assuming that the path separator will be a certain character.
EDIT from the year 2013: Or use URLs (more specifically, NS/CFURL objects), which Apple has made pretty clear are the proper way to refer to files from now on, and are necessary for some tasks in a sandbox.

if-query: if (Nslog isEqualtoString #"...") - How can I make this?

I want my app to do something when the last NSLog has a certain string. I thought I could realize this with an if-query and isEqualtoString, but how can I make this?
Sorry for my bad English ;)
Maybe I don't understand what you're trying to do, but you can just create the string somewhere, log it, and then test it:
NSInteger _someInt = 2;
NSString *_someString = #"bananas";
NSString *_stringToBeLogged = [NSString stringWithFormat:#"%d %#", _someInt, _someString];
NSLog(#"%#", _stringToBeLogged);
if ([_stringToBeLogged isEqualToString:#"2 bananas"]) {
NSLog(#"I logged two bananas...");
}
You could consider creating your own custom log function which calls NSLog() after checking for your string constant. This would keep your code a bit cleaner if you want this functionality in multiple places and also allows you to easily extend the logging function further if desired.

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;