Skip to content

Instantly share code, notes, and snippets.

View benaneesh's full-sized avatar

Ben Aneesh benaneesh

View GitHub Profile
@benaneesh
benaneesh / ios-cell-registration-swift.md
Created May 11, 2016 22:01 — forked from gonzalezreal/ios-cell-registration-swift.md
iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

iOS Cell Registration & Reusing with Swift Protocol Extensions and Generics

A common task when developing iOS apps is to register custom cell subclasses for both UITableView and UICollectionView. Well, that is if you don’t use Storyboards, of course.

Both UITableView and UICollectionView offer a similar API to register custom cell classes:

public func registerClass(cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)
public func registerNib(nib: UINib?, forCellWithReuseIdentifier identifier: String)
func compressImage(image:UIImage) -> NSData? {
var actualHeight : CGFloat = image.size.height
var actualWidth : CGFloat = image.size.width
let maxHeight : CGFloat = 1136.0
let maxWidth : CGFloat = 640.0
var imgRatio : CGFloat = actualWidth/actualHeight
let maxRatio : CGFloat = maxWidth/maxHeight
var compressionQuality : CGFloat = 0.3
extension UIStoryboard {
enum Storyboard : String {
case Main
case News
case Gallery
}
/**
Better convenience initializer to hide ugly code
import UIKit
enum Currency{
case USD
case EUR
case AUD
case CAD
case HKD
case INR
let tweetSheet = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
tweetSheet.setInitialText(myText)
tweetSheet.addImage(myImage)
tweetSheet.addURL(myUrl)
tweetSheet.completionHandler = ({
switch($0) {
case .Cancelled:
// means the user cancelled without sending the Tweet
break
case .Done:
@benaneesh
benaneesh / fbLogin.swift
Created March 30, 2015 01:21
IOS FaceBook login with Parse
// MARK: - Login Button Pressed
@IBAction func loginButtonPressed(sender: AnyObject) {
MBProgressHUD.showHUDAddedTo(self.view, animated: true)
let permissions = ["user_about_me", "public_profile", "email", "user_relationships", "user_birthday", "user_location"]
PFFacebookUtils.logInWithPermissions(permissions, { (user: PFUser!, error: NSError!) -> Void in
if user == nil {
if (error == nil) {
println("User cancelled FB login")
}else{
println("FB login error: \(error)")
@benaneesh
benaneesh / ClassName.swift
Created February 4, 2015 19:40
Getting NSCoding on swift
import UIKit
class ClassName: NSObject, NSCoding {
var myVariable: String?
override init() {}
required init(coder aDecoder: NSCoder) {
myVariable = aDecoder.decodeObjectForKey("myVariable") as? String
}
//
// GooglePlaces.swift
//
// Created by Kirby Shabaga on 9/8/14.
// ------------------------------------------------------------------------------------------
// Ref: https://developers.google.com/places/documentation/search#PlaceSearchRequests
//
// Example search
//
@benaneesh
benaneesh / Log.m
Created January 29, 2015 21:16
Suppress printlns in production
#ifdef DEBUG
#define NSLog(...) /* suppress NSLog when in release mode */
#endif
@benaneesh
benaneesh / Push.swift
Last active October 5, 2017 20:26
Push notification in swift (iOS)
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
// notification
if let options = launchOptions {
let notificationPayload: NSDictionary = options[UIApplicationLaunchOptionsRemoteNotificationKey] as NSDictionary
// do somehting with the notifications
}
// Register for Push Notitications, if running iOS 8