使用Twilio API/SDK在iOS上创建基本电话应用程序

问题描述:

Twilio文档表明可以创建一个简单的iOS应用程序来发出和接收呼叫。甚至还有一个示例项目。名为MonkeyPhone的示例项目包含ARC错误,因此拒绝运行。使用Twilio API/SDK在iOS上创建基本电话应用程序

更广泛的问题是,Twilio是用于在iOS或Android应用上放置和接收呼叫的最佳API /平台吗?

+0

我也使用twilio Api到我的ios应用程序project.it工作正常.. – Ravindhiran 2013-02-14 04:09:31

+0

您是否能够获得Twilio提供的HelloMonkey示例工作? – samonderous 2013-02-15 00:56:45

为此,您可以使用TCConnectionDelegate方法。它会自动处理您的呼叫过程。这个步骤你不得不遵循。

1>您需要首先创建一个twilio帐户并获得帐户SID和身份验证令牌 enter image description here

2>之后去到开发工具 - > TWIML应用

创建一个新的twiml应用程序并创建 enter image description here

请提及您要通过电话号码的语音请求URL。

3>将您的手机号码上TCConnectionDelegate连接方法

YourTwilioDeviceClass *phone = appDelegate.phone; 
    [phone connect:self.textFieldPhoneNumber.text]; 

4>现在通过传递accountSid,的authToken,APPID您auth.php文件在服务器上得到认证令牌。

#pragma mark - 
    #pragma mark TCDevice Capability Token 

    -(NSString*)getCapabilityToken:(NSError**)error 
    { 
     //Creates a new capability token from the auth.php file on server 
     NSString *capabilityToken = nil; 

     NSString *accountSid = [[NSUserDefaults standardUserDefaults] objectForKey:@"accountSid"]; 
     NSString *authToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"authToken"]; 
     NSString *appId = [[NSUserDefaults standardUserDefaults] objectForKey:@"appId"]; 

     //Make the URL Connection to your server 

     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.yourauthfilepath.com/twillo/auth.php?accountSid=%@&authToken=%@&appSid=%@",accountSid,authToken,appId]]; 
     NSURLResponse *response = nil; 
     NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] 
              returningResponse:&response error:error]; 
     if (data) 
     { 
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 

      if (httpResponse.statusCode==200) 
      { 
       capabilityToken = [[[NSString alloc] initWithData:data 
                 encoding:NSUTF8StringEncoding] autorelease]; 
      } 
      else 
      { 
       //*error = [ConferencePhone errorFromHTTPResponse:httpResponse domain:@"CapabilityTokenDomain"]; 
      } 
     } 
     // else there is likely an error which got assigned to the incoming error pointer. 

     return capabilityToken; 
    } 

    -(BOOL)capabilityTokenValid 
    { 
     //Check TCDevice's capability token to see if it is still valid 
     BOOL isValid = NO; 
     // NSLog(@"_device.capabilities %@",_device.capabilities); 
     NSNumber *expirationTimeObject = [_device.capabilities objectForKey:@"expiration"]; 
     long long expirationTimeValue = [expirationTimeObject longLongValue]; 
     long long currentTimeValue = (long long)[[NSDate date] timeIntervalSince1970]; 

     if ((expirationTimeValue-currentTimeValue)>0) 
      isValid = YES; 

     return isValid; 
    } 

    #pragma mark - 
    #pragma mark Device Network connect and disconnect 

    -(void)connect:(NSString*)phoneNumber 
    { 
     // first check to see if the token we have is valid, and if not, refresh it. 
     // Your own client may ask the user to re-authenticate to obtain a new token depending on 
     // your security requirements. 

     HelloMonkeyAppDelegate *appDelegate=(HelloMonkeyAppDelegate*)[[UIApplication sharedApplication]delegate]; 

     if (![self capabilityTokenValid] || (!appDelegate.isTokenGet)) 
     { 
      //Capability token is not valid, so create a new one and update device 
      [self login]; 
     } 



     if (![self reachabiltyCheck]) 
     { 
      NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"5", nil] forKeys:[NSArray arrayWithObjects:CPLoginDidFailWithError, nil]]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 

      HelloMonkeyAppDelegate *appDelegate=(HelloMonkeyAppDelegate*)[[UIApplication sharedApplication]delegate]; 
      appDelegate.isTokenGet=FALSE; 
     } 
     else 
     { 
      NSDictionary* parameters = nil; 
      if ([phoneNumber length] > 0) 
      { 
       parameters = [NSDictionary dictionaryWithObject:phoneNumber forKey:@"PhoneNumber"]; 
      } 

      NSLog(@"parameters ===%@",parameters); 
      _connection = [_device connect:parameters delegate:self]; 
      [_connection retain]; 
     } 


    } 

    -(void)disconnect 
    { 
     [_connection disconnect]; 
     [_connection release]; 
     _connection = nil; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"hideCallingView" object:nil]; 
    } 

    #pragma mark - 
    #pragma mark - TCConnection Delegate Methods 

    -(void)connectionDidDisconnect:(TCConnection*)connection 
    { 
     NSLog(@"Call disconnected"); 

     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"4", nil] forKeys:[NSArray arrayWithObjects:@"Disconnected", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 

    } 

    -(void)connection:(TCConnection*)connection didFailWithError:(NSError*)error 
    { 
     NSLog(@"Failed %@",[error description]); 
     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"3", nil] forKeys:[NSArray arrayWithObjects:@"Error", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)connectionDidStartConnecting:(TCConnection*)connection 
    { 
     NSLog(@"Calling.."); 
     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", nil] forKeys:[NSArray arrayWithObjects:@"Calling", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)connectionDidConnect:(TCConnection*)connection 
    { 
     NSLog(@"In call.."); 

     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"2", nil] forKeys:[NSArray arrayWithObjects:@"In call", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)dealloc 
    { 
     [_device release]; 
     [_connection release]; 

     [super dealloc]; 
    } 

5>最后使用TCConnectionDelegate方法

#pragma mark - 
    #pragma mark TCDevice Capability Token 

    -(NSString*)getCapabilityToken:(NSError**)error 
    { 
     //Creates a new capability token from the auth.php file on server 
     NSString *capabilityToken = nil; 

     NSString *accountSid = [[NSUserDefaults standardUserDefaults] objectForKey:@"accountSid"]; 
     NSString *authToken = [[NSUserDefaults standardUserDefaults] objectForKey:@"authToken"]; 
     NSString *appId = [[NSUserDefaults standardUserDefaults] objectForKey:@"appId"]; 

     //Make the URL Connection to your server 

     NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.e-home.com/twillo/auth.php?accountSid=%@&authToken=%@&appSid=%@",accountSid,authToken,appId]]; 
     NSURLResponse *response = nil; 
     NSData *data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:url] 
              returningResponse:&response error:error]; 
     if (data) 
     { 
      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response; 

      if (httpResponse.statusCode==200) 
      { 
       capabilityToken = [[[NSString alloc] initWithData:data 
                 encoding:NSUTF8StringEncoding] autorelease]; 
      } 
      else 
      { 
       //*error = [ConferencePhone errorFromHTTPResponse:httpResponse domain:@"CapabilityTokenDomain"]; 
      } 
     } 
     // else there is likely an error which got assigned to the incoming error pointer. 

     return capabilityToken; 
    } 

    -(BOOL)capabilityTokenValid 
    { 
     //Check TCDevice's capability token to see if it is still valid 
     BOOL isValid = NO; 
     // NSLog(@"_device.capabilities %@",_device.capabilities); 
     NSNumber *expirationTimeObject = [_device.capabilities objectForKey:@"expiration"]; 
     long long expirationTimeValue = [expirationTimeObject longLongValue]; 
     long long currentTimeValue = (long long)[[NSDate date] timeIntervalSince1970]; 

     if ((expirationTimeValue-currentTimeValue)>0) 
      isValid = YES; 

     return isValid; 
    } 

    #pragma mark - 
    #pragma mark Device Network connect and disconnect 

    -(void)connect:(NSString*)phoneNumber 
    { 
     // first check to see if the token we have is valid, and if not, refresh it. 
     // Your own client may ask the user to re-authenticate to obtain a new token depending on 
     // your security requirements. 

     HelloMonkeyAppDelegate *appDelegate=(HelloMonkeyAppDelegate*)[[UIApplication sharedApplication]delegate]; 

     if (![self capabilityTokenValid] || (!appDelegate.isTokenGet)) 
     { 
      //Capability token is not valid, so create a new one and update device 
      [self login]; 
     } 



     if (![self reachabiltyCheck]) 
     { 
      NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"5", nil] forKeys:[NSArray arrayWithObjects:CPLoginDidFailWithError, nil]]; 
      [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 

      HelloMonkeyAppDelegate *appDelegate=(HelloMonkeyAppDelegate*)[[UIApplication sharedApplication]delegate]; 
      appDelegate.isTokenGet=FALSE; 
     } 
     else 
     { 
      NSDictionary* parameters = nil; 
      if ([phoneNumber length] > 0) 
      { 
       parameters = [NSDictionary dictionaryWithObject:phoneNumber forKey:@"PhoneNumber"]; 
      } 

      NSLog(@"parameters ===%@",parameters); 
      _connection = [_device connect:parameters delegate:self]; 
      [_connection retain]; 
     } 


    } 

    -(void)disconnect 
    { 
     [_connection disconnect]; 
     [_connection release]; 
     _connection = nil; 

     [[NSNotificationCenter defaultCenter] postNotificationName:@"hideCallingView" object:nil]; 
    } 

    #pragma mark - 
    #pragma mark - TCConnection Delegate Methods 

    -(void)connectionDidDisconnect:(TCConnection*)connection 
    { 
     NSLog(@"Call disconnected"); 

     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"4", nil] forKeys:[NSArray arrayWithObjects:@"Disconnected", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 

    } 

    -(void)connection:(TCConnection*)connection didFailWithError:(NSError*)error 
    { 
     NSLog(@"Failed %@",[error description]); 
     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"3", nil] forKeys:[NSArray arrayWithObjects:@"Error", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)connectionDidStartConnecting:(TCConnection*)connection 
    { 
     NSLog(@"Calling.."); 
     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"1", nil] forKeys:[NSArray arrayWithObjects:@"Calling", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)connectionDidConnect:(TCConnection*)connection 
    { 
     NSLog(@"In call.."); 

     NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"2", nil] forKeys:[NSArray arrayWithObjects:@"In call", nil]]; 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"showCallingStatusView" object:nil userInfo:dict]; 
    } 

    -(void)dealloc 
    { 
     [_device release]; 
     [_connection release]; 

     [super dealloc]; 
    } 

我希望它可以帮助你。如果您提出任何问题,请让我知道。

+0

连接委托方法不被称为 – 2015-06-26 07:11:22

+0

因此,我们必须拨打电话的电话号码是Twillio生成的号码或任何电话号码。 – 2017-02-22 09:58:33