前言:如果你不想自己编译PJSIP,可以使用别人编译好了的。下面我就介绍如果快速的实战。
1.下载别人编译好了的PJSIP,xpjsip
pod 'xpjsip'
Pod下来之后你会发现全是c语言,接下来我们就需要一个别人写好的封装库了
2.导入PJSIP的封装库,Gossip
这个库不支持CocoaPods,下载源码-将
Gossip
文件夹及里面的文件拖入项目即可。如果发现一些简单bug,修改一下即可。
3.简单使用及简单封装
3.1 连接服务并注册账户
导入Gossip,
#import "Gossip.h"
/**
* 注册账户
* @param username 用户名
* @param password 密码
* @param domain 域
* @param server 服务器地址(带端口)
*/
- (BOOL)registerAccount:(NSString *)username
password:(NSString *)password
domain:(NSString *)domain
server:(NSString *)server
{
GSAccountConfiguration *account = [GSAccountConfiguration defaultConfiguration];
account.address = [NSString stringWithFormat:@"%@@%@", username, domain];
account.username = username;
account.password = password;
account.domain = domain;
account.proxyServer = server;
GSConfiguration *configuration = [GSConfiguration defaultConfiguration];
configuration.account = account;
configuration.logLevel = 3;
configuration.consoleLogLevel = 3;
GSUserAgent *agent = [GSUserAgent sharedAgent];
[agent configure:configuration];
BOOL flag = [agent start];
if (!flag) {
return NO;
}
GSAccount *acc = agent.account;
acc.delegate = self;
BOOL connect = [acc connect];
return connect;
}
描述:acc.delegate = self;
只有一个代理方法- (void)account:(GSAccount *)account didReceiveIncomingCall:(GSCall *)call
这个就是来电回调。
3.2 拨打电话
/**
* 拨打电话
* @param phone 电话号码
* @param domain 域
*/
- (void)outgoingCall:(NSString *)phone
domain:(NSString *)domain
{
GSAccount *account = [GSUserAgent sharedAgent].account;
NSString *uri = [NSString stringWithFormat:@"sip:%@@%@", phone, domain];
GSCall *call = [GSCall outgoingCallToUri:uri fromAccount:account];
// 弹出电话页面
// ...
}
3.3 重置账户
/**
* 重置账户
*/
- (BOOL)reset
{
return [[GSUserAgent sharedAgent] reset];
}
其实这就是断开服务,退出账户(在你的应用退出业务的时候调用)。一下Gossip对该方法的原注释:
/// Resets the user agent to an unconfigured state.
/** You will need to call GSUserAgent::configure() and GSUserAgent::start() again.
* You may use this method to resets and reconnect user agent to a different account.
*/
- (BOOL)reset;
总结
PJSIP在全网的资料特别少,特别是iOS相关资料。我写这篇文章也是想后面的人少走弯路,能愉快的开发!PJSIP特别强大,更多的API就你们自己去挖掘!当然你有更好的封装库,欢迎推荐!