Skip to content

Instantly share code, notes, and snippets.

View priore's full-sized avatar

Priore priore

View GitHub Profile
@priore
priore / convproperty.m
Last active February 28, 2016 02:54
Convert property names in a text string with their value
// 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:@""];
@priore
priore / uniqueid.m
Last active February 28, 2016 02:55
Generate a unique id of a NSObject
#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);
@priore
priore / screenbounds.m
Last active February 28, 2016 02:56
iOS8: correct screen bounds
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;
}
}
@priore
priore / gist:d45ffd2e9ea02c0d2f6e
Created November 12, 2014 11:22
How to check the end of an audio (AVPlayer)
// 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);
@priore
priore / gist:f5b5c3ebf4a3544382e2
Created November 12, 2014 11:22
How to get duration (HH:MM:SS) from AVAudioPlayer
// 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]);
@priore
priore / gist:9c24dad864c70ebccfc3
Created November 12, 2014 11:21
fix the wrong value of sizeThatFits in a UITableViewCell
// 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
@priore
priore / gist:b91f6647c7fcca3b67cc
Created November 12, 2014 11:21
Get list of all interfaces on the iPhone - iPad Device
#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] ;
@priore
priore / gist:bb6f9cd11b1591552208
Created November 12, 2014 11:20
Settings from a PLIST file
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];
@priore
priore / gist:4725304d15d417d42884
Created November 12, 2014 11:20
Loading the XIB dependent on the device
// 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:@""];
@priore
priore / gist:14ced317638f694afacd
Created November 12, 2014 11:18
NSObject set property values with NSDictionary
// 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];
}];
}