This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Convert property names in a text string with their value | |
| object = user defined object | |
| // your text below with the property names enclosed in square brackets | |
| NSString *result = @"Your text here with macros eg. [name], [value], [description]"; | |
| NSRegularExpression *regEx = [NSRegularExpression regularExpressionWithPattern:@"\\[(.*?)\\]" options:0 error:NULL]; | |
| NSArray *matches = [regEx matchesInString:result options:0 range:NSMakeRange(0, [text length])]; | |
| if (matches) { | |
| for (NSTextCheckingResult *match in matches) { | |
| NSString *macro = [[[text substringWithRange:match.range] stringByReplacingOccurrencesOfString:@"[" withString:@""] stringByReplacingOccurrencesOfString:@"]" withString:@""]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import <CommonCrypto/CommonDigest.h> | |
| @implementation NSObject (Utility) | |
| - (NSString*)uniqueID | |
| { | |
| // nsobject --> nsdata -- > md5 hash --> hex string (30 chars) | |
| NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self]; | |
| unsigned char result[CC_MD5_DIGEST_LENGTH]; | |
| CC_MD5([data bytes], (uint32_t)[data length], result); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static inline CGSize screenSize() | |
| { | |
| CGSize screenSize = [UIScreen mainScreen].bounds.size; | |
| if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(getOrientation())) { | |
| return CGSizeMake(screenSize.height, screenSize.width); | |
| } else { | |
| return screenSize; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // How to check the end of an audio | |
| AVAsset *asset = [AVURLAsset URLAssetWithURL:your-url options:nil]; | |
| AVPlayerItem *anItem = [AVPlayerItem playerItemWithAsset:asset]; | |
| self.player = [AVPlayer playerWithPlayerItem:anItem]; | |
| __weak typeof(self) wself = self; | |
| CMTime interval = CMTimeMake(1, 1); | |
| id observer = [self.soundPlayer addPeriodicTimeObserverForInterval:interval queue:dispatch_get_main_queue() usingBlock:^(CMTime time) { | |
| // controlla la fine dell'audio | |
| CMTime duration = CMTimeConvertScale(wself.player.currentItem.duration, wself.player.currentTime.timescale, kCMTimeRoundingMethod_Default); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // How to get duration (HH:MM:SS) from AVAudioPlayer | |
| AVAudioPlayer *audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:@"you-url-sreaming"] error:nil]; | |
| [audioPlayer prepareToPlay]; | |
| CMTime duration = [[[audioPlayer currentItem] asset] duration]; | |
| float seconds = CMTimeGetSeconds(duration); | |
| NSLog("Duration : %@", [NSString stringWithFormat:@"%02d:%02d:%02d", (int)((int)(seconds)) / 3600, (int)((int)(seconds)) / 60, (int)((int)(seconds)) % 60]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // fix the wrong value of sizeThatFits in a UITableViewCell for iOS8 | |
| // UITableViewCell+iOS8.h | |
| #import <UIKit/UIKit.h> | |
| @interface UITableViewCell (iOS8) | |
| - (CGSize)sizeThatFitsiOS8:(CGSize)size; | |
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <sys/socket.h> | |
| #include <arpa/inet.h> | |
| #include <netdb.h> | |
| #include <sys/types.h> | |
| #include <ifaddrs.h> | |
| #include <net/if.h> | |
| - (NSArray *)localIPAddresses | |
| { | |
| NSMutableArray *ipAddresses = [NSMutableArray array] ; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static NSDictionary *settings; | |
| + (id)settingsValueForKeyPath:(NSString*)keyPath | |
| { | |
| if (settings == nil) { | |
| NSString *plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"settings.plist"]; | |
| settings = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; | |
| } | |
| return [settings valueForKeyPath:keyPath]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Loading the XIB dependent on the device | |
| // | |
| // 1. create your viewcontroller's named MyViewController~ipad.xib and MyViewController~iphone.xib | |
| // | |
| // 2. inizialize your class with MyViewController myvc = [[MyViewController alloc] initWithDefaultNib]; | |
| // | |
| - (id)initWithDefaultNib | |
| { | |
| NSString *nibName =[[[[[NSBundle mainBundle] pathForResource: NSStringFromClass([self class]) ofType:@"nib"] componentsSeparatedByString:@"/"] lastObject] stringByReplacingOccurrencesOfString:@".nib" withString:@""]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // NSObject set property values with NSDictionary | |
| - (void)setValuesWithDictionary:(NSDictionary *)dict | |
| { | |
| if (dict != nil) { | |
| [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { | |
| NSString *setPropName = [NSString stringWithFormat:@"set%@%@:", [[key substringWithRange:(NSRange){0, 1}] uppercaseString], [key substringFromIndex:1]]; | |
| if ([self respondsToSelector:NSSelectorFromString(setPropName)]) | |
| [self setValue:obj forKey:key]; | |
| }]; | |
| } |