Index: pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj	(revision 4496)
+++ pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua.xcodeproj/project.pbxproj	(revision 4497)
@@ -8,13 +8,12 @@
 
 /* Begin PBXBuildFile section */
-		3A00CD2E170EA6590093CE38 /* pjsua_cli.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A00CD2A170EA6590093CE38 /* pjsua_cli.c */; };
-		3A00CD2F170EA6590093CE38 /* pjsua_common.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A00CD2B170EA6590093CE38 /* pjsua_common.c */; };
-		3A00CD30170EA6590093CE38 /* pjsua_config.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A00CD2C170EA6590093CE38 /* pjsua_config.c */; };
-		3A00CD31170EA6590093CE38 /* pjsua_legacy.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A00CD2D170EA6590093CE38 /* pjsua_legacy.c */; };
-		3A92068416F1CE8000D49F96 /* pjsua_app.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A92067F16F1CE8000D49F96 /* pjsua_app.c */; };
-		3A92068516F1CE8000D49F96 /* pjsua_cli_cmd.c in Sources */ = {isa = PBXBuildFile; fileRef = 3A92068016F1CE8000D49F96 /* pjsua_cli_cmd.c */; };
 		3A92068F16F1DE7100D49F96 /* AudioToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A92068E16F1DE7100D49F96 /* AudioToolbox.framework */; };
 		3A92069316F1DEA500D49F96 /* CFNetwork.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A92069216F1DEA500D49F96 /* CFNetwork.framework */; };
 		3ADCCD171715338D0007BE8E /* pjsua.png in Resources */ = {isa = PBXBuildFile; fileRef = 3ADCCD161715338D0007BE8E /* pjsua.png */; };
+		3ADCCD231726D1920007BE8E /* pjsua_app_cli.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD1E1726D1920007BE8E /* pjsua_app_cli.c */; };
+		3ADCCD241726D1920007BE8E /* pjsua_app_common.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD1F1726D1920007BE8E /* pjsua_app_common.c */; };
+		3ADCCD251726D1920007BE8E /* pjsua_app_config.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD201726D1920007BE8E /* pjsua_app_config.c */; };
+		3ADCCD261726D1920007BE8E /* pjsua_app_legacy.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD211726D1920007BE8E /* pjsua_app_legacy.c */; };
+		3ADCCD271726D1920007BE8E /* pjsua_app.c in Sources */ = {isa = PBXBuildFile; fileRef = 3ADCCD221726D1920007BE8E /* pjsua_app.c */; };
 		3AF0580916F050770046B835 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF0580816F050770046B835 /* UIKit.framework */; };
 		3AF0580B16F050770046B835 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3AF0580A16F050770046B835 /* Foundation.framework */; };
@@ -51,13 +50,12 @@
 
 /* Begin PBXFileReference section */
-		3A00CD2A170EA6590093CE38 /* pjsua_cli.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_cli.c; path = ../../pjsua_cli.c; sourceTree = "<group>"; };
-		3A00CD2B170EA6590093CE38 /* pjsua_common.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_common.c; path = ../../pjsua_common.c; sourceTree = "<group>"; };
-		3A00CD2C170EA6590093CE38 /* pjsua_config.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_config.c; path = ../../pjsua_config.c; sourceTree = "<group>"; };
-		3A00CD2D170EA6590093CE38 /* pjsua_legacy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_legacy.c; path = ../../pjsua_legacy.c; sourceTree = "<group>"; };
-		3A92067F16F1CE8000D49F96 /* pjsua_app.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_app.c; path = ../../pjsua_app.c; sourceTree = "<group>"; };
-		3A92068016F1CE8000D49F96 /* pjsua_cli_cmd.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = pjsua_cli_cmd.c; path = ../../pjsua_cli_cmd.c; sourceTree = "<group>"; };
 		3A92068E16F1DE7100D49F96 /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; };
 		3A92069216F1DEA500D49F96 /* CFNetwork.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CFNetwork.framework; path = System/Library/Frameworks/CFNetwork.framework; sourceTree = SDKROOT; };
 		3ADCCD161715338D0007BE8E /* pjsua.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = pjsua.png; sourceTree = "<group>"; };
+		3ADCCD1E1726D1920007BE8E /* pjsua_app_cli.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pjsua_app_cli.c; sourceTree = "<group>"; };
+		3ADCCD1F1726D1920007BE8E /* pjsua_app_common.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pjsua_app_common.c; sourceTree = "<group>"; };
+		3ADCCD201726D1920007BE8E /* pjsua_app_config.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pjsua_app_config.c; sourceTree = "<group>"; };
+		3ADCCD211726D1920007BE8E /* pjsua_app_legacy.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pjsua_app_legacy.c; sourceTree = "<group>"; };
+		3ADCCD221726D1920007BE8E /* pjsua_app.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = pjsua_app.c; sourceTree = "<group>"; };
 		3AF0580416F050770046B835 /* ipjsua.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ipjsua.app; sourceTree = BUILT_PRODUCTS_DIR; };
 		3AF0580816F050770046B835 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
@@ -136,10 +134,9 @@
 			isa = PBXGroup;
 			children = (
-				3A00CD2A170EA6590093CE38 /* pjsua_cli.c */,
-				3A00CD2B170EA6590093CE38 /* pjsua_common.c */,
-				3A00CD2C170EA6590093CE38 /* pjsua_config.c */,
-				3A00CD2D170EA6590093CE38 /* pjsua_legacy.c */,
-				3A92067F16F1CE8000D49F96 /* pjsua_app.c */,
-				3A92068016F1CE8000D49F96 /* pjsua_cli_cmd.c */,
+				3ADCCD1E1726D1920007BE8E /* pjsua_app_cli.c */,
+				3ADCCD1F1726D1920007BE8E /* pjsua_app_common.c */,
+				3ADCCD201726D1920007BE8E /* pjsua_app_config.c */,
+				3ADCCD211726D1920007BE8E /* pjsua_app_legacy.c */,
+				3ADCCD221726D1920007BE8E /* pjsua_app.c */,
 			);
 			name = pjsua;
@@ -304,10 +301,9 @@
 				3AF0581916F050780046B835 /* ipjsuaAppDelegate.m in Sources */,
 				3AF0582216F050780046B835 /* ipjsuaViewController.m in Sources */,
-				3A92068416F1CE8000D49F96 /* pjsua_app.c in Sources */,
-				3A92068516F1CE8000D49F96 /* pjsua_cli_cmd.c in Sources */,
-				3A00CD2E170EA6590093CE38 /* pjsua_cli.c in Sources */,
-				3A00CD2F170EA6590093CE38 /* pjsua_common.c in Sources */,
-				3A00CD30170EA6590093CE38 /* pjsua_config.c in Sources */,
-				3A00CD31170EA6590093CE38 /* pjsua_legacy.c in Sources */,
+				3ADCCD231726D1920007BE8E /* pjsua_app_cli.c in Sources */,
+				3ADCCD241726D1920007BE8E /* pjsua_app_common.c in Sources */,
+				3ADCCD251726D1920007BE8E /* pjsua_app_config.c in Sources */,
+				3ADCCD261726D1920007BE8E /* pjsua_app_legacy.c in Sources */,
+				3ADCCD271726D1920007BE8E /* pjsua_app.c in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -410,4 +406,5 @@
 					../../../../pjnath/include,
 					../../../../pjmedia/include,
+					..,
 				);
 				INFOPLIST_FILE = "ipjsua/ipjsua-Info.plist";
@@ -443,4 +440,5 @@
 					../../../../pjnath/include,
 					../../../../pjmedia/include,
+					..,
 				);
 				INFOPLIST_FILE = "ipjsua/ipjsua-Info.plist";
Index: pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m
===================================================================
--- pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m	(revision 4496)
+++ pjproject/trunk/pjsip-apps/src/pjsua/ios/ipjsua/ipjsuaAppDelegate.m	(revision 4497)
@@ -11,5 +11,7 @@
 #import <pjsua.h>
 #import <pj/log.h>
-#include "../../pjsua_common.h"
+
+#include "../../pjsua_app.h"
+#include "../../pjsua_app_config.h"
 
 #import "ipjsuaViewController.h"
@@ -17,55 +19,108 @@
 @implementation ipjsuaAppDelegate
 
+#define THIS_FILE	"ipjsuaAppDelegate.m"
+
 #define KEEP_ALIVE_INTERVAL 600
 
-static int _argc = 4;
-static char *_argv[] = {"",
-    "--use-cli",
-    "--no-cli-console",
-    "--cli-telnet-port=6378"
-};
-
-ipjsuaAppDelegate              *app;
-extern pj_cli_telnet_on_started on_started_cb;
-extern pj_cli_on_quit           on_quit_cb;
-extern pj_cli_on_destroy        on_destroy_cb;
-extern pj_cli_on_restart_pjsua  on_restart_pjsua_cb;
-static pj_thread_desc           a_thread_desc;
-static pj_thread_t             *a_thread;
-
-int main_func(int argc, char *argv[]);
-
-static void cli_telnet_started(pj_cli_telnet_info *telnet_info)
-{
-    PJ_LOG(3,("ipjsua", "Telnet to %.*s:%d",
-	      telnet_info->ip_address.slen, telnet_info->ip_address.ptr,
-	      telnet_info->port));
-    NSString *str = [NSString stringWithFormat:@"Telnet to %.*s:%d",
-                     (int)telnet_info->ip_address.slen,
-                     telnet_info->ip_address.ptr,
-                     telnet_info->port];
+ipjsuaAppDelegate      *app;
+static pjsua_app_cfg_t  app_cfg;
+static bool             isShuttingDown;
+static char           **restartArgv;
+static int              restartArgc;
+static pj_thread_desc   a_thread_desc;
+static pj_thread_t     *a_thread;
+
+static void displayMsg(const char *msg)
+{
+    NSString *str = [NSString stringWithFormat:@"%s", msg];
     [app performSelectorOnMainThread:@selector(displayMsg:) withObject:str
-         waitUntilDone:NO];
-}
-
-static void cli_on_quit (pj_bool_t is_restarted)
-{
-    PJ_LOG(3,("ipjsua", "CLI quit, restart(%d)", is_restarted));
-    if (!is_restarted) {
-        NSString *str = [NSString stringWithFormat:@"CLI quit, "
-                         "telnet unavailable"];
-        [app performSelectorOnMainThread:@selector(displayMsg:) withObject:str
-             waitUntilDone:NO];
-    }
-}
-
-- (void)displayMsg:(NSString *)str {
+                       waitUntilDone:NO];
+}
+
+static void pjsuaOnStartedCb(pj_status_t status, const char* msg)
+{
+    char errmsg[PJ_ERR_MSG_SIZE];
+    
+    if (status != PJ_SUCCESS && (!msg || !*msg)) {
+	pj_strerror(status, errmsg, sizeof(errmsg));
+	PJ_LOG(3,(THIS_FILE, "Error: %s", errmsg));
+	msg = errmsg;
+    } else {
+	PJ_LOG(3,(THIS_FILE, "Started: %s", msg));
+    }
+
+    displayMsg(msg);
+}
+
+static void pjsuaOnStoppedCb(pj_bool_t restart,
+                             int argc, char** argv)
+{
+    PJ_LOG(3,("ipjsua", "CLI %s request", (restart? "restart" : "shutdown")));
+    if (restart) {
+        displayMsg("Restarting..");
+	pj_thread_sleep(100);
+        app_cfg.argc = argc;
+        app_cfg.argv = argv;
+    } else {
+        displayMsg("Shutting down..");
+	pj_thread_sleep(100);
+        isShuttingDown = true;
+    }
+}
+
+static void pjsuaOnAppConfigCb(pjsua_app_config *cfg)
+{
+    PJ_UNUSED_ARG(cfg);
+}
+
+- (void)displayMsg:(NSString *)str
+{
     app.viewController.textLabel.text = str;
 }
 
-- (void)start_app {
-    on_started_cb = &cli_telnet_started;
-    on_quit_cb = &cli_on_quit;
-    main_func(_argc, _argv);
+- (void)pjsuaStart
+{
+    // TODO: read from config?
+    const char **argv = pjsua_app_def_argv;
+    int argc = PJ_ARRAY_SIZE(pjsua_app_def_argv) -1;
+    pj_status_t status;
+    
+    isShuttingDown = false;
+    displayMsg("Starting..");
+    
+    pj_bzero(&app_cfg, sizeof(app_cfg));
+    if (restartArgc) {
+	app_cfg.argc = restartArgc;
+	app_cfg.argv = restartArgv;
+    } else {
+	app_cfg.argc = argc;
+	app_cfg.argv = (char**)argv;
+    }
+    app_cfg.on_started = &pjsuaOnStartedCb;
+    app_cfg.on_stopped = &pjsuaOnStoppedCb;
+    app_cfg.on_config_init = &pjsuaOnAppConfigCb;
+    
+    while (!isShuttingDown) {
+        status = pjsua_app_init(&app_cfg);
+        if (status != PJ_SUCCESS) {
+            char errmsg[PJ_ERR_MSG_SIZE];
+            pj_strerror(status, errmsg, sizeof(errmsg));
+            displayMsg(errmsg);
+            pjsua_app_destroy();
+            return;
+        }
+    
+        status = pjsua_app_run(PJ_TRUE);
+        if (status != PJ_SUCCESS) {
+            char errmsg[PJ_ERR_MSG_SIZE];
+            pj_strerror(status, errmsg, sizeof(errmsg));
+            displayMsg(errmsg);
+        }
+    
+        pjsua_app_destroy();
+    }
+    
+    restartArgv = NULL;
+    restartArgc = 0;
 }
 
@@ -85,5 +140,5 @@
     
     /* Start pjsua app thread */
-    [NSThread detachNewThreadSelector:@selector(start_app) toTarget:self withObject:nil];
+    [NSThread detachNewThreadSelector:@selector(pjsuaStart) toTarget:self withObject:nil];
 
     return YES;
