ios本地服务器搭建

      在开发MDM的过程中,服务器端动态生成描述文件后会有临时文件保存在服务器端,拖慢服务器效率。为解决该问题,服务器端将描述文件放在responseobject里返回给客户端,客户端搭建本地服务器,将描述文件存在本地服务器上,并调用safari访问本地服务器安装描述文件。具体步骤:

1.下载CocoaHTTPServer,并加入project

2./*

*启动本地服务器端口

*/

- (void)startServer

{

_httpServer = [[HTTPServer alloc] init];

[_httpServer setType:@"_http._tcp."];

[_httpServer setPort:12345];

NSString *documentsDirectory =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//设置服务器根路径

[_httpServer setDocumentRoot:documentsDirectory];

NSError * error;

if([_httpServer start:&error])

{

NSLog(@"start server success in port %d %@",[_httpServer listeningPort],[_httpServer publishedName]);

}

else

{

NSLog(@"启动失败");

}

}

3.

-(void)launchLocalServer:(NSString *)config{

NSString *documentsDirectory =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

//获取文件路径

NSString *filename = [documentsDirectory stringByAppendingPathComponent:@"config.mobileconfig"];

//将描述文件写入该目录下

[config writeToFile:filename atomically:YES encoding:NSUTF8StringEncoding error:nil];

[self startServer];

//本地服务器地址

NSString *path = [NSString stringWithFormat:@"http://localhost:12345/config.mobileconfig"];

//调用safari安装描述文件

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:path]];

}

你可能感兴趣的:(ios本地服务器搭建)