Skip to content

Commit 5e7aa38

Browse files
author
Devin Rader
committed
Swift formatting, add notifications
1 parent acbb778 commit 5e7aa38

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

SwiftPhone/AppDelegate.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1616

1717
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
1818
// Override point for customization after application launch.
19+
if UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")) {
20+
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, categories: nil))
21+
}
22+
1923
return true
2024
}
2125

SwiftPhone/Phone.swift

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
import Foundation
1010

11-
let SPDefaultClientName:String = "jenny";
11+
let SPDefaultClientName:String = "jenny"
1212
let SPBaseCapabilityTokenUrl:String = "http://example.com/generateToken?%@"
13-
let SPTwiMLAppSid:String = "APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
13+
let SPTwiMLAppSid:String = "APxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
1414

1515
public class Phone : NSObject, TCDeviceDelegate {
1616
var device:TCDevice!
@@ -19,57 +19,53 @@ public class Phone : NSObject, TCDeviceDelegate {
1919

2020
func login() {
2121

22-
TwilioClient.sharedInstance().setLogLevel(TCLogLevel._LOG_VERBOSE);
22+
TwilioClient.sharedInstance().setLogLevel(TCLogLevel._LOG_VERBOSE)
2323

24-
var url:String = self.getCapabilityTokenUrl();
24+
var url:String = self.getCapabilityTokenUrl()
2525

26-
var swiftRequest = SwiftRequest();
26+
var swiftRequest = SwiftRequest()
2727
swiftRequest.get(url, callback: { (err, response, body) -> () in
28-
if (err != nil) {
29-
return;
28+
if err != nil {
29+
return
3030
}
3131

3232
var token = body as! String
33-
println(token);
33+
println(token)
3434

35-
if (err == nil && token != "")
36-
{
37-
if ( self.device == nil )
38-
{
39-
self.device = TCDevice(capabilityToken: token as String, delegate: nil);
35+
if err == nil && token != "" {
36+
if self.device == nil {
37+
self.device = TCDevice(capabilityToken: token as String, delegate: nil)
4038
}
41-
else
42-
{
43-
self.device!.updateCapabilityToken(token);
39+
else {
40+
self.device!.updateCapabilityToken(token)
4441
}
4542
}
46-
else if ( err != nil && response != nil) {
43+
else if err != nil && response != nil {
4744
// We received and error with a response
4845
}
49-
else if (err != nil) {
46+
else if err != nil {
5047
// We received an error without a response
5148
}
52-
});
49+
})
5350
}
5451

5552
func getCapabilityTokenUrl() -> String {
5653

57-
var querystring:String = String();
54+
var querystring:String = String()
5855

59-
querystring += String(format:"&sid=%@", SPTwiMLAppSid);
60-
querystring += String(format:"&name=%@", SPDefaultClientName);
56+
querystring += String(format:"&sid=%@", SPTwiMLAppSid)
57+
querystring += String(format:"&name=%@", SPDefaultClientName)
6158

62-
return String(format:SPBaseCapabilityTokenUrl, querystring);
59+
return String(format:SPBaseCapabilityTokenUrl, querystring)
6360
}
6461

6562
func connectWithParams(params dictParams:Dictionary<String,String> = Dictionary<String,String>()) {
6663

67-
if (!self.capabilityTokenValid())
68-
{
69-
self.login();
64+
if !self.capabilityTokenValid() {
65+
self.login()
7066
}
7167

72-
self.connection = self.device?.connect(dictParams, delegate: nil);
68+
self.connection = self.device?.connect(dictParams, delegate: nil)
7369
}
7470

7571
func acceptConnection() {
@@ -90,21 +86,21 @@ public class Phone : NSObject, TCDeviceDelegate {
9086
}
9187

9288
func capabilityTokenValid()->(Bool) {
93-
var isValid:Bool = false;
89+
var isValid:Bool = false
9490

95-
if (self.device != nil) {
91+
if self.device != nil {
9692
var capabilities = self.device!.capabilities as NSDictionary;
9793

9894
var expirationTimeObject:NSNumber = capabilities.objectForKey("expiration") as! NSNumber;
9995
var expirationTimeValue:Int64 = expirationTimeObject.longLongValue;
10096
var currentTimeValue:NSTimeInterval = NSDate().timeIntervalSince1970;
10197

102-
if( (expirationTimeValue-Int64(currentTimeValue)) > 0 ) {
98+
if (expirationTimeValue-Int64(currentTimeValue)) > 0 {
10399
isValid = true;
104100
}
105101
}
106102

107-
return isValid;
103+
return isValid
108104
}
109105

110106
public func deviceDidStartListeningForIncomingConnections(device: TCDevice)->() {

SwiftPhone/ViewController.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ class ViewController: UIViewController {
3434
// Dispose of any resources that can be recreated.
3535
}
3636

37-
3837
@IBAction func btnCall(sender: AnyObject) {
3938
self.phone.connectWithParams();
4039
}
@@ -52,6 +51,13 @@ class ViewController: UIViewController {
5251
}
5352

5453
func pendingIncomingConnectionReceived(notification:NSNotification) {
54+
55+
if UIApplication.sharedApplication().applicationState != UIApplicationState.Active {
56+
var notification:UILocalNotification = UILocalNotification()
57+
notification.alertBody = "Incoming Call"
58+
UIApplication.sharedApplication().presentLocalNotificationNow(notification)
59+
}
60+
5561
self.btnAnswer.enabled = true
5662
self.btnReject.enabled = true
5763
self.btnIgnore.enabled = true

0 commit comments

Comments
 (0)