Skip to content

Instantly share code, notes, and snippets.

View maximbilan's full-sized avatar
🧑‍🚒
Working...

Maksym Bilan maximbilan

🧑‍🚒
Working...
View GitHub Profile
import Foundation
extension String {
func isValidEmail() -> Bool {
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluateWithObject(self)
}
@maximbilan
maximbilan / BasicAuthentication.swift
Created July 2, 2016 18:52
HTTP Basic Authentication using NSURLSession
let login = "test"
let password = "12345"
let url = NSURL(string: "http://test.com/api/v1/example.json")
let request = NSMutableURLRequest(URL: url!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "\(login):\(password)"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
#
# c.f. StackOverflow question/answer here: http://stackoverflow.com/questions/3520977/build-fat-static-library-device-simulator-using-xcode-and-sdk-4
#
# Version 2.5
#
# Latest Change:
# - The "copy headers" section now respects the build setting for the location of the public headers
# - Opens the directory with the universal library after build (Can be annoying)
#
# Purpose:
@maximbilan
maximbilan / CenterViewFlowLayout.swift
Created February 7, 2016 13:41
CenterViewFlowLayout
import UIKit
class CenterViewFlowLayout: UICollectionViewFlowLayout {
override func collectionViewContentSize() -> CGSize {
// Only support single section for now.
// Only support Horizontal scroll
let count = self.collectionView?.dataSource?.collectionView(self.collectionView!, numberOfItemsInSection: 0)
let canvasSize = self.collectionView!.frame.size
var contentSize = canvasSize
@maximbilan
maximbilan / gist:b7a747eb9a56a5d5e54b
Created February 6, 2016 10:50
iCloudIssue.swift
let directories = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true)
if let documentDirectory = directories.first {
do {
let documents = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(documentDirectory)
for files in documents {
let urlForm = NSURL.fileURLWithPath(documentDirectory + "/" + files)
do {
try print("\(files): \(urlForm.resourceValuesForKeys([NSURLIsExcludedFromBackupKey]))")
} catch {
print("can't find key")
@maximbilan
maximbilan / iCloudIssue.m
Created February 6, 2016 10:47
iCloud Issue
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSArray *documents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:basePath error:nil];
NSURL *URL;
NSString *completeFilePath;
for (NSString *file in documents) {
completeFilePath = [NSString stringWithFormat:@"%@/%@", basePath, file];
URL = [NSURL fileURLWithPath:completeFilePath];
NSLog(@"File %@ is excluded from backup %@", file, [URL resourceValuesForKeys:[NSArray arrayWithObject:NSURLIsExcludedFromBackupKey] error:nil]);
}
override func loadView() {
super.loadView()
...
let nibName = someCondition ? "OneXIB" : "AnotherXIB"
let nib = UINib(nibName: nibName, bundle: nil)
nib.instantiateWithOwner(self, options: nil)
}
- (void)loadView
{
[super loadView];
...
NSString *nibName = someCondition ? @"OneXIB" : @"AnotherXIB";
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
[nib instantiateWithOwner:self options:nil];
}
import UIKit
public enum DeviceSpecific {
case iPhone
case iPhoneRetina
case iPhone5
case iPhone6
case iPhone6Plus
case iPad
case iPadRetina
@maximbilan
maximbilan / SKTextureGradient.swift
Last active January 1, 2016 16:15
SKTextureGradient
//
// SKTextureGradient.swift
// Linear gradient texture
// Based on: https://gist.github.com/Tantas/7fc01803d6b559da48d6, https://gist.github.com/craiggrummitt/ad855e358004b5480960
//
// Created by Maxim on 1/1/16.
// Copyright © 2016 Maxim Bilan. All rights reserved.
//
import SpriteKit