iPhone上搭建簡單服務(wù)器

緣起:

一個(gè)朋友給我一份HTML寫的游戲代碼,想要跑到iOS上,做成原生應(yīng)用。很簡單嘛,開個(gè)工程,拖一個(gè)WKWebView,加載本地HTML,一氣呵成。但是。。。黑屏!查看代碼發(fā)現(xiàn)

    <script>
    // Issue a warning if trying to preview an exported project on disk.
    (function(){
        // Check for running exported on file protocol
        if (window.location.protocol.substr(0, 4) === "file")
        {
            alert("Exported games won't work until you upload them. (When running on the file:/// protocol, browsers block many features from working for security reasons.)");
        }
    })();
    </script>

不支持本地加載。。。

但是總不能為這個(gè)游戲買個(gè)域名空間,做個(gè)后臺(tái)吧。于是我就想能不能再iPhone上搭建一個(gè)服務(wù)器,直接請(qǐng)求本地呢,就像Django一樣,于是,我打開了邪惡的百度(hosts不能用了。。。)

解決方案:

果然互聯(lián)網(wǎng)上資源多啊,早在好幾年前就有人這么做了,并且開源了,就是CocoaHTTPServer,但是時(shí)間長了,不支持Pod,于是search了一下,有一個(gè)支持了pod的開源,叫CVCocoaHTTPServeriOS。按照說明,這個(gè)庫支持OSX,iOS:

1、Built in support for bonjour broadcasting
2、IPv4 and IPv6 support
3、Asynchronous networking using GCD and standard sockets
4、Password protection support
5、SSL/TLS encryption support
6、Extremely FAST and memory efficient
7、Extremely scalable (built entirely upon GCD)
8、Heavily commented code
9、Very easily extensible
10、WebDAV is supported too!

emmmm...姿勢很多,聽說有人用這個(gè)做出了迅雷WiFi傳片的功能,這里暫時(shí)用不到,只是用來加載本地資源。

實(shí)現(xiàn):

于是用pod加載了CocoaHTTPServer,引入頭文件,在AppDelegate里開啟服務(wù)器,并且記錄隨機(jī)分配的端口號(hào):

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

    [self configLocalHttpServer];
    return YES;
}
#pragma mark - 搭建本地服務(wù)器 并且啟動(dòng)
- (void)configLocalHttpServer{
    self.localHttpServer = [[HTTPServer alloc] init];
    [self.localHttpServer setType:@"_http.tcp"];
    //指定服務(wù)器的目錄,以后請(qǐng)求資源就在這個(gè)目錄里,這個(gè)目錄拖拽進(jìn)來的時(shí)候要選Create folder references,是一個(gè)藍(lán)色文件夾
    NSString * webLocalPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"5"];
    [self.localHttpServer setDocumentRoot:webLocalPath];
    [self startServer];
}
- (void)startServer
{
    NSError *error;
    if([self.localHttpServer start:&error]){
        self.port = [NSString stringWithFormat:@"%d",[self.localHttpServer listeningPort]];
    }
    else{

    }
}

這里端口號(hào)如果沒有指定的話,會(huì)隨機(jī)分配一個(gè),也可以在啟動(dòng)服務(wù)器的時(shí)候指定一個(gè)端口號(hào):

[self.localHttpServer setPort:6000];

服務(wù)器啟動(dòng)以后就可以加載網(wǎng)頁了,像這樣:

    AppDelegate *appd = (AppDelegate *)[UIApplication sharedApplication].delegate;
    NSString *port = appd.port;
    if (nil == port) {
        return NO;
    }
    NSString *str = [NSString stringWithFormat:@"http://localhost:%@", port];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:request];

因?yàn)槲业亩丝谔?hào)是隨機(jī)分配的,所以這里引入了AppDelegate。加載http://localhost拼接你的端口號(hào),默認(rèn)會(huì)加載剛才的資源目錄里的index.html文件,此時(shí),游戲就已經(jīng)可以跑起來了!如果你是模擬器運(yùn)行,還可以用Mac上的瀏覽器,打開http://localhost拼接你的端口號(hào),就可以訪問你的iPhone 上的服務(wù)器了!

總結(jié):

由于iOS程序不能常駐后臺(tái),想要在后臺(tái)也開啟,就需要用定位功能或者其他的后臺(tái)模式??偟膩碚f,能在iPhone上實(shí)現(xiàn)一個(gè)服務(wù)器,后續(xù)可以想象的空間很大??!

Demo在此

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容