Run a Web Server from an iPhone App

本文介绍了一种从iPhone应用程序中运行轻量级Web服务器的方法,并保持应用在后台运行不被挂起。使用了cocoahttpserver作为Web服务器组件及一个修改版的MMPDeepSleepPreventer来阻止应用进入睡眠状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://www.mokten.com/2011/12/run-a-web-server-from-an-iphone-app/


Run a Web Server from an iPhone App

Yoo-Jin Lee Wed, Dec 14, 2011

If you ever needed to run a simple web server from an iPhone App here is one way to do it.

This example uses a lightweight web server cocoahttpserver and a sleep timer based onMMPDeepSleepPreventer by Marco Peluso.

 

Key Points

  1. Running a web server from an iPhone App is possible but will not respond when in the suspended or terminated state
  2. Stop the iPhone App going to the suspended state, when exiting, by playing an empty audio file
    1. Playing an audio file does not work in the iOS Simulator v5.0. So you need to test this on a device.
    2. Background executiion is only available in iOS 4 and later
    3. Reference: iOS Developer Library

 

Overview

  1. MTAudioPlayer.m: plays an empty audio file that loops forever
    1. Based on old version of MMPDeepSleepPreventer by Marco Peluso
    2. Needed to be modified because the the periodic thread scheduling was not working in iOS 5
  2. iPhoneHTTPServer-Info.plist
  3. Add property: Required background modes: App plays audio
  • iPhoneHTTPServerViewController.m: Opens up the localhost server URL and should show /Web/index.html

 

Full sample code: cocoahttpserver
Run the sample: /Samples/iPhoneHTTPServer

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值