Skip to content

Instantly share code, notes, and snippets.

@yycking
yycking / NS_ENUM_STRING.m
Created December 1, 2016 08:35
NS_ENUM 支援字串
#define LIST_OF_ServerAPI \
/*會員*/ \
api(登入, member/login) \
api(FB登入, member/fbLogin)
typedef NS_ENUM(NSInteger, ServerAPI) {
#define api(key, name) key,
LIST_OF_ServerAPI
#undef api
};
@yycking
yycking / NSLocalizedString+Base.m
Last active December 1, 2016 09:06
NSLocalizedString auto load Base.lproj if key not found
#ifndef L
#define L(key) [NSBundle.mainBundle localizedStringForKey:(key)]
#endif
@interface NSBundle (NSLocalizedString_Base)
- (NSString *)localizedStringForKey:(NSString *)key;
@end
@implementation NSBundle (NSLocalizedString_Base)
@yycking
yycking / plcrashReporter_setCrashCallbacks.swift
Created December 12, 2016 10:16
crashReporter.setCrashCallbacks on swift
func crashReporterScreenCapture(info:UnsafeMutablePointer<siginfo_t>, uap:UnsafeMutablePointer<ucontext_t>, context:UnsafeMutablePointer<Void>) {
}
var crashReporterCapture = PLCrashReporterCallbacks()
crashReporterCapture.version = 0
crashReporterCapture.context = nil
crashReporterCapture.handleSignal = crashReporterScreenCapture
crashReporter.setCrashCallbacks(&crashReporterCapture)
@yycking
yycking / UILabel.m
Created January 4, 2017 03:28
將UILabel中的圖字置中
UILabel *label = [[UILabel alloc] initWithFrame:tableView.bounds];
label.numberOfLines = 0;
NSMutableAttributedString *attachmentString = [NSMutableAttributedString new];
NSTextAttachment *imageAttachment = [NSTextAttachment new];
imageAttachment.image = [UIImage imageNamed:images[index]];
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:imageAttachment];
[attachmentString appendAttributedString:imageString];
@yycking
yycking / self-signed-SSL.swift
Created January 23, 2017 10:26
accept a self-signed SSL certificate
import UIKit
class NSURLSessionPinningDelegate: NSObject, URLSessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Swift.Void) {
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
if let trust = challenge.protectionSpace.serverTrust,
let pem = Bundle.main.path(forResource: "https", ofType: "cer"),
let data = NSData(contentsOfFile: pem),
@yycking
yycking / Data+=.swift
Created February 15, 2017 06:02
+ and += operator for Data in swift
public protocol DataConvertible {
static func + (lhs: Data, rhs: Self) -> Data
static func += (lhs: inout Data, rhs: Self)
}
extension DataConvertible {
public static func + (lhs: Data, rhs: Self) -> Data {
var value = rhs
let data = Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
return lhs + data
@yycking
yycking / facebook-bot.php
Created February 17, 2017 05:24
Simple Facebook Chat Bot for PHP
<?php
ob_start();
function send($data) {
$token = file_get_contents('.token');
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token='.$token;
$content = json_encode($data);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
@yycking
yycking / pipe-operator.swift
Created February 22, 2017 10:25
pay respect to pipe-forward (|>) operator and Then(Super sweet syntactic sugar for Swift initializers.)
infix operator |>: AdditionPrecedence
public func |> <T,U>(lhs: T, rhs: (T) -> U) -> U {
return rhs(lhs)
}
public func |> <T>(lhs: T, rhs: inout T) {
rhs = lhs
}
infix operator <|: AdditionPrecedence
@yycking
yycking / UILabel+Link.m
Last active December 9, 2019 01:55
Make a clickable link in an UILabel
@yycking
yycking / UIBarButtonSystemItem+image.swift
Created September 22, 2017 08:15
Get UIBarButtonItem icon
extension UIBarButtonSystemItem {
func image() -> UIImage? {
let tempItem = UIBarButtonItem(barButtonSystemItem: self,
target: nil,
action: nil)
// add to toolbar and render it
let bar = UIToolbar()
bar.setItems([tempItem],
animated: false)