Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mattt on github.
  • I am mattt (https://keybase.io/mattt) on keybase.
  • I have a public key whose fingerprint is 58FC 9E3A 40F3 5F72 E402 F6C0 2755 0012 282D EB01

To claim this, I am signing this object:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:d="http://www.apple.com/DTDs/DictionaryService-1.0.rng">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<d:entry id="m_en_gb0035330" d:title="apple" class="entry">
<span class="hg">
<span d:dhw="1" role="text" linebreaks="apple" class="hw">apple</span>
@mattt
mattt / UTTypeForImageData.m
Created March 27, 2014 23:19
A quick function for determining an image's file type by its first couple of bytes
@import MobileCoreServices;
static CFStringRef UTTypeForImageData(NSData *data) {
const unsigned char * bytes = [data bytes];
if (data.length >= 8) {
if (bytes[0] == 0x89 && bytes[1] == 0x50 && bytes[2] == 0x4E && bytes[3] == 0x47 && bytes[4] == 0x0D && bytes[5] == 0x0A && bytes[6] == 0x1A && bytes[7] == 0x0A) {
return kUTTypePNG;
}
}

A number of tech news outlets, including WIRED, GigaOm, and MIT Technology Review, have recently started writing about Multipeer Connectivity ("one weird trick that the NSA hates"). Since the NSHipster article on the subject has been linked to in a lot of this coverage, I wanted to share some additional thoughts on the matter:

Multipeer Connectivity(http://nshipster.com/multipeer-connectivity/) represents a significant shift in the opposite direction of how we conventionally think about mobile applications. Nearly every app on your phone operates in a client-server model, with the device making requests to remote cloud services to send and receive messages, photos, and videos. The [

@mattt
mattt / BCP47LanguageCodeForString.m
Last active April 21, 2018 08:45
Detect ISO 681 Language Code from String and Convert to BCP 47 Language Code
static NSString * BCP47LanguageCodeFromISO681LanguageCode(NSString *ISO681LanguageCode) {
if ([ISO681LanguageCode isEqualToString:@"ar"]) {
return @"ar-SA";
} else if ([ISO681LanguageCode hasPrefix:@"cs"]) {
return @"cs-CZ";
} else if ([ISO681LanguageCode hasPrefix:@"da"]) {
return @"da-DK";
} else if ([ISO681LanguageCode hasPrefix:@"de"]) {
return @"de-DE";
} else if ([ISO681LanguageCode hasPrefix:@"el"]) {
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:@"user" password:@"Pa55w0rd"]
NSURL *filePath = [NSURL fileURLWithPath:@"file://path/to/image.png"];
[manager POST:@"http://example.com/resources.json" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePath name:@"image" error:nil];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success: %@", responseObject);
static NSString * const CCErrorDomain = @"com.apple.commoncrypto.error";
static NSString * CCDescriptionForStatus(CCCryptorStatus status) {
static NSString * const _CCStatusDescriptions[] = {
[-kCCSuccess] = @"Operation completed normally.",
[-kCCParamError] = @"Illegal parameter value.",
[-kCCBufferTooSmall] = @"Insufficent buffer provided for specified operation.",
[-kCCMemoryFailure] = @"Memory allocation failure.",
[-kCCAlignmentError] = @"Input size was not aligned properly.",
[-kCCDecodeError] = @"Input data did not decode or decrypt properly.",
require 'open-uri'
require 'nokogiri'
year = 2014
doc = Nokogiri::HTML(open("https://developer.apple.com/videos/wwdc/#{year}/"))
doc.search("p.download").each do |download|
href = download.at("a").attr('href')
uri = URI(href)
uri.path = uri.path.split('/')[0...-1].join('/') + '/'
// See: https://devforums.apple.com/message/1000934#1000934
import Foundation
// Logic
operator prefix ¬ {}
@prefix func ¬ (value: Bool) -> Bool {
return !value
}
postfix operator ‽ {}
postfix func ‽ (value: Bool) -> Bool {
return arc4random_uniform(2) == 0
}
prefix operator ⸘ {}
prefix func ⸘ (value: Bool) -> Bool {
return value‽
}