Skip to content

Instantly share code, notes, and snippets.

View priore's full-sized avatar

Priore priore

View GitHub Profile
@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 / 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 / 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 / devicetype.m
Last active February 28, 2016 02:54
Detect the type of iPhone Device independent from iOS version
// Detect the type of iPhone Device independent from iOS version
CGSize size = [UIScreen mainScreen].bounds.size;
CGFloat r = size.width > size.height ? size.width / size.height : size.height / size.width;
if (r < 1.5) {
// iPad
} else if (r == 1.5) {
// iPhone 4
} else if (r > 1.5) {
// iPhone 5/6/6+
}
@priore
priore / avplayerstall.m
Last active February 28, 2016 02:53
AVPlayer verify streaming when stalled
@interface MYPlayer : AVPlayer
{
id playerObserver;
}
- (void)play;
- (void)stop;
- (void)pause;
@end
@priore
priore / validvideourl.m
Last active February 28, 2016 02:53
Check valid MPMoviePlayerController video URL
- (void)validVideoURL:(NSString*)url valid:(void(^)())valid invalid:(void(^)())invalid
{
// AFNetworking https://github.com/AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/vnd.apple.mpegurl", @"video/mp2t",
@"video/mov", @"video/mpv", @"video/3gp", @"video/mp4", nil];
NSMutableURLRequest *request = [manager.requestSerializer requestWithMethod:@"HEAD" URLString:url parameters:nil error:nil];
AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
@priore
priore / 3DESClass.cs
Last active February 28, 2016 02:52
3DES Encrypt/Decrypt
//
// https://github.com/priore/SOAPEngine/blob/master/CSHARP/3DESClass.cs
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
@priore
priore / AES256Class.cs
Last active February 28, 2016 02:52
AES256 Encrypt/Decrypt
//
// https://github.com/priore/SOAPEngine/blob/master/CSHARP/AES256Class.cs
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.IO;
using System.Text;
@priore
priore / routerip.m
Last active August 25, 2016 04:20
How to get router IP address in Objective-C
#include <stdio.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <string.h>
#include <net/route.h>
#include <arpa/inet.h>
#define CTL_NET 4 /* network, see socket.h */
@priore
priore / nslc.m
Last active February 28, 2016 02:51
Change value of existing NSLayoutConstraints
- (void)setConstraintValue:(CGFloat)value forAttribute:(NSLayoutAttribute)attribute animation:(BOOL)animation
{
NSArray *constraints = self.view.constraints;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"firstAttribute = %d", attribute];
NSArray *filteredArray = [constraints filteredArrayUsingPredicate:predicate];
NSLayoutConstraint *constraint = [filteredArray firstObject];
if (constraint.constant != value) {
[self.view removeConstraint:constraint];
constraint.constant = value;
[self.view addConstraint:constraint];