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]];
}

你可能感兴趣的:(iOS http服务)