Tuesday, 29 November 2016

How to create UITableview in Swift - Tutorial

UITableView in Swift Tutorial

Introduction to UITableView in Swift:

UITableView is the basic control used in mobile apps. In this tutorial, we will learn how to create UITableView in swift language. All teh delgates for the tableview are same only syntax changes is there in swift language Let us start creating UITableView in swift.

Implementation of code for UITableView:

Create a single view application template and press next button

Select Single view application template


Name your project "UITableView-Swift" and select your development language as Swift language

Select Development language as Swift language 

Chose the location to save your project files and press create.

Now as we successfully created our project, open Main.storyboard and drag UITableView to the view. Add constraints (assuming that you know auto-layout). You can change your view's screen size from Attributed inspector as shown in below image

Change Screen Size of your view to iPhone 4- inch


 Open ViewController.swift file, create IBOutlet for our UITableView  and declare our cellIdentifier.


    @IBOutlet var tblList : UITableView?
    let cellIdentifier = "cellIdentifier"


Open Main.storyboard file and connect IBOutlet created in previous step with UITableView. Also connect UIItableView's delegate and datsource outlets with ViewController.

Connecting our UITableViews delegate and datasource Outlets to ViewController

 Open ViewController.swift file, here we will implement our UITableView delegates and data source
methods


    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 5;
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        
        var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
        if cell == nil {
            cell = UITableViewCell.init(style:UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
        }
        cell?.textLabel?.text = "row";
        return cell!;

    }


Run your app and you will see UITableView showing text inside rows. You can modify the code as per your needs.

Download Code for UITableView in swift: UitableView-Swift Source Code

Where to go from here:

In this tutorial you learned, how to create UITableView in swift language. I hope you enjoyed this post on How To create UITableview in Swift - Tutorial. If you have any questions, please feel free to comment.


Tuesday, 1 November 2016

How to create or generate .pem file for push notification - Tutorial

Generate .pem file for push notification - tutorial




Introduction to .pem file:

In this tutorial we will learn how to create / generate .pem file for Apple push notification in iOS.Pem file are placed on the server that connects with Apple Push server in order to send push to our iOS app.

Steps to create .pem file for Push Notification:

Step 1: Login with your developer account on Apple developer website

Step 2: Click on certificates, identifiers and profiles as shown in given below image/screenshot

Dashboard of Apple developer account


Step 3:  Click + icon/button in order to start generating push certificate


Step 4: Select Apple push certificate as per your requirement, we have two types of push certificates
1) Apple push Service SSL (Sandbox) -  used to send push notification during app development phase
2) Apple push Service SSL (Sandbox & production) - used to send push notification when app goes live on app store

Select type of push certificate you want to generate


Step 5:  We selected Apple Push Notification SSL listed under Development tab for this tutorial, After selection press continue button at the bottom of the page.

Selected development push certificate
Step 6: Select  app id for which you want to configure Push Notifications and press continue button

Selected App id for which you want to  add push notifications


Select 7: Generate CSR (certificate Signing Request) as described by Apple. If you already have one press continue and upload it by clicking on Choose file button. Press continue to move to next step.

Step 8: You will see your apple development push service certificate. Download it and press done.

Development push certificate created successfully


Step 9: Locate the downloaded certificate in the finder and double click in order to install it in your mac machine. It will open another application called "Keychain Access".  Now go to “Keychain” on you mac system, look for the certificate you have just installed. If you are unsure which certificate is the correct , it should start with “Apple Production IOS Push Services”and  followed by app’s bundleID.

Step 10:  Expand the certificate, you should see the private key with either your name or your company name. Select both items by using the “Select” key on your keyboard, right click (or cmd-click if you use a single button mouse), choose “Export 2 items”. It will save as .p12 file.

Export 2 item from keychain access 


 Step 11: Save them with name "pushcert". System will prompt you to secure the exported item with password, enter password (if you don't want to enter any password then leave it blank). In next step system will ask you to enter your system password in order to complete export.

Step 12: Open terminal and locate to place where you save .p12 files. (in our case its Desktop). Type below command in terminal

cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -
clcerts 

 We are done with the tutorial, we successfully generated .pem file. If you have any questions please feel 
free to comment.  





Sunday, 23 October 2016

How to create development and Distribution provisioning profile for Xcode - Tutorial

How to create development and Distribution provisioning profile for Xcode - Tutorial

Introduction to Provisioning profiles - Xcode:

In our last post we learned how can we create Development and Distribution certificates. In this tutorial we would learn how to create provisioning profile. If you did not know how to create development and distribution certificates for code signing, you can learn from below link

 Learn how to create Development and Distribution certificate

NOTE: Main difference between certificates and provisioning profiles is that certificates are independent of app id's whereas provisioning profiles are dependent to App id's and different for different App id's.

Provisioning profile contains information about your app and the physical devices on which app can get installed during development phase.

First we will learn to create development provisioning profile, for this we need to create app identifier(if already not created) and add our devices on which we want to install our app during development phase

App identifier: As the name suggest this uniquely identifies your app within the iOS system. Thus helping in enabling you to use things like in-app purchasing, push notifications, game center and more inside your app.

Steps to create provisioning profiles:

Step 1: Login with your developer account on Apple developer website

Step 2: Click on certificates, identifiers and profiles as shown in given below image/screenshot

 


Step 3:  Select App IDs placed as second column on left side of your screen.

Select App IDs 

Step 4: Click on + button, you will see screen as shown below 

Enter App ID name in teh field App ID Description

Give it a name that is meaningful and easy to remember e.g. MathLearner , i created this id for my app named MathLearner

Under App ID Suffix, select Explicit App ID(default selection). Enter you id in Bundle ID field as shown 

Enter your app ID in Bundle ID field

Step 6: Select App services your app offering to the users. e.g. push notification etc.

Enable app services offered by your app


Step 7: Press continue and in next step confirm the services you opt for your app. Press register. You successfully created identifier for your app.

Now we will add device (iPhone, iPad, iPod touch) on which we want to test our app. For this we need UDID (Unique device identifier). 

Step 8: Click on All under devices listed on left side column on your screen. Type the name and UDID. Click Continue and follow steps. You can only add 100 devices.

Add Device to the Apple poirtal


Step 9: Now we are done with our identifiers and devices let us create our provisioning profiles. Select iOS App Development, if you want to create development provisioning profile and App Store, if you want to create distribution provisioning profile. After selection press continue.

Select what typwe of provisioning profile you need


Step 10: Select App id for which you want to create the provisioning profile and press continue.


Select App id for which you want to create provisonig profile


Step 11:  Select certificate with which you want to sign your provisioning profile and press continue.

 Select certificates with which you want to sign your provisioning profile


Step 12:  Name your provisioning profile and press continue. (If you are creating development profile then you need to select devices before this steps shown on your screen)

Name your provisioning profile


Step 13: You are done with creating provisioning profile. You can now download it form download button.

Where to go from here:

In this post you learn how to create provisioning profile (Development Provisioning profile and Distribution Provisioning profile) along with App identifiers and how to add iPhone devices to the developer portal.

Tuesday, 18 October 2016

How to create development and Distribution certificate for Xcode Tutorial




How to create development and Distribution certificate for Xcode Tutorial

Introduction to Development and Distribution Certificates:

To test your iOS app on your iPhone, one needs certificate and provisioning profiles to sign the app. In this post we will learn how to create development and distribution certificates so that we can sign our app source code in xcode and install them on real iPhone devices or upload them to Apple stores.

Steps to create Development and Distribution Certificates:

Step 1: Login with your developer account on Apple developer website

Step 2: Click on certificates, identifiers and profiles as shown in given below image/screenshot

Click on Certificates, identifiers, profiles button

Apple provides two kinds of certificates,

1) Development certificates: Used to sign app in order to run them on test devices during development phase.
2) Distribution certificates: Used to sign app in order to upload them to app stores and make thme live to public around the globe.

Click '+' button

Clcik + button in order to start creating development certifcates


Step 3:  Select iOS app development by clicking on radio button next to it and press continue button at the bottom of the page.

Select development certifiacte by clicking radio button next to it

Step 4: Now we have to create CSR (Certificate signing request). For more guidance on how to create CSR follow below link

Create a Certificate Signing Request in a MAC

Step 5: Select CSR file created in step 4 by click choose file button and press continue.


Select CSR file by clicking choose file button

Step 6: You are done with creating development certificate. In the same way you can crest distribution certificate.

In this post we learned how to create development and distribution certificates for signing our app during development phase and for uploading them  to the apple store. 

Where to go from here:

Next we will learn how to create development and distribution provisioning profiles.  


Sunday, 25 September 2016

How to solve iOS app upload issue-The executable contains incomplete bitcode.To compile binaries with complete bitcode



How to solve iOS app upload issue - The executable contains incomplete bitcode.To compile binaries with complete bitcode


With new Xcode, one of the problem faced by iOS developers while uploading app to App store is bitcode related errors. In this post we will learn that how to fix bitcode error in our project.

If you are not using pods then simply open your project build settings and search bitcode  as shown in picture

Set enable bitcode to NO


Set Enable Bitcode to NO, by default it's set to YES. This will fix the bitcode error and  your app will get uploaded successfully to the store.

If you are using pods in project then you have to write a script in your pod file that will disable Enable Bitcode to NO in all of your installed pods during pods installation phase or while you are updating your pods.

Script for disabling bitcode: 



post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end




Your pod file will look like this



# Uncomment this line to define a global platform for your project
# platform :ios, '8.0'

use_frameworks!

target 'projectname' do

pod 'AFNetworking', '~> 3.0'
pod 'Fabric'
pod 'TwitterKit'
pod 'Crashlytics'
pod 'PubNub'
pod 'IQKeyboardManager'
pod 'SDWebImage', '~>3.8'
pod 'GoogleMaps'
pod 'RMUniversalAlert'
pod 'HexColors'

end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      config.build_settings['ENABLE_BITCODE'] = 'NO'
    end
  end
end


Hope this post helps you in overcoming bitcode errors while uploading your app to app store. 

Sunday, 18 September 2016

How to create PageViewController in IOS Tutorial - Swift




Introduction to UIPageViewController:

UIPageControl in IOS is a control that is used mostly in help screens or info screens for IOS apps. In this we are going to create a small tutorial for UIPageControl using swift language,  in which we will change background color of UIImageView as per page number selected by user on tapping on UIPageControl or swiping UIScrollViewUIScrollView is used with UIPageControl as the page control itself is set of dots and does not has display interface.

Learn how to create PageViewController in Objective C - Tutorial

Implementation of UIPageControl in swift:

Lets start our tutorial of creating page control in swift language. Create a new project with single view application template and name it UIPageViewController-swift.  Open Main.storyboard and drop UIScrollView and UIpagecontrol on to the view.

Drag UIscrollView and UIPageControl on to view
Drag UIscrollView and UIPageControl on to view

Add leading, trailing, bottom, top contraints to UIScrollView, Add constraint to page control we add horizontal and top constraint with respect to scrollview.


Add constraints to UIScrollView
Add constraints to UIScrollView

Add constraints to UIPageControl
Add constraints to UIPageControl

Now open ViewController.swift and create IBOutlets for UIPageControl and UIScrollView


 @IBOutlet var pageControl: UIPageControl?
  @IBOutlet var scrollView: UIScrollView?


and an IBAction changePage that we are going to assign to UIPageControl ValueChanged event so that we can detect that user has taped on UIPageControl.


@IBAction func changePage(){
        
    }





your ViewController.swift file will look like this


class ViewController: UIViewController {

    @IBOutlet var pageControl: UIPageControl?
    @IBOutlet var scrollView: UIScrollView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    @IBAction func changePage(){
        
    }
}





Open Main.storyboard and connect IBOutlets and IBAction to UIScrollView and UIPageControl

Connecting UIPageControl IBOutlet
Connecting UIPageControl IBOutlet

Connecting IBOutlet to UIScrollView
Connecting IBOutlet to UIScrollView





UIPageControl has properties to set minimum number of pages you want in your UIPageControl and which page do you want to set as current page. We will stick with default values for this tutorial




Set minimum pages and current page for UIPageControl



Open ViewController.swift , Inside viewDidLoad method, we will add UIImageView to our scroll and assign its delegate to self.We will set contentSize of scroll to number of pages we want in UIPageControl multiplied by width of UIScrollView.Then, we add three UIImageView to our scroll and set different background color to it.Add code to your viewDidLoad as shown below





var scrollWidth : CGFloat = UIScreen.mainScreen().bounds.size.width - 76.0
    var scrollHeight : CGFloat = UIScreen.mainScreen().bounds.size.height - 163.0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        scrollView?.contentSize = CGSizeMake((scrollWidth * 3), scrollHeight)
        scrollView?.delegate = self;
        scrollView?.pagingEnabled=true
        
        for i in 0...2 {
            let ih = Float(i)
            let xPos = (scrollWidth*CGFloat(ih));
            let imgView = UIImageView.init()
            imgView.frame = CGRectMake(CGFloat(xPos), 0scrollWidth,scrollHeight)
            imgView.backgroundColor = UIColor.redColor()
            if i == 0 {
                imgView.backgroundColor = UIColor.greenColor()
            }
            
            if i == 1 {
                imgView.backgroundColor = UIColor.blueColor()
            }
            
            scrollView?.addSubview(imgView)
        }
        
    }



Note: scrollWidth and scrollHeight are the width and height calculated with respect to empty space we left on top, bottom,left right or our UIScrollView with respect to view.

Run you code and you will see colored UIMageViews inside your scroll, Swipe left and right, you will see page effect. Still we have to implement our page control as user can also tap on page control in order to navigate through the different pages.


In our changePage action, we will use scrollRectToVisible  method of UIScrollView so that we can scroll our UIScrollView as per user tap on UIPageControl.


 @IBAction func changePage(){
         scrollView!.scrollRectToVisible(CGRectMake( scrollWidth * CGFloat ((pageControl?.currentPage)!), 0, scrollWidth, scrollHeight), animated: true)
    }
    



Lastly. we ned to implement scrollViewDidEndDecelerating delegate of UIScrollView so that we can calculate page number if user did not use UIPageControl and simple scroll down UIScrollView.


  
func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
       setIndiactorForCurrentPage()
    }
    
    func setIndiactorForCurrentPage()  {
       let page = (scrollView?.contentOffset.x)!/scrollWidth
        pageControl?.currentPage = Int(page)

    }

    



In setIndicatorForCurrentPage, we calculated page by diving x offset of UIScrollView with its width and the assign that page to our pageControl as its current or selected page.


Download Source Code: UIPageControl in Swift

Where to go from here:

In this post we learn about how to use/create UIPageControl in iOS using swift language. You can check UIPageControl objective C version from here UIPageControl tutorial in objective C 
I hope you enjoyed this post on How to create PageViewController in IOS Tutorial - Swift.