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 below self . imageLoading . setImageName d ( "Activity" )          self . imageLoading . startAnimati ngWithImages (in:  NSMakeRange ( 1 ,  15 ), duration:  3.0 , repeatCount:  0 ) To hide the indicator, do as below:-  self . imageLoading . stop Animating ()          self . imageLoading . setHidden ( tr ue )

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  WCSession Implement 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                  try WatchData . 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. Steps:- 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 PDF Use the Xcode image catalog image set as you would with any other image. For instance, calling --im

Collection view inside table view with NO vertical scrolling

UICollectionView inside a UITableViewCell — dynamic height? Given the following structure: TableView TableViewCell CollectionView CollectionViewCell CollectionViewCell CollectionViewCell [...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) -> Void in             if (error == nil )             {                 do {                     let appMetadataDictionary : NSDictionary = try NSJSONSerialization . JSONObjectWithData (data!, options: NSJSONReadingOptions .MutableContainers) as ! NSDictionary                     let resultsArray : NSArray = appMetadataDictionary. objectForKey ( "results" ) as ! NSArray                     let resultsDic : NSDictionary = resultsArray. firstObject as ! NSDictionary                     let iTunesVersion : String = r

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: AnyObject in topLevelObjects {                 if (currentObject is UITableViewCell ) {                     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 ),

Load custom xib from viewcontroller in SWIFT

In custom UIView class   class NearByOffer: UIView,  override the init method as follows     override init (frame: CGRect ) {         super . init (frame: frame)         xibSetup ()     }     required init ?(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

Blocks in SWIFT 3 and SWIFT 2

SWIFT 3 //Custom view/cell say  LocationPickerView (: UIView) typealias blockDefination_DepatureCityAction = ( _ button : UIButton ) -> Void var handler_DepatureCityAction: blockDefination_DepatureCityAction ? @IBAction func 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)         } SWIFT 2 //**************** BLOCK WITHOUT PARAMETER //DECLARE as property typealias completionBlockSeeMore = () -> Void var cMoreButtonCallBa