Sinch on swift 2 - swift

I am new in develop app for IOS and i am still learning but i can not handle a incoming call.
I do not know how to delegate and override the method. Can someone write a little example in swift 2.
Thats what i have until now, how can override the method didReceiveIncomingCall?
import Foundation
import UIKit
class LoginView: UIViewController, SINCallClientDelegate, SINCallDelegate{
#IBOutlet weak var labelUsuario: UILabel!
#IBOutlet weak var nameUsername: UILabel!
#IBAction func buttonDesconectar() {
//Add Disconnect code for VOIP
let defaults = NSUserDefaults.standardUserDefaults()
defaults.removeObjectForKey("usuario")
self.performSegueWithIdentifier("segueLoginViewToInicio", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
let vozIP = Sinch.clientWithApplicationKey(<APP_KEY>, applicationSecret: <APP_SECRET>, environmentHost: "sandbox.sinch.com", userId: "Roke" )
vozIP.setSupportCalling(true)
vozIP.setSupportActiveConnectionInBackground(true)
vozIP.start()
vozIP.startListeningOnActiveConnection()
}
}

Related

Check if timer is finished

I'm sandboxing a little timer app.
I'm using this cocoapod
This is my code so far:
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
}
}
Now I want to check if the timer is finished (can use this cocoapod built in function: countdownLabel.isFinished() ).
But I have no clue WHERE and HOW I can check this, e.g. in viewDidLoad() I can’t check .isFinished()....
As an example: if(countdownLabel.isFinished()){countdownLabel.text = "Finished"}
WHERE to insert this line in the code??
I would be very happy if you can quickly copy and paste the code as an answer - with the mentioned line correctly inserted :)
Thank you!
You can use delegate method countdownFinished() of CountdownLabel to check when counter finished as mentioned in the example of this pod. Your updated code as below
import UIKit
import CountdownLabel
class ViewController: UIViewController {
#IBOutlet weak var moreBtn: UIButton!
#IBOutlet weak var lessBtn: UIButton!
#IBOutlet weak var gifview: UIImageView!
#IBOutlet weak var countdownLabel: CountdownLabel!
var mins = 25
override func viewDidLoad() {
super.viewDidLoad()
setupCountdown()
updateTimer(min: mins)
}
// MARK: Buttons
#IBAction func startPressed(_ sender: Any) {
countdownLabel.start()
}
#IBAction func lessPressed(_ sender: Any) {
if(mins > 0){
mins = mins - 5
updateTimer(min: mins)
}
}
#IBAction func morePressed(_ sender: Any) {
mins = mins + 5
updateTimer(min: mins)
}
//MARK: Helper Func
func updateTimer(min: Int){
countdownLabel.setCountDownTime(minutes: 60*Double(min))
}
func setupCountdown(){
countdownLabel.textColor = .black
countdownLabel.font = UIFont(name:"Courier", size:UIFont.labelFontSize)
countdownLabel.animationType = .Evaporate
countdownLabel.countdownDelegate = self //>>>>>> added this line to your code
}
}
//>>>>>>added this function to your code
extension ViewController: CountdownLabelDelegate {
func countdownFinished() {
debugPrint("countdownFinished at delegate.")
}
}
I think what you are looking for is a 'listener' for when it reaches 0 ("Finished")
From the docs (they only show on various times, though for 0 you should use
countdownLabel.then(0) { [unowned self] in
countdownLabel.text = "Finished"
}

OS X Hello World Swift tutorial

So I'm following this tutorial for Swift and am not getting the desired result at the end. This is my code for the ViewController:
import Cocoa
class ViewController: NSViewController {
#IBOutlet weak var nameField: NSTextField!
#IBOutlet weak var helloLabel: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
#IBAction func sayButtonClicked(_ sender: Any) {
var name = nameField.stringValue
if name.isEmpty {
name = "World"
}
let greeting = "Hello \(name)!"
helloLabel.stringValue = greeting
}
}
And the following is the result:
When I debug the program however it does show the correct variables:
What am I doing wrong here?
Turns out I didn't stretch out the the Label field enough.

Error : Consecutive statements on a line must be separated by ';' What's wrong?

import UIKit
class ViewController: UIViewController {
//Place your instance variables here
let questions = Questionbank()
#IBOutlet weak var questionLabel: UILabel!
#IBOutlet weak var scoreLabel: UILabel!
#IBOutlet var progressBar: UIView!
#IBOutlet weak var progressLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let firstQuestion = questions.list[0]
questionLabel.text = firstQuestion.questionText
}
#IBAction func answerPressed(_ sender: AnyObject) {
}
func updateUI() {
}
func nextQuestion() {
}
func checkAnswer() {
}
func startOver() {
}
}
This is my code above. And following is the error come up.
This is the errors come up.
There seems to be problem with questionLabel.text line but
I can't figure it out. This is a part of ios tutorial I am currently
working on.
Thank you for help.
I just found out why.
Problem was questionLabel.text not being written using xcode auto complete feature.
First time I typed manually and got error.
Second time, I let Xcode autocomplete it and it worked.
Anyone know why this works this way?

Unable to perform segue from UIView connected to XIB

This is UIView file's owner of XIB
import UIKit
class sideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let mapviewcontroller = MapViewController()
#IBAction func userProfile(_ sender: UIButton) {
mapviewcontroller.performSegueFromView(stringFromView: "userprofile")
}
Whereas inside my UIviewcontroller
class MapViewController: UIViewController
{
func performSegueFromView(stringFromView:String){
performSegue(withIdentifier: "\(stringFromView)", sender: nil)
}
}
It says that my segue identifier does not exist, please help, i'm quite lost. And i couldn't perform segue in UIView as well, and i tried to use storyboard ID method too, but the present(vc,animated:true,completion:nil) not showing up.
First things first, I think you should not access the view controller from your view. It is better to model the controller logic in the view controller rather than in the view.
That said, you can use a delegate pattern to implement your functionality.
import UIKit
class SideBarContent: UIView {
#IBOutlet weak var userName: UILabel!
#IBOutlet var sideBarContentView: UIView!
let delegate: SideBarContentDelegate? = nil
#IBAction func userProfile(_ sender: UIButton) {
self.delegate?.performSegueFromView(withIdentifier: "userprofile")
}
}
protocol SideBarContentDelegate {
func performSegueFromView(withIdentifier identifier: String)
}
class MapViewController: UIViewController, SideBarContentDelegate {
#IBOutlet private weak var sidebarView: SideBarContent!
override func viewDidLoad() {
super.viewDidLoad()
self.sidebarView.delegate = self
}
func performSegueFromView(withIdentifier identifier: String) {
self.performSegue(withIdentifier: identifier, sender: nil)
}
}
More info : https://stackoverflow.com/a/1340470/2603900

Xcode 7 Swift 2 error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, SUBCODE=0X0)

Ello! I am working on a notes app for mac, using swift as the language. I am currently working on a save and load feature, and have run across an error I am not sure how to fix. Here is the problematic code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
// The line below gets the error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
var name = loadnamefield.stringValue;
var lnote = NSUserDefaults.standardUserDefaults().objectForKey(name)
notefield.stringValue = lnote as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
What might I be doing wrong?
I figured it out. With some testing, I discovered that everything worked until I added notefield.stringValue into the code.I then realized that I had this is a different window. The code is covering 3 different windows, 2 of which are popups. notefield is the only element I had inside the code from the main window. I moved all of the elements to the same window, and recoded the entire thing. I got it working well using the following code:
import Cocoa
class NoteViewController: ViewController {
#IBOutlet weak var notefield: NSTextField!
#IBOutlet weak var savenamefield: NSTextField!
#IBOutlet weak var loadnamefield: NSTextField!
#IBAction func save(sender: AnyObject) {
NSUserDefaults.standardUserDefaults().setObject(notefield.stringValue, forKey: savenamefield.stringValue)
}
#IBAction func load(sender: AnyObject) {
notefield.stringValue = NSUserDefaults.standardUserDefaults().objectForKey((loadnamefield?.stringValue)!) as! String
}
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
}
}
Please note that I am supplying this code as a guideline only. Thanks for the help that was given.