Skip to content

Instantly share code, notes, and snippets.

View maximbilan's full-sized avatar
🚀
Fixing what I broke yesterday

Maksym Bilan maximbilan

🚀
Fixing what I broke yesterday
View GitHub Profile
override func loadView() {
super.loadView()
...
let nibName = someCondition ? "OneXIB" : "AnotherXIB"
let nib = UINib(nibName: nibName, bundle: nil)
nib.instantiateWithOwner(self, options: nil)
}
@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]);
}
@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 / 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
#
# 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 / 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))
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 / ConnectionCheck.swift
Created July 16, 2016 09:21
Swift Quick Internet Availability Check
import SystemConfiguration
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in()
zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
@maximbilan
maximbilan / NSLayoutConstraintMultiplierExtension.swift
Created July 21, 2016 12:51
NSLayoutConstraint Multiplier Extension
extension NSLayoutConstraint {
func setMultiplier(multiplier:CGFloat) -> NSLayoutConstraint {
let newConstraint = NSLayoutConstraint(
item: firstItem,
attribute: firstAttribute,
relatedBy: relation,
toItem: secondItem,
attribute: secondAttribute,
@maximbilan
maximbilan / InstagramWallPost.swift
Created July 24, 2016 06:11
iOS Instagram Wall Post
let image = UIImage(named: "example")
let instagramURL = NSURL(string: "instagram://app")
if UIApplication.sharedApplication().canOpenURL(instagramURL!) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let saveImagePath = (documentsPath as NSString).stringByAppendingPathComponent("Image.igo")
let imageData = UIImagePNGRepresentation(image!)
do {
try imageData?.writeToFile(saveImagePath, options: NSDataWritingOptions(rawValue: 0))
} catch {
print("Instagram sharing error")