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

 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路径至少要配置一个&quot;/&quot;)</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

 

转载于:https://my.oschina.net/ruowen/blog/700368

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值