iOS8: How to clear NSUserDefaults on simulator

There is a terrible behavior in the iOS8 Simulator. When you uninstall an app on the iOS8 simulator, the NSUserDefaults is not deleted.

You must to remove it manually. I hope that this bug will be solved with the new version of iOS8.x.

So, delete the application from simulator and then delete the plist file from

Macintosh HD ▸ Users ▸ username ▸ Library ▸ Developer ▸ CoreSimulator ▸ Devices ▸ SIMULATORUUID ▸ data ▸ Library ▸ Preferences ▸ YourApplicationBundleID.plist

Into the Devices folder, you can check the deviceType in the device.plist file, to check the right simulator folder.

How to resolve pod error in Yosemite OS X 10.10

With the new OS X 10.10, aka Yosemite, rubygems works differently. So, if you try the command “pod init” you will obtain a error like this below:

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:126:in `require': cannot load such file -- xcodeproj/prebuilt/universal.x86_64-darwin14-2.0.0/xcodeproj_ext...

Just resolve the error following those steps:

  1. Open Xcode 6
  2. Open Preferences
  3. Click the Locations tab
  4. Change the Command Line Tools version to last Xcode 6.x
  5. Uninstall all Cocoapods (you can not remove executables)
    $ sudo gem uninstall cocoapods
  6. Install xcodeproj
    $ sudo gem install xcodeproj
  7. Install Cocoapods
    $ sudo gem install cocoapods
  8. Run pod —version to verify that it worked

That’s all!

Touchable text in NSMutableAttributedString

Do you want some text clickable in your text string? That’s easy using iOS7/8, NSMutableAttributedString and the UITextView control!

Touchable text in NSMutableAttributedString - Daniele Galiotto - www.g8production.com

1- Set text into the UITextView. Clickable texts must be a custom attribute;

//ViewDidLoad
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
    paragraph.alignment = NSTextAlignmentJustified;
    paragraph.lineSpacing = 3;
    
    NSMutableAttributedString* agreeAttributedString = [[NSMutableAttributedString alloc] initWithString:@"You confirm you read our {0} and our {1}" attributes:@{ NSForegroundColorAttributeName : [UIColor blackColor]}];
    
    NSAttributedString* termsAttributedString = [[NSAttributedString alloc] initWithString:@"Terms"
             attributes:@{
             @"termsTag" : @(YES),
             NSForegroundColorAttributeName : [UIColor redColor]}];
    
    NSAttributedString* policyAttributedString = [[NSAttributedString alloc] initWithString:localize:@"Policy"
               attributes:@{
               @"policyTag" : @(YES),
               NSForegroundColorAttributeName : [UIColor greenColor]}];
    
    NSRange range0 = [[agreeAttributedString string] rangeOfString:@"{0}"];
    if(range0.location != NSNotFound)
        [agreeAttributedString replaceCharactersInRange:range0 withAttributedString:termsAttributedString];
    
    NSRange range1 = [[agreeAttributedString string] rangeOfString:@"{1}"];
    if(range1.location != NSNotFound)
        [agreeAttributedString replaceCharactersInRange:range1 withAttributedString:policyAttributedString];
    
    [agreeAttributedString addAttribute:NSParagraphStyleAttributeName value:paragraph range:NSMakeRange(0, [agreeAttributedString length])];
    
    self.txvText.attributedText = agreeAttributedString;

2- Add gesture tap to the UITextView

//ViewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(txvTextTouched:)];
    [self.txvText addGestureRecognizer:tap];

3- Recognize the tap, find the character that’s been tapped and check its attributes 

    
-(void)txvTextTouched:(UITapGestureRecognizer *)recognizer
{
    UITextView *textView = (UITextView *)recognizer.view;
        
    NSLayoutManager *layoutManager = textView.layoutManager;
    CGPoint location = [recognizer locationInView:textView];
    location.x -= textView.textContainerInset.left;
    location.y -= textView.textContainerInset.top;
        
    NSUInteger characterIndex;
    characterIndex = [layoutManager characterIndexForPoint:location
                                           inTextContainer:textView.textContainer
                  fractionOfDistanceBetweenInsertionPoints:NULL];
    
    if (characterIndex < textView.textStorage.length) {
        
        NSRange range0;
        NSRange range1;
        id termsValue = [textView.textStorage attribute:@"termsTag" atIndex:characterIndex effectiveRange:&range0];
        id policyValue = [textView.textStorage attribute:@"policyTag" atIndex:characterIndex effectiveRange:&range1];
        
        if(termsValue)
        {
            NSLog(@"TERMS TAPPED");
            return;
        }
        
        if(policyValue)
        {
            NSLog(@"POLICY TAPPED");
            return;
        }
    }
}

 That’s all!

        NSLog(@"%@, %lu, %lu", termsValue, (unsigned long)range0.location, (unsigned long)range0.length);

NSFileManager documents & caches extension

iosswift:

Here’s a handy little NSFileManager extension that gives you easy access to the documents and caches directories for your app…

import Foundation

extension NSFileManager {
	class func documentsDir() -> String {
		var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true) as [String]
		return paths[0]
	}
	
	class func cachesDir() -> String {
		var paths = NSSearchPathForDirectoriesInDomains(.CachesDirectory, .UserDomainMask, true) as [String]
		return paths[0]
	}
}