Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
codeswimmer / ios_screen_brightness.m
Created January 19, 2013 16:39
iOS: how to change the screen brightness
[[UIScreen mainScreen] setBrightness:0.5];
@codeswimmer
codeswimmer / add_char_to_end_of_line.sh
Created January 18, 2013 17:21
bash: Add a '\' character to the end of each line in a file
sed 's/$/ \\/' payload.txt > result.txt
@codeswimmer
codeswimmer / ios_lookup_word.m
Last active December 10, 2015 12:58
iOS: How To Find A Word Definition In iOS Dictionary
/* iOS 5 has a new framework to look for a word definition and present the definition.
*
* UIReferenceLibraryViewController is a subclas of UIViewController, so you can push
* or present it as modal view controller to see the definition.
*/
NSString *term = @"Reference";
if ([UIReferenceLibraryViewController dictionaryHasDefinitionForTerm:term]) {
NSLog(@"Term: %@, has definition", term);
@codeswimmer
codeswimmer / ios_simple_pasteboard.m
Created January 2, 2013 20:10
iOS: Simple Usage Of UIPasteBoard
Basically, UIPasteBoard allows us to share data to other application. Below is an example of UIpasteBoard usage.
COPY
UIPasteboard *appPasteBoard = [UIPasteboard generalPasteboard];
appPasteBoard.persistent = YES;
[appPasteBoard setString:@"STRING TO COPY"];
PASTE
@codeswimmer
codeswimmer / ios_list_files_in_dir.m
Created January 2, 2013 20:10
iOS: How To Get A List Of Files In A Directory
This example below list down all files inside the application resource.
NSString *documentsDirectory = [[NSBundle mainBundle] resourcePath];
NSError *err;
NSArray *directoryContentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&err];
NSLog(@"%@", directoryContentArray);
@codeswimmer
codeswimmer / ios_sharing_files.m
Created January 2, 2013 20:09
iOS: Sharing Files With The User
From Apple.
An application that wants to make files accessible to the user can do so through the file sharing feature. File sharing exposes the contents of an application’s Documents directory in iTunes. Exposing this directory allows the user to add files to it or delete files from it. To enable file sharing for your application, do the following:
Add the UIFileSharingEnabled key to your application’s Info.plist file and set the value of the key to YES.
Put whatever files you want to share in your application’s Documents directory.
When the device is plugged into the user’s computer, iTunes displays a File Sharing section in the Apps pane of the selected device. The user can add files to this directory or move files to the desktop.
@codeswimmer
codeswimmer / ios_url_encode_string.m
Created January 2, 2013 20:08
iOS: URL Encode A String
This is useful so that before you process any URL request from a URL String, this URL String becomeURL encoded and gets replaced to percent escapes.
iOS 5: iOS 5 requires A Toll-Free Bridging
- (NSString *)escape:(NSString *)text
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes( NULL, (__bridge CFStringRef)text, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
}
Previous iOS:
@codeswimmer
codeswimmer / ios_array_string_search.m
Created January 2, 2013 20:07
iOS: Search A String In Array
-(void)searchString:(NSString *)str inArray:(NSArray *)arr{
for (id key in arr) {
//check if there is a matching string (key and str)
if ([key rangeOfString:str options:NSCaseInsensitiveSearch].location != NSNotFound) {
NSLog(@"String: %@ Found", key);
}
}
}
@codeswimmer
codeswimmer / ios_app_tmp_dir.m
Created January 2, 2013 20:07
iOS: How To Access The Application Temporary Directory
Getting a reference to your application’s temporary directory is even easier than getting a reference to the Documents directory. The Foundation function called NSTemporaryDirectory() will return a string containing the full path to your application’s temporary directory. To create a filename for a file that will get stored in the temporary directory, we first find the temporary directory:
NSString *tempPath = NSTemporaryDirectory();
Then, we create a path to a file in that directory by appending a filename to that path, like this:
NSString *tempFile = [tempPath stringByAppendingPathComponent:@”tempFile.txt”];
@codeswimmer
codeswimmer / ios_image_join.m
Created January 2, 2013 20:06
iOS: How To Join Two Images
(UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();