Skip to content

Instantly share code, notes, and snippets.

@edwardean
edwardean / CustomURLProtocol.swift
Created April 13, 2017 01:48 — forked from aerickson14/CustomURLProtocol.swift
Custom URLProtocol monitoring URLRequests over a URLSession in Swift 3
import UIKit
class CustomURLProtocol: URLProtocol {
struct Constants {
static let RequestHandledKey = "URLProtocolRequestHandled"
}
var session: URLSession?
var sessionTask: URLSessionDataTask?
@edwardean
edwardean / gist:08460e2e8ae91bdc690048c336963c29
Created April 10, 2017 01:32
Time interval since last reboot.
/**
Returns an accurate version of the system uptime even while the device is asleep.
http://stackoverflow.com/questions/12488481/getting-ios-system-uptime-that-doesnt-pause-when-asleep
- returns: Time interval since last reboot.
*/
public static func systemUptime() -> TimeInterval {
var boottime = timeval()
var mib = [CTL_KERN, KERN_BOOTTIME]
var size = MemoryLayout<timeval>.stride

像strong, retain, copy,block引用循环这些常见的这里不再赘述。

  • 下面代码输出结果
- (void)viewDidLoad {
    [super viewDidLoad];

    dispatch_sync(dispatch_get_global_queue(0, 0), ^{
        
@edwardean
edwardean / gist:11ff3883e6d89d195e3cf1ae03cf0e95
Created March 2, 2017 08:15 — forked from bryanluby/gist:8773920
Resign first responder from anywhere.
// Make sure viewDidAppear has happened before sending this action to ensure that all links
// in the responder chain are connected.
// http://stackoverflow.com/a/11768282/1306872
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder)
to:nil
from:nil
forEvent:nil];
//////////////////////////////////////////////////
@implementation UIView (FirstResponder)
- (UIView *)findFirstResponder {
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView findFirstResponder];
if (firstResponder != nil) {
if [ $CONFIGURATION == Release ]; then
echo "Bumping build number..."
plist=${PROJECT_DIR}/${INFOPLIST_FILE}
# increment the build number (ie 115 to 116)
buildnum=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${plist}")
if [[ "${buildnum}" == "" ]]; then
echo "No build number in $plist"
exit 2
fi
func sumRecursively(numbers: [Int], _ acc:Int = 0) -> Int {
if let head = numbers.first {
let tail = Array(dropFirst(numbers))
return sumRecursively(tail, head + acc)
} else {
return acc
}
}
let myNumbers = [1,2,3,4,5]
@edwardean
edwardean / NSDictionary+Unicode.m
Last active July 21, 2017 02:47
NSDictionary+Unicode(解决NSDictionary乱码)
//
// NSDictionary+Unicode.m
//
// Created by lihang on 2017/2/5.
// Copyright © 2017年 HL. All rights reserved.
//
@implementation NSDictionary (HLUnicode)
+ (void)load {
@edwardean
edwardean / gist:218949afa8439d6f1134af7745f409c9
Created February 4, 2017 10:23
iOS keep running in Background
import Foundation
import AVFoundation
import UIKit
class LHPersistentRunTask : NSObject, AVAudioPlayerDelegate {
var player : AVAudioPlayer?
var backgroundTask = UIBackgroundTaskInvalid
var expirationHandler : (()->Void)?
let label = UILabel()
label.frame = CGRect(x: 0.0, y: 0.0, width: 400.0, height: 75.0)
label.backgroundColor = UIColor.whiteColor()
label.font = UIFont.preferredFontForTextStyle(UIFontTextStyleTitle1)
label.textAlignment = .Center
label.text = "Hello, world!"
let fadeTransition = CATransition()
fadeTransition.duration = 0.2