This file contains 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
@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) { |
This file contains 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 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
- (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 | |
} |
This file contains 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 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]; |
This file contains 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 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 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 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]; | |
}]; | |
} |
This file contains 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 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 |
OlderNewer