Tag Archives: iphone

Error adding EKEvent on iOS 5

If you try to add an EKEvent with startDate equals to endDate to EKEventStore on iOS5, it will throw an error like “No end date has been set”.
To get rid of this problem, you should add a second to endDate property, something like:

if ([event.endDate isEqualToDate:event.startDate]) {
    event.endDate = [event.startDate dateByAddingTimeInterval:1.0]; // add one second
}

Source: stack overflow

After upgrading ASIHttpRequest, responseStatusCode returns 0

If you are using ASIHttpRequest to download large files and cache them, you may run into an issue with request.responseStatusCode value is 0 after upgrading to version 1.8.1.
To fix the issue, check this github issue.
You should replace this line in the original source code:
[self setResponseStatusCode:[[headers objectForKey:@"X-ASIHTTPRequest-Response-Status-Code"] intValue]];

Play videos with MPMoviePlayerController in landscape-only

Today’s HD videos displayed best on 16:9 sized displays, which is available on iOS devices as well in landscape mode.
That’s why it is useful to start 16:9 videos in landscape mode, and even don’t allow to be rotated in portrait mode.
Here is a good tutorial about MPMoviePlayerController usage, with good example, however to start in landscape mode, apply the following lines in your ViewController, which owns the MoviePlayerController’s view:

- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

Ignoring ringer mute switch for audio/video playback

If you wish to let your app playback audio independently from mute switch, check the following tutorial, including sample code.
The most important part of the code:

NSError *_error = nil;
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &_error];

import AVFoundation/AVFoundation.hu and also add the framework with the same name to linking build phase.

iOS 5 NSDateComponents bug

It seems, that there is an issue with iOS5 NSDateComponents NSWeekCalendarUnit property.
It returns one week ahead the current, however it returns the correct number on iOS 4.
You can test it with the following code:
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekComponents = [gregorian components:(NSWeekCalendarUnit) fromDate:[NSDate date];
NSInteger weekNr = [weekComponents week]

Update I have tested NSWeekOfYearCalendarUnit or NSWeekWeekOfMonthCalendarUnit (available in iOS 5 only), and they still return incorrect value, so currently the best solution is to check iOS version and modify the number manually.

Displaying route with iOS Maps application

To easily display a route between two locations with the built-in Maps app (iOS 4 is recommended for multitasking), insert the following code into your app:
NSString* url = [NSString stringWithFormat: @"http://maps.google.com/maps?saddr=Current\%20Location&daddr=%f,%f", destCoordinate.latitude, destCoordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

Source: Stackoverflow.com

Changing background image of UINavigationBar with CALayer

Adding a background image to a navigation bar can be done using categories, however I think it is not too elegant, and difficult to modify in application. (though sometimes it is the only solution)
The following code sets the background image using the layer of navigationbar view:
self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"background.png"].CGImage;

Source: Stackoverflow.com