Skip to content

Instantly share code, notes, and snippets.

View software-mariodiana's full-sized avatar

Mario Diana software-mariodiana

View GitHub Profile
@software-mariodiana
software-mariodiana / UIImageFixIOS13Bug.m
Last active June 25, 2021 00:29
We swizzle in a fix for the iOS 13 RouteMe/Mapbox (legacy) tiling bug via a category on UIImage.
#import "UIImage+FixIOS13Bug.h"
#import <objc/runtime.h>
/**
* Fix UIImage -drawRect: bug in iOS 13 that impacts RouteMe library tiling.
*
* SEE: https://forums.developer.apple.com/thread/120526
*/
@implementation UIImage (FixIOS13Bug)
UIGraphicsBeginImageContext(CGSizeMake(512.0, 512.0));
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(ctx, [UIColor redColor].CGColor);
CGContextFillRect(ctx, CGRectMake(0.0, 0.0, 512.0, 512.0));
UIImage* myImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
class Cat {
func sayHello() -> String {
return "Meow!"
}
}
func schrodingersCat() -> Cat? {
// Open the box!
let cat = [0, 1].randomElement()
@software-mariodiana
software-mariodiana / AutoLayoutScrollView.m
Last active March 28, 2019 06:14
Example of setting up a UIScrollView programmatically in Objective-C, using Auto Layout. We put a UIView inside the scroll view as its content view, and add subviews to the content view. The scroll view scrolls vertically. (See Apple Technical Note: TN2154.)
- (void)viewDidLoad
{
[super viewDidLoad];
// See TN2154: https://developer.apple.com/library/archive/technotes/tn2154/_index.html
UIScrollView* scrollView = [[UIScrollView alloc] init];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[[self view] addSubview:scrollView];
// Pin the scroll view to the main view.
[[scrollView topAnchor] constraintEqualToAnchor:[[[self view] safeAreaLayoutGuide] topAnchor]].active = YES;
@software-mariodiana
software-mariodiana / detect_notch.m
Last active June 10, 2022 00:23
Objective-C method to detect whether the iOS device has a "notch," à la iPhone X.
#import <LocalAuthentication/LocalAuthentication.h>
/**
* Return YES if iOS device has a notch; NO, otherwise.
*/
- (BOOL)hasDeviceNotch
{
if ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)) {
return NO;
}
@software-mariodiana
software-mariodiana / AddHeaderToAFNewtorking.m
Created February 4, 2019 20:52
How to add an HTTP header field to AFNetworking (v2.4.1)
- (IBAction)sendGETRequest:(id)sender
{
AFHTTPSessionManager* manager =
[[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://localhost:8080"]];
AFHTTPRequestSerializer* requestSerializer = [AFHTTPRequestSerializer serializer];
// Simulate the JWT.
[requestSerializer setValue:@"Bearer abc123doremiabc123babyuandme" forHTTPHeaderField:@"Authorization"];
@software-mariodiana
software-mariodiana / sha256.m
Last active January 2, 2019 18:24
How to hash plaintext.
#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonDigest.h>
NSString* const MDXHashSalt = @"usesomekindofsecret";
@interface MDXHash : NSObject
- (NSString *)createHash:(NSString *)plaintext;
@end
@software-mariodiana
software-mariodiana / BlockObject.m
Last active October 31, 2018 13:46
In Objective-C, a block is an object and can be stored in a collection. A bit of casting is needed when you retrieve the object, and a typedef makes this easier.
#import <Foundation/Foundation.h>
typedef void (^SampleBlock)(void);
int main(int argc, char *argv[]) {
@autoreleasepool {
SampleBlock myBlock = ^void(void) {
NSLog(@"Hello, World!");
};
@software-mariodiana
software-mariodiana / ItemStore.m
Last active September 1, 2018 00:59
Wrap an NSMutableArray, extending it to implement KVC compliance for a "count" property.
/* Header: ItemStore.h
#import <Foundation/Foundation.h>
@interface ItemStore : NSObject <NSFastEnumeration>
@property (nonatomic, assign, readonly) NSUInteger count;
@end
*/
#import "ItemStore.h"
@software-mariodiana
software-mariodiana / chomp.c
Created June 21, 2018 01:26
Demonstrate Perl's chomp function in C.
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
/**
* Truncate string at first newline, if exists, returning true on chomp; false, if no chomp.
*/
bool chomp(char *line) {
// https://stackoverflow.com/a/3217634/155167
bool success = false;