Skip to content

Instantly share code, notes, and snippets.

@Adam0101
Adam0101 / NSRegularExpressions Example iOS.m
Created January 13, 2011 15:55
Example for blog post at www.remarkablepixels.com on NSRegularExpressions
- (NSString *)stripOutHttp:(NSString *)httpLine {
// Setup an NSError object to catch any failures
NSError *error = NULL;
// create the NSRegularExpression object and initialize it with a pattern
// the pattern will match any http or https url, with option case insensitive
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"https?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?" options:NSRegularExpressionCaseInsensitive error:&error];
// create an NSRange object using our regex object for the first match in the string httpline
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])];
// check that our NSRange object is not equal to range of NSNotFound
if (!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0))) {
@Adam0101
Adam0101 / NSRegularExpressionsExamplesubstitue.m
Created January 13, 2011 19:14
Example of character substitution using NSRegularExpression
- (NSString *)grabData:(NSString *)searchTerm {
// Setup an error to catch stuff in
NSError *error = NULL;
//Create the regular expression to match against
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\s" options:NSRegularExpressionCaseInsensitive error:&error];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
NSString *newSearchString = [regex stringByReplacingMatchesInString:searchTerm options:0 range:NSMakeRange(0, [searchTerm length]) withTemplate:@"%20"];
NSLog(@"New string: %@",newSearchString);
return newSearchString;
}
@Adam0101
Adam0101 / DismissMe.m
Created January 24, 2011 03:39
Example of a simple way to dismiss the iphone keyboard when a user clicks anywhere but the textfield
- (void)touchesEnded: (NSSet *)touches withEvent: (UIEvent *)event {
for (UIView* view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]])
[view resignFirstResponder];
}
}
@Adam0101
Adam0101 / iRateExample.m
Created February 21, 2011 22:09
Example of iRate
+ (void)initialize
{
//configure iRate
[iRate sharedInstance].appStoreID = 355313284;
}
@Adam0101
Adam0101 / searchTwitter.h
Created May 19, 2011 15:32
Search Twitter Class
#import <Foundation/Foundation.h>
@interface SearchTwitter : NSObject {
NSMutableData *tweetBlob;
}
- (void)grabData:(NSString *)searchTerm;
@end
@Adam0101
Adam0101 / sanitize.m
Created May 19, 2011 15:42
sanitize search twitter
//Create the regular expression to match against whitespace or % or ^
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(\\s|%|^)" options:NSRegularExpressionCaseInsensitive error:&error];
//Create the regular expression to match against hash tags
NSRegularExpression *regexHash = [NSRegularExpression regularExpressionWithPattern:@"#" options:NSRegularExpressionCaseInsensitive error:&error];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
NSString *tempString = [regex stringByReplacingMatchesInString:searchTerm options:0 range:NSMakeRange(0, [searchTerm length]) withTemplate:@"%20"];
// create the new string by replacing the matching of the regex pattern with the template pattern(whitespace)
@Adam0101
Adam0101 / urlsetup.m
Created May 19, 2011 15:42
setup twitter search url
// Build the string to search against the twitter search api
NSString *urlString = [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=&ands=%@&phrase=&rpp=50",searchTerm];
NSURL *url = [NSURL URLWithString:urlString];
@Adam0101
Adam0101 / dltweets.m
Created May 19, 2011 15:46
dl twitter search results
//Setup and start async download
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection release];
[request release];
// Gather up all the asynch data as it comes in and store it in the blob
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Check to see if this is a new search, if so it will be nil, so create an object
if(tweetBlob == nil) {
tweetBlob = [[NSMutableData alloc]init];
[tweetBlob appendData:data];
}
else {
[tweetBlob appendData:data];
@Adam0101
Adam0101 / gotem.m
Created May 19, 2011 16:18
finishedTweets
// Once the data is complete, then we can parse it
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// Store data blob we got back from twitter into a JSON string
NSString *jsonString = [[NSString alloc] initWithData:tweetBlob encoding:NSUTF8StringEncoding];
//Now use the Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];
// Build an Array from the dictionary for easy access to each entry