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


No comments:

Post a Comment