iphone snippets

Fix for "Code Sign error: The identity 'iPhone Developer' doesn't match any valid certificate/private key pair in the default keychain"

Tagged iphone, xcode, titanium, certificate  Languages 

I got this error the other day:

Code Sign error: The identity 'iPhone Developer' doesn't match any valid certificate/private key pair in the default keychain

The fix for me was this:

  • Login to iPhone Developer Program
  • Click on Certificates
  • Under Current Development Certificates click on the button to download your certificate. It gets saved as developer_identity.cer.
  • Open the Keychain tool
  • Make sure the "login" keychain is selected in the left pane.
  • Make sure that "login" is the default keychain (right-click on the login item to do this)
  • Drag the developer_identity.cer file to the login keychain's list of certificates.
  • Build your app

How to load a local file into UIWebView

Tagged iphone, uiwebview  Languages objectivec

First create index.html. Next add it to the Resources folder by right-clicking and selecting Add existing files from the menu.

Now you can display the file in a UIWebView with the following code:

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];

Via http://www.iphonedevsdk.com/forum/iphone-sdk-development/1784-how-can-i-access-local-html-file-using-uiwebview.html

How to extract current year, month, and day from an NSDate with NSCalendar, NSDateComponents, and NSDateFormatter

Tagged year, month, day, nsdateformatter, nsdate, nscalendar, nsdatecomponents, iphone  Languages objectivec

You have three options to achieve what the title says:

Using NSDate and NSCalendar to extract current year, month, and day

NSDate *now = [NSDate date];
// Specify which units we would like to use
unsigned units = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [calendar components:units fromDate:now];

NSInteger year = [components year];
NSInteger month = [components month];
NSInteger day = [components day];

Using NSDate descriptionWithCalendarFormat to extract current year, month, and day

NSString *year = [now descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:nil];
NSString *month = [now descriptionWithCalendarFormat:@"%m" timeZone:nil locale:nil];
NSString *day = [now descriptionWithCalendarFormat:@"%d" timeZone:nil locale:nil];

The code will compile but spit out the following warning, because it's not supported on the iPhone:

warning: 'NSDate' may not respond to '-descriptionWithCalendarFormat:timeZone:locale:

Using NSDateFormatter to extract current year, month, and day

NSDate *now = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy"];
NSString *year = [formatter stringFromDate:now];
[formatter setDateFormat:@"MM"];
NSString *month = [formatter stringFromDate:now];
[formatter setDateFormat:@"dd"];
NSString *day = [formatter stringFromDate:now];

How to load a local file into a UIWebView

Tagged iphone, uiwebview, file  Languages objectivec

First you'll need to setup a UIWebView in for example Interface Builder. Then in the controller you add this incredible piece of code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"about" ofType:@"html" inDirectory:@"html"];
NSLog(@"%@", path);
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];

This beautiful piece of code will look for a file named about.html in a folder called html. Now go create that folder and the file with your favorite tools.

Next drag that folder under your application in Groups & Files in XCode. Now to make XCode happy you have to make sure you select Create Folder References for any added folders.

That's it. If you get this error it means the file couldn't be found, and that you didn't import the files correctly:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL initFileURLWithPath:]: nil string parameter'