Category Archives: iPhone

“Real” URL encoding in iOS

Sometimes if you wish to encode a whole URL including /,&,: , [NSString stringByAddingPercentEscapes:] fails, to solve this issue, use CFURLCreateStringByAddingPercentEscapes

+ (NSString*)urlEncode: (NSString*) url {
    NSString* encoded = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)url, NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]",kCFStringEncodingUTF8);
    return [encoded autorelease];


Calculating angle between two GPS positions

Easy way to determine bearing/angle between two locations:

----- CLLocation+Bearing.h


@interface CLLocation (Bearing)

-(double) bearingToLocation:(CLLocation *) destinationLocation;
-(NSString *) compassOrdinalToLocation:(CLLocation *) nwEndPoint;


#import "CLLocation+Bearing.h"

double DegreesToRadians(double degrees) {return degrees * M_PI / 180;};
double RadiansToDegrees(double radians) {return radians * 180/M_PI;};

@implementation CLLocation (Bearing)

-(double) bearingToLocation:(CLLocation *) destinationLocation {

 double lat1 = DegreesToRadians(self.coordinate.latitude);
 double lon1 = DegreesToRadians(self.coordinate.longitude);

 double lat2 = DegreesToRadians(destinationLocation.coordinate.latitude);
 double lon2 = DegreesToRadians(destinationLocation.coordinate.longitude);

 double dLon = lon2 - lon1;

 double y = sin(dLon) * cos(lat2);
 double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
 double radiansBearing = atan2(y, x);

 return RadiansToDegrees(radiansBearing);

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/ and also add the framework with the same name to linking build phase.