Skip to content

Instantly share code, notes, and snippets.

View bryanjclark's full-sized avatar

Bryan Clark bryanjclark

View GitHub Profile
@bryanjclark
bryanjclark / ProvisionalNotifications.swift
Created September 25, 2019 16:40
Provisional notifications in iOS
/// Sets up provisional authorization (as in, "trial notifications")
// see: https://asciiwwdc.com/2018/sessions/710
fileprivate func requestProvisionalAuthorization() {
if #available(iOS 12.0, *) {
let options: UNAuthorizationOptions = [.provisional]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (authorized, error) in
if let error = error {
logger.error(String(describing: error))
}
}
@bryanjclark
bryanjclark / UIAlertController+Blixt.swift
Created September 17, 2019 20:12
UIAlertController + "Not Built Yet" helper
extension UIAlertController {
static func notBuiltYet(title: String? = "Haven't built this yet") -> UIAlertController {
let alertController = UIAlertController(
title: title,
message: "Not built yet. Sorry!",
preferredStyle: .alert
)
alertController.addAction(UIAlertAction.dismiss)
return alertController
}
@bryanjclark
bryanjclark / LocketColorStuff.swift
Created May 9, 2019 15:50
Locket Photo's color-theme management code
// Locket's color-management stuff
// First up, here's the protocol for a "color theme".
// I only have two implemented; they're listed below.
public enum SemanticColor: CaseIterable {
case backgroundColor
case backgroundColorSecondary
case backgroundColorCell
case backgroundColorCellHighlighted
@bryanjclark
bryanjclark / gist:da9cd521103ce8dd25662b233b71c2e9
Created May 24, 2018 21:22
iOS: Clearing out old keychain if app is re-installed
//Clear keychain on first run in case of reinstallation
if (![[NSUserDefaults standardUserDefaults] objectForKey:@"FirstRun"]) {
// Delete values from keychain here
if ([[BXTAccountManager sharedAccountManager] authenticatedAccounts]) {
[[BXTNotificationsManager sharedNotificationsManager] removeAllAccountsFromServer];
}
[[BXTAccountManager sharedAccountManager] clearKeychain];
NSLog(@"Clearing keychain");
[[NSUserDefaults standardUserDefaults] setValue:@"1strun" forKey:@"FirstRun"];
@bryanjclark
bryanjclark / ADNKit_photo_embed
Last active August 29, 2015 13:57
Embedding a photos.app.net object in a new ANKPost (ADNKit on iOS)
UIImage *imageToUpload = ...
ANKPost *post = [ANKPost new];
post.text = @"Hello, world. Here's a photo of you:";
post.entities = [ANKEntities new];
post.entities.parseLinks = YES;
post.entities.parseMarkdownLinks = YES;
//Upload the file
NSData *imageData = UIImageJPEGRepresentation(image, 1.0f);
@bryanjclark
bryanjclark / gist:8391889
Created January 12, 2014 23:00
Here's how I grab the matching #topics in Threadnote, based on the substring of text that's been typed so far.
-(void)showTagTableWithString:(NSString *)partialTagString {
[self.noteTextView setSpellCheckingType:UITextSpellCheckingTypeNo];
//If there isn't any string
if (partialTagString.length == 0) {
//Display all of the tags, sorted by most recently used.
NSFetchRequest *hashtagRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *tagEntityDescription = [NSEntityDescription entityForName:@"Tags"
inManagedObjectContext:self.note.managedObjectContext];
[hashtagRequest setEntity:tagEntityDescription];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateLastUsed"
@bryanjclark
bryanjclark / adn-createStreamAndMonitor
Created November 9, 2013 23:17
I'm trying to build a Python-based push notification process. Eventually, I'll host it on a Mac or on AWS, and send out push notifications through there. Here's my current working copy (I'll update it occasionally) in trying to build that python service.
import time
import json
import requests
from pprint import pprint
# Here's what I want running on the server. For now, I'm running it on my local machine.
# FIRST, WE NEED AN APP TOKEN
appTokenURL = 'https://account.app.net/oauth/access_token'
clientID = 'MY_CLIENT_ID'
@bryanjclark
bryanjclark / Given an NSRange and a UILabel with attributed text, find the CGRect of a substring
Last active July 15, 2021 10:42
My three current contenders for solving a tricky UILabel question: given an NSRange and a UILabel that displays attributed text, can you find the rect of the text in that range? Link to my related Stack Overflow question: http://stackoverflow.com/questions/19417776/how-do-i-locate-the-cgrect-for-a-substring-of-text-in-a-uilabel
- (CGRect)rectForSubstringWithRange:(NSRange)range
{
/* Core Text methods that seem somewhere in the neighborhood of useful:
CTRunGetPositionsPtr
CTRunGetPositions
CTRunGetImageBounds
CTLineGetStringRange
CTLineGetOffsetForStringIndex
CTLineGetImageBounds