iOS http服务

1.下载CocoaHTTPServer,并加入project

/*启动本地服务器端口/

- (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(@"启动失败");
    }
}
  1. 设置
-(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]];
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容