Skip to content

Instantly share code, notes, and snippets.

View Thunderbird7's full-sized avatar
🏠
Working from home

Yuttana K. Thunderbird7

🏠
Working from home
View GitHub Profile
// This function creates the required URLRequestConvertible and NSData we need to use Alamofire.upload
func urlRequestComponentsWithParams(urlString:String, parameter: [String: AnyObject], imageData:[NSData], inputName: String) -> (URLRequestConvertible, NSData) {
// create url request to send
let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
mutableURLRequest.HTTPMethod = Alamofire.Method.PUT.rawValue
let boundaryConstant = "FileUploader-boundary-\(arc4random())-\(arc4random())";
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
// ARN Endpoint
// For Amazon SNS
NSString *myARN = @"arn:aws:sns:ap-southeast-1:570761336625:app/APNS_SANDBOX/Airportthai";
AWSSNSCreatePlatformEndpointInput *platformEndpointRequest = [AWSSNSCreatePlatformEndpointInput new];
[platformEndpointRequest setCustomUserData:@"custom user data เช่นระบบ iOS version, Device Version อะไรก็ได้"];
[platformEndpointRequest setToken:[self deviceTokenAsString:deviceToken]]; //แปลง device token ให้เป็น string ก่อนนะ
[platformEndpointRequest setPlatformApplicationArn:myARN];
// AWS SNS Config
// Credential
AWSStaticCredentialsProvider *credential = [[AWSStaticCredentialsProvider alloc] initWithAccessKey:@"YOUR_AMAZON_ACCESSKEY"
secretKey:@"YOUR_AMAZON_SECERT"];
// Config region
AWSServiceConfiguration *configuration = [[AWSServiceConfiguration alloc] initWithRegion:AWSRegionAPSoutheast1
credentialsProvider:credential];
[AWSServiceManager defaultServiceManager].defaultServiceConfiguration = configuration;
@Thunderbird7
Thunderbird7 / jsondecode.swift
Last active December 29, 2015 11:44
Deserialize json string UTF8 encoding
//: Playground - noun: a place where people can play
import UIKit
let jsonString = "{\"username\" : \"thunderbird\", \"password\" : \"tb00110\"}"
func jsonDecodeString(json: String) -> Dictionary<String, AnyObject>? {
let json:NSData = jsonString.dataUsingEncoding(NSUTF8StringEncoding)!
do {
return try NSJSONSerialization.JSONObjectWithData(json, options: NSJSONReadingOptions.MutableContainers) as? Dictionary<String, AnyObject>
@Thunderbird7
Thunderbird7 / TestURLConnection.m
Last active November 24, 2015 17:43
Test case example to test response of an asynchronous networking request with XCTestExpectation APIs
- (void)testAsynchronousURLConnection {
// Create request
NSURL *URL = [NSURL URLWithString:@"http://httpbin.org/get"];
NSString *description = [NSString stringWithFormat:@"GET %@", URL];
XCTestExpectation *expectation = [self expectationWithDescription:description];
NSURLSession *session = [NSURLSession sharedSession];
@Thunderbird7
Thunderbird7 / AbbreviateNumber.swift
Last active December 27, 2018 15:54
[Swift] Abbreviate number class, such as number of 1500 convert to 1.5k
func abbreviateNumber(num: NSNumber) -> String {
// less than 1000, no abbreviation
if num < 1000 {
return "\(num)"
}
// less than 1 million, abbreviate to thousands
if num < 1000000 {
var n = Double(num);
n = Double( floor(n/100)/10 )