Skip to content

nbasham/iOSUtil

Repository files navigation

iOSUtil

  • CaptureView - creates a PDF or UIImage screenshot from a UIView.
  • DelimitedFile - creates an array of user defined objects from a delimited file.
  • EmailController - a simple way to send email from a UIViewController.
  • NSArray+Primitive - a convenient way to use NSArray with primitive types.
  • ShareController - a simple way to share items via Facebook, Twitter, Weibo, Message, Mail, Print, Copy to Pasteboard, Assign to Contacts, and Save to Camera Roll.

CaptureView

Note: This implementation uses the CPU not GPU, and assumes the scale of the screen.

NSData* pdfData = [CaptureView viewToPdf:self.view];
UIImage* screenshotImage = [CaptureView viewToImage:self.view];

See ViewController.m in sample project for examples.

DelimitedFile

Example: Creates an array of user defined objects populated with a tab separated file. In this example, objects of type Band are created from file "data.tsv":

NSArray* bands = [DelimitedFile load:@"data.tsv" toClass:[Band class] delimeter:kTabDelimeter];

Band.h

  @interface Band : NSObject<DelimitedFileDelegate>
  @property(nonatomic, assign)int uid;
  @property(nonatomic, strong)NSString* name;
  @property(nonatomic, strong)NSArray* members;

Band.m

-(void)fromValues:(NSArray*)tsv {
	self.uid = [[tsv objectAtIndex:0] intValue];
	self.name = [tsv objectAtIndex:1];
	self.members = [[tsv objectAtIndex:2] componentsSeparatedByString:@","];
}

"data.tsv"

1 Crazy Horse Billy,Pancho,Ralph,Neil,Danny
2 The Band Rick,Levon,Garth,Richard,Robbie,Jim,Stan,Randy,Richard

Full implementation in sample project. Similarly, you can do the same with any delimiter e.g. comma separated (CSV) files (see sample project).

Questions:

  • Is there a real world case for using a NSCharacterSet instead of a char delimiter?
  • Should fromValues return a BOOL allowing filtering of some objects from the array?

EmailController

Note: Requires MessageUI.framework

Example 1: The snippet below displays Apple's mail composer

emailController = [[EmailController alloc] init];
[emailController sendEmail:self];

Example 2: The snippet below emails a screenshot of the current UIViewController with an HTML body.

emailController = [[EmailController alloc] init];
emailController.attachmentName = @"screenshot.png";
emailController.subject = @"Image example";
emailController.isHTML = YES;
emailController.body = @"Enclosed is an <b>image</b>.";
UIImage* screenshotImage = [CaptureView viewToImage:self.view];
[emailController attachImage:screenshotImage];
[emailController addRecipient:@"steve@mac.com"];
[emailController sendEmail:self];

See ViewController.m in sample project for examples. The reference to emailController must be retained for the life span of the email composer.

Optional Properties

NSData* attachment NSData to be attached to email.
NSMutableArray* recipients Array of recipients, instantiated in init.
NSString* subject Text to appear as email's subject.
NSString* body The default body of the email. This value can be changed by user.
NSString* mimeType The attachment mime type.
NSString* attachmentName The name of the attachment, this is visible to the user.
BOOL isHTML Defaults to YES. Treat body as HTML.
BOOL logResult Send log info to console.

NSArray+Primitive

Notes:

  • #import "NSArray+Primitive.h" before using.
  • These arrays support serialization via NSCoding.
  • Current support for BOOL, char, int and float but very easy to extend to any type (see NSNumber).

Example 1: Creating a char NSArray.

NSMutableArray* charArray = [NSMutableArray array];
for (char c = 'a'; c <= 'z'; c++) {
    [charArray addChar:c];
}

Example 2: Inserting and replacing a char NSArray.

NSLog(@"%c", [charArray charAtIndex:13]); // prints 'n'
NSMutableArray* charArray = [NSMutableArray array];
for (char c = 'a'; c <= 'z'; c++) {
    [charArray addChar:c];
}
[charArray insertChar:'3' atIndex:3];
[charArray replaceCharAtIndex:13 withChar:'N'];
NSLog(@"%c", [charArray charAtIndex:0]);   //  prints a
NSLog(@"%c", [charArray charAtIndex:3]);   //  prints 3
NSLog(@"%c", [charArray charAtIndex:13]);   //  prints N

ShareController

> Note: Requires Social.framework linked as 'optional'. Requires iOS 6, previous iOS versions display an alert.

Example 1: The snippet below displays an image via Apple's share panel.

NSArray* items = @[@"ShareController example", [UIImage imageNamed:@"crazyHorse.png"]];
[ShareController showFromParent:self items:items];

Example 2: The snippet below displays an image via Apple's share panel excluding the 'Copy To Pasteboard' service. It also demonstrates using a callback.

  NSArray* items = @[@"ShareController example", [UIImage imageNamed:@"theBand.jpg"]];  
  NSArray* excludes = @[UIActivityTypeCopyToPasteboard];
	[ShareController showFromParent:self items:items excludes:excludes callback:^(NSString* activityType, BOOL performedService) {
  	if(activityType == nil) {
  		NSLog(@"%@", @"Share panel was dismissed by user.");
  	} else {
  		NSLog(@"%@ was %@.", activityType, performedService ? @"performed" : @"cancelled");
  	}
  }];

About

Utilities for iOS development.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published