Twilio文檔表明可以創建一個簡單的iOS應用程序來發出和接收呼叫。甚至還有一個示例項目。名爲MonkeyPhone的示例項目包含ARC錯誤,因此拒絕運行。使用Twilio API/SDK在iOS上創建基本電話應用程序
更廣泛的問題是,Twilio是用於在iOS或Android應用上放置和接收呼叫的最佳API /平臺嗎?
Twilio文檔表明可以創建一個簡單的iOS應用程序來發出和接收呼叫。甚至還有一個示例項目。名爲MonkeyPhone的示例項目包含ARC錯誤,因此拒絕運行。使用Twilio API/SDK在iOS上創建基本電話應用程序
更廣泛的問題是,Twilio是用於在iOS或Android應用上放置和接收呼叫的最佳API /平臺嗎?
爲此,您可以使用TCConnectionDelegate方法。它會自動處理您的呼叫過程。這個步驟你不得不遵循。
1>您需要首先創建一個twilio帳戶並獲得帳戶SID和身份驗證令牌
2>之後去到開發工具 - > TWIML應用
創建一個新的twiml應用程序並創建
請提及您要通過電話號碼的語音請求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];
}
我希望它可以幫助你。如果您提出任何問題,請讓我知道。
連接委託方法不被稱爲 – 2015-06-26 07:11:22
因此,我們必須撥打電話的電話號碼是Twillio生成的號碼或任何電話號碼。 – 2017-02-22 09:58:33
我也使用twilio Api到我的ios應用程序project.it工作正常.. – Ravindhiran 2013-02-14 04:09:31
您是否能夠獲得Twilio提供的HelloMonkey示例工作? – samonderous 2013-02-15 00:56:45