您的位置:首页 > 理论基础 > 计算机网络

ios扩展http服务,将iphone做小型的http服务器

2016-06-24 00:00 423 查看
摘要: ios扩展http服务,将iphone做小型的http服务器.根据http://blog.csdn.net/zhibudefeng/article/details/9125783的方法整理好NewIdeasAPI_LocHttpServer.framework框架.使用方法如下

AppDelegate.h

//
//  AppDelegate.m
//  IosHttpServer
//
//  Created by  罗若文 on 16/6/24.
//  Copyright © 2016年 罗若文. All rights reserved.
//

#import "AppDelegate.h"
#import <NewIdeasAPI_LocHttpServer/NewIdeasAPI_LocHttpServer.h>

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

//[NIHTTPServer setPort:8080 withAction:@[@"/",@"/1/1.do"]];//如果没有配置Settings.plist可以用此方法配置端口和action路径
[[NIHTTPServer sharedNIHTTPServer] start];//开启http服务
return YES;
}

#pragma mark - 程序意外暂行
- (void)applicationWillTerminate:(UIApplication *)application {
[[NIHTTPServer sharedNIHTTPServer] stop];
}

@end

ViewController.h

//
//  ViewController.m
//  IosHttpServer
//
//  Created by  罗若文 on 16/6/24.
//  Copyright © 2016年 罗若文. All rights reserved.
//

#import "ViewController.h"
#import <NewIdeasAPI_LocHttpServer/NewIdeasAPI_LocHttpServer.h>

@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIButton *localhost;
@property (strong, nonatomic) IBOutlet UIButton *loaclhost_1;
@property (strong, nonatomic) IBOutlet UIButton *localhost_login;
@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];

//局域网内可以用浏览器访问http://手机ip:端口/1/1.do  得到该数据
[NIHTTPServer setActionResponseHtml:@"<html><head><title>测试</title></head><body><h1>html页面</h1><p>内容</p></body></html>" action:@"/1/1.do"];

//局域网内可以用浏览器访问http://手机ip:端口/login.do  得到该数据
[NIHTTPServer setActionResponseData:@"这边可以设置/login.do路径下可以得到的字符串" action:@"/login.do"];

//这方法可以设置在请求本机http服务的时候,在Response返回数据之前执行testHttp方法
[NIHTTPServer setMethodForHTTPServer:@selector(testHttp:) methodObject:self];

}

-(void)testHttp:(NSString *)action{
//这边可以拦截action,判断做对应的操作,可以控制对应的action要返回的动态数据
if([@"/" isEqualToString:action]){
//同步请求数据
NSString *dataUrl = [NSString stringWithFormat:@"https://git.oschina.net/luoruowen/LRWplist/raw/master/Mbomc.plist"];
dataUrl = [dataUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:dataUrl];
NSString *responseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
//将数据放入到
[NIHTTPServer setActionResponseData:responseString action:action];
}
}

- (IBAction)localhost:(UIButton *)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/"]];
}

- (IBAction)loaclhost_1:(UIButton *)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/1/1.do"]];
}

- (IBAction)localhost_login:(UIButton *)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://127.0.0.1:8080/login.do"]];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

Settings.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>HTTPServerConfigure</key>
<dict>
<key>说明</key>
<string>这边配置扩展http服务端口(默认8080)和action路径(action路径至少要配置一个"/")</string>
<key>HTTP_SERVER_PORT</key>
<real>8080</real>
<key>HTTP_SERVER_ACTION</key>
<array>
<string>/</string>
<string>/login.do</string>
<string>/1/1.do</string>
</array>
</dict>
</dict>
</plist>


我整理的一个ios扩展http服务 NewIdeasAPI_LocHttpServer.framework

上面demo下载地址:http://git.oschina.net/shareDemoCode/IosHttpServer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息