Skip to content

Instantly share code, notes, and snippets.

View priore's full-sized avatar

Prioregroup.com priore

View GitHub Profile
@priore
priore / NSDictionary+Values.m
Last active August 29, 2015 14:01
Fixes crash in case of missing of the partial KeyPath in NSDictionary
@implementation NSDictionary (Values)
- (id)valueForKeyPath:(NSString *)keyPath
{
__block id value = nil;
if (keyPath != nil) {
NSArray *keys = [keyPath componentsSeparatedByString:@"."];
if (keys && [keys count] > 0) {
[keys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
@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:1daa138d38f8cee0ff29
Created November 12, 2014 11:15
Tips: a solid dealloc for all classes
- (void)dealloc
{
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[[NSNotificationCenter defaultCenter] removeObserver:self];
if ([self respondsToSelector:@selector(setDelegate:)])
[self performSelector:@selector(setDelegate:) withObject:nil];
[super dealloc]; // not needed in ARC
}
@priore
priore / gist:eb8ebee056375d7f8a0e
Created November 12, 2014 11:14
Fix the crash in iOS6 when you start a not supported video stream
// Fix the crash in iOS6 when you start a not supported video stream
// note: do not use initWithUrl, instead use setContentURL
MPMoviePlayerViewController viewController = [[MPMoviePlayerViewController alloc] init];
viewController.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
viewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[viewController.moviePlayer setContentURL:url];
@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: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: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: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];
}];
}
@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: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