Thursday, 14 July 2016

Onclick Label calling with PhoneNumber in ios swift2

OnClick Label Calling with PhoneNumber

    1.  On click Label Call dynamically

      <***********Steps to create  calling Button**********>

      • Add code in info.plist

      </dict>
          <key>LSApplicationQueriesSchemes</key>
           <array>       
              <string>telprompt</string>
              <string>tel</string>
          </array>
      </dict>

      • Add label in ViewController dynamically by using CGPoints.
                //declare label

      let phoneNumberLabel:UILabel = UILabel(frame: CGRectMake(85, 220, 120, 45))

      //In viewDidLoad()
       override func viewDidLoad() {
              super.viewDidLoad()
       phoneNumberLabel.bounds = CGRectMake(CGRectGetMidX(self.view.frame), 220, 120, 45)
              phoneNumberLabel.text = "Click To Call"
              self.view.addSubview(phoneNumberLabel)
      }
      •  Create An touchesBegan Event in View Controller 

      override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
              for touch: AnyObject in touches {       
                  let location = touch.locationInView(self.view)
                  if CGRectContainsPoint(phoneNumberLabel.frame, location) {
                      UIApplication.sharedApplication().openURL(NSURL(string: "tel://1234567890")!)
                  }
              }
          }          

<*******OR********> 

<***********Steps to create  calling label with Outlet**********>

      1. Add pies of code in info.plist .

        </dict>
            <key>LSApplicationQueriesSchemes</key>
             <array>       
                <string>telprompt</string>
                <string>tel</string>
            </array>
        </dict>

      2. Add Label in Storyboard.
      3.   Create An Label Outlet in View Controller.

        @IBOutlet var callPhoneNumberLabel: UILabel!
      4. Create Function for Calling label.

        override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
                for touch: AnyObject in touches {       
                    let location = touch.locationInView(self.view)
                    if CGRectContainsPoint(callPhoneNumberLabel.frame, location) {
                        UIApplication.sharedApplication().openURL(NSURL(string: "tel://1234567890")!)
                    }
                }
            }          

         

      5. Finished Run and Check ......happy Coding


Onclick Button calling with PhoneNumber in ios swift2

  1.  On click Button Call

    <***********Steps to create  calling Button**********>

    1. Add pies of code in info.plist

      </dict>
          <key>LSApplicationQueriesSchemes</key>
           <array>       
              <string>telprompt</string>
              <string>tel</string>
          </array>
      </dict>

    2. Add Button in Storyboard
    3.  Create An Button Action in View Controller 

      @IBAction func call(sender: AnyObject) {
      //Replace with your Phone Number
      UIApplication.sharedApplication().openURL(NSURL(string: "tel://1234567890")!)
      }

      <*******OR********>

      <***********Steps to create  calling Button with function**********>

    1. Add pies of code in info.plist 

      </dict>
          <key>LSApplicationQueriesSchemes</key>
           <array>       
              <string>telprompt</string>
              <string>tel</string>
          </array>
      </dict>

    2. Add Button in Storyboard
    3.  Create An Button Action in View Controller with callPhoneNumber Method.

      @IBAction func call(sender: AnyObject) { 
      //Replace with your Phone Number
      callPhoneNumber("1234567890")
      }
    4. Create Function with name callPhoneNumber() 

      private func callPhoneNumber(phoneNumber:String) {
              if let phoneCallURL:NSURL = NSURL(string: "tel://8050741946") {
                  let application:UIApplication = UIApplication.sharedApplication()
                  if (application.canOpenURL(phoneCallURL)) {
                      application.openURL(phoneCallURL);
                  }
              }
          }

       

    5. Finished Run and Check ......happy Coding