Pages

Reverting all Xcode settings to default settings

This is the officially recommended way to delta Xcode 5's preferences, type in Terminal.app:

defaults delete com.apple.dt.Xcode
 
That should restore Xcode to the state of its first launch.
(for older versions of Xcode the command was defaults delete com.apple.Xcode, i.e. without the dt in the middle).

Creating Custom URL schemes in iOS/iPhone

iPhone apps can also specify their own custom URL scheme (for example, myapp://doStuff). When might you want to use a custom URL scheme for your app?
  • To transfer data from your app to your  another app
  • To allow other apps (or even web pages) to call your app (and send data to it)
  • To handle callbacks for custom authentication (such as OAuth) and third party API's

Implementing a Custom URL Scheme

 

The first step is to create a custom URL scheme – start by locating and clicking on the project info.plist in the Xcode Project Navigator. With the plist displayed in the right pane, right click on the list and select Add Row:
From the list presented scroll down and select URL types.



 Select URL Types for the new item. Once that's added, click the grey arrow next to "URL Types" to show "Item 0". Set your URL identifier to a unique string - something like com.yourcompany.yourappname. 
 
After you've set the URL identifier, select that line and click the "+" sign again, and add a new item for URL Schemes. Then click the grey arrow next to "URL Schemes" to reveal "Item 0". Set the value for Item 0 to be your URL scheme name. 


 

 

Handling Custom URL Calls

In order for your app to respond when it receives a custom URL call, you must implement the application:openURL:sourceApplication:annotation: method in the application delegate class:

 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{

// handler code here
}

Parsing the Custom URL

There are several parts to a URL:
scheme://host/path?query
The parts to the URL can be retrieved through the NSURL object that is passed into the application:openURL:sourceApplication:annotation: method. If you have a fairly simple URL naming scheme and want to allow access to specific pages/keys, you can just use the host name:

 

 To pass data into your app, you'll want to use the query string. Here's a simple method for parsing the query string from the url:
- (NSDictionary *)parseQueryString:(NSString *)query {
    NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithCapacity:6] autorelease];
    NSArray *pairs = [query componentsSeparatedByString:@"&"];
    
    for (NSString *pair in pairs) {
        NSArray *elements = [pair componentsSeparatedByString:@"="];
        NSString *key = [[elements objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSString *val = [[elements objectAtIndex:1] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        [dict setObject:val forKey:key];
    }
    return dict;
}
 

Testing The Custom URL

You can easily test your URL scheme in the simulator. Just add a test button to one of your views, and implement the IBAction method for it as follows:

- (IBAction)getTest:(id)sender {
     NSString *escapedDataString = [@"any data you want to parse" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
     NSURL *quotescreator = [NSURL URLWithString:[NSString stringWithFormat:@"quotescreator://sendtext?quote=%@",escapedDataString]];
                                    
     if ([[UIApplication sharedApplication] canOpenURL:quotescreator]) {
                                       
            [[UIApplication sharedApplication] openURL:quotescreator];
       } else {
                                        
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://itunes.apple.com/app/id895331100"]];
      }
}

 Then in your app delegate, implement the application:openURL:sourceApplication:annotation: method:

 - (BOOL)application:(UIApplication *)application
             openURL:(NSURL *)url
   sourceApplication:(NSString *)sourceApplication
          annotation:(id)annotation {
 
    NSLog(@"url recieved: %@", url);
    NSLog(@"query string: %@", [url query]);
    NSLog(@"host: %@", [url host]);
    NSLog(@"url path: %@", [url path]);
    NSDictionary *dict = [self parseQueryString:[url query]];
    NSLog(@"query dict: %@", dict);
    return YES;
}




Add shadow to text in UITextView


    NSShadow * shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor blackColor];
    shadow.shadowOffset = CGSizeMake(1, 2);
  
    NSDictionary * textAttributes =  @{
                                       NSForegroundColorAttributeName : [UIColor blueColor],
                                       NSShadowAttributeName          : shadow,
                                       NSFontAttributeName            : [UIFont boldSystemFontOfSize:20]
                                       };
  
    txtView.attributedText = [[NSAttributedString alloc] initWithString:@"Go to Heaven for the climate, Hell for the company." attributes:textAttributes];



Disable iPhoto auto launch when connecting an iPhone / iPad

  1. Plug in your iPhone/iPad
  2. Open application "Image capture"
  3. Select your iPhone/iPad device
  4. Press the triangle in square symbol in the lower left corner.
  5. Choose "No application" in the drop-list under "Connecting this iPhone opens:"
 

add small image after text in UILabel

NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
attachment.image = [UIImage imageNamed:@"myimage.png"];

NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:attachment];

NSMutableAttributedString *myString= [[NSMutableAttributedString alloc] initWithString:@"My label text"];
[myString appendAttributedString:attachmentString];

myLabel.attributedText = myString;