Skip to main content


Showing posts from 2016

How to show activity indicator in watch kit

To show loading indicator in watch kit without using any third party, follow the below steps:- Download the images here and add them in watch kit at xcassets (MyPWatch WatchKit App -> Assets.xcassets)Add the WKInterfaceImage (named imageLoading) to your 
WKInterfaceController Show loading indicator as belowself.imageLoading.setImageNamed("Activity") self.imageLoading.startAnimatingWithImages(in: NSMakeRange(1, 15), duration: 3.0, repeatCount: 0) To hide the indicator, do as below:- self.imageLoading.stopAnimating() self.imageLoading.setHidden(true)

Passing data to Apple Watch app from iPhone

I made a utility for transferring the data between iWatch and iPhone. This is done using the WatchConnectivity framework which is available in watch OS 2 and later.General steps to perform. Although all steps are written in the working sample code. Create the WCSessionImplement the WCSessionDelegate methods. WKInterfaceController will receive the context dictionary in one of the delegate method.In the UIViewController, Create the WCSession. When ever you want to share information to watch, then update the ApplicationContext tryWatchData.sharedInstance.session.updateApplicationContext(["message" : message])

Full working code is available at

Vector graphics in iOS

In past, designers had to create multiple versions of the same asset (1x, 2x, 3x) to satisfy multiple screen sizes. Using vector assets can save you time because you only have to generate the asset once. You just need an.xcassets file in your project for storing your images. In there, you can declare image sets to be “Vectors”.
A vector file contains a lot of metadata of an asset that tells the system how to render it's contents, independent of the screen's resolution. This also means that whenever we get larger screen resolutions, Xcode will be able to scale up your images from your already existing vector PDF for you, giving you automatic support for future devices for free.

Select “New Image Set” in your XCAsset.Select the Attributes Inspector in Utilities panel.Under the types drop-down menu, select "Vector"Drag and drop your vector PDFUse the Xcode image catalog image set as you would with any other image. For instance, calling --imageNamed: in code, and pa…

Collection view inside table view with NO vertical scrolling

UICollectionView inside a UITableViewCell — dynamic height?
Given the following structure: TableViewTableViewCellCollectionViewCollectionViewCellCollectionViewCellCollectionViewCell[...variable number of cells or different cell sizes]
The solution is to tell auto layout to compute first the collectionViewCell sizes, then the collection view contentSize, and use it as the size of your cell. This is the UIView method that "does the magic": -(void)systemLayoutSizeFittingSize:(CGSize)targetSize      withHorizontalFittingPriority:(UILayoutPriority)horizontalFittingPriority            verticalFittingPriority:(UILayoutPriority)verticalFittingPriority

Full code is available here to download.
Thanks to this awesome answer.

Disabling print for Production In Swift Project

You'll need to set up a compiler flag to use the Swift preprocessor - go to the Swift Compiler - Custom Flags section of Build Settings to set up a -D DEBUG flag:

Then in your code you can define a DLog() function and only print your message if the DEBUG flag is set:
func DLog(message: String, function: String = __FUNCTION__) { #if DEBUG     println("\(function): \(message)") #endif }

Check Application version | iOS | Swift

Show the alert within application for update the application.
func checkApplicationVersion(){ let urlString = String(format: "",AppleAppID) let url = NSURL(string: urlString) let urlRequest = NSURLRequest(URL: url!) NSURLConnection.sendAsynchronousRequest(urlRequest, queue: NSOperationQueue.mainQueue()) { (response, data, error) -> Voidin if (error == nil)             { do { let appMetadataDictionary : NSDictionary = tryNSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary let resultsArray : NSArray = appMetadataDictionary.objectForKey("results") as! NSArray let resultsDic : NSDictionary = resultsArray.firstObjectas! NSDictionary
let iTunesVersion : String = resultsDic.objectForKey("version") as! String let appVersionString : String = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as! String if(…

Load custom table view cell from xib if it contains multiple views

var cell : UploadedPhotoTableCell!=tableView.dequeueReusableCellWithIdentifier("UploadedPhotoTableCell") as? UploadedPhotoTableCell if cell == nil         { let topLevelObjects: [AnyObject] = NSBundle.mainBundle().loadNibNamed("UploadedPhotoTableCell", owner: self, options: nil) for currentObject: AnyObjectin topLevelObjects { if (currentObject isUITableViewCell) {                     cell = currentObject as! UploadedPhotoTableCell                 }             }         }

// It it UITableViewCell class contains only one view i.e content view 
var cell : UploadedPhotoTableCell!=tableView.dequeueReusableCellWithIdentifier("UploadedPhotoTableCell") as? UploadedPhotoTableCell if cell == nil         {             tableView.registerNib(UINib(nibName: "UploadedPhotoTableCell", bundle: nil), forCellReuseIdentifier: "UploadedPhotoTableCell")             cell = tableView.dequeueReusableCellWithIdentifier("UploadedPhotoTableCell") as? UploadedPhotoTa…

Load custom xib from viewcontroller in SWIFT

In custom UIView class  class NearByOffer: UIView, override the init method as follows

overrideinit(frame: CGRect) { super.init(frame: frame) xibSetup()     }
requiredinit?(coder aDecoder: NSCoder) { super.init(coder: aDecoder)

Set up the xib as follows:-
func xibSetup() { view = loadViewFromNib() view.frame = bounds view.autoresizingMask = [UIViewAutoresizing.FlexibleWidth, UIViewAutoresizing.FlexibleHeight]         // Adding custom subview on top of our view (over any custom drawing > see note below) view.backgroundColor = UIColor(white: 1.0, alpha: 0.5) addSubview(view)     }

Load view from custom nib/xib as follows:- func loadViewFromNib() -> UIView { let bundle = NSBundle(forClass: self.dynamicType) let nib = UINib(nibName: "NearByOffer", bundle: bundle) // Assumes UIView is top level and only object in CustomView.xib file let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView return view     }

USAGE in ViewController
func showNearByOfferPopup()     { let window = UIApplic…

Blocks in SWIFT 3 and SWIFT 2


//Custom view/cell say LocationPickerView (: UIView)

typealias blockDefination_DepatureCityAction = (_ button : UIButton) -> Void
var handler_DepatureCityAction:blockDefination_DepatureCityAction?

@IBActionfunc btnDepatureCityAction(_ sender: UIButton) { if(self.responds(to: #selector(getter: LocationPickerView.handler_DepatureCityAction))){ self.handler_DepatureCityAction?(sender)         }
//View controller where you want callback
let locationPickerObject = LocationPickerView(frame: viewLocation.bounds)
locationPickerObject.handler_DepatureCityAction = { button in print("DepatureCityAction in view controller") print(button)         }

//**************** BLOCK WITHOUT PARAMETER
//DECLARE as property typealias completionBlockSeeMore = () -> Voidvar cMoreButtonCallBack:completionBlockSeeMore?if(self.respondsToSelector("cMoreButtonCallBack")){self.cMoreButtonCallBack!() }

//USAGE cell.cMoreButtonCallBack = { () -> Voidin                    printf("Cal…