ios - 让应用程序充当服务器

标签 ios iphone cocoahttpserver

我正在使用 CocoaHTTPServer,并且我能够启动服务器。在我的资源文件夹中,我添加了一个名为“index.html”的文件,这让我感到困惑。

  • 我可以通过

    获取索引文件的路径

    [主包 pathForResource:@"index"ofType:@"html"]

    但它给了我路径 /Users/Library/Application%20Support/iPhone%20Simulator/7.0.3/Applications/。 有什么方法可以让路径变成像这样的web-url http://127.0.0.1:56000/user/library/.../index.html" 将其加载到 UIWebView。

  • 我想获取像上面格式的文件路径,因为我想 当 UIWebView 向它发出请求时捕获以及应用程序何时会捕获 回应。

有什么办法可以实现吗?? 提前致谢。

最佳答案

  1. 您可以像这样将资源加载到 UIWebView

    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
    [webview loadRequest:[NSURLRequest requestWithURL:url]];
    
  2. 要捕获UIWebView 的请求,请检查UIWebViewDelegate 协议(protocol)here .您可以尝试使用以下方法,例如 webView:shouldStartLoadWithRequest:navigationType:

关于ios - 让应用程序充当服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22038273/

相关文章:

location - iOS 的 kCLLocationAccuracyHundredMeters 常量

iphone - iTunes App Store:主版本是否升级=较长的批准队列时间?

ios - Objective-C/CocoaHTTPServer - 是否可以在没有任何 UI 的情况下将 CocoaHTTPServer 作为后台应用程序运行?

ios - Chromecast 无法播放保存的视频

objective-c - NSThread 线程安全的初始化实现。

iphone - 如何使用 MFMailComposerViewController 将 UIImage 添加到电子邮件中?

ios - HealthKit 请求关联类型授权(针对标识符 : . 血压)

iphone - 子类化 UIToolbar 的正确方法

ios - initWithCoder 在 ios 中显示错误的帧

ios - 带有 SSL 和证书的 iPhoneHTTPServer