我的应用程序使用GCDWebServer
我从我的iPhone相册中获取照片AssetsLibrary
并将其存储在中NSDocumentDirectory
.我只想访问此照片的Url以在网页中显示它们.
有我的代码:
[_webServer addHandlerForMethod:@"POST"
path:@"/"
requestClass:[GCDWebServerURLEncodedFormRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
ALAsset *asset = [self.arrayPictures objectAtIndex:0];
ALAssetRepresentation *rep = [asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *thumbnail = [UIImage imageWithCGImage:iref];
NSString* path ;
if (thumbnail != nil)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
path = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"test.png"]];
NSData* data = UIImagePNGRepresentation(thumbnail);
[data writeToFile:path atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:path];
NSString* html = [NSString stringWithFormat:@"<html><body><img …
Run Code Online (Sandbox Code Playgroud) objective-c ios assetslibrary nsdocumentdirectory gcdwebserver
我正在尝试创建一个简单的 JSON 对象,但仍然出现错误,我知道我的代码有什么问题:
NSString *vCard = [BRContacts getContacts]; // this is just a string, could be nil
NSDictionary *JSONdic = nil;
if (vCard)
{
JSONdic = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"status",vCard,@"data", nil];
}
else
{
JSONdic = [NSDictionary dictionaryWithObjectsAndKeys:@"0",@"status",@"vCard is empty",@"error", nil];
}
NSError *error = nil;
NSData *JSONData = [NSJSONSerialization dataWithJSONObject:JSONdic options:NSJSONWritingPrettyPrinted error:&error];
return [GCDWebServerDataResponse responseWithJSONObject:JSONdata];
Run Code Online (Sandbox Code Playgroud)
例外是
JSON 写入中的顶级类型无效
我也检查过JSONdic
,在每种情况下都不是零。有什么建议?
我创建了一个小型演示应用程序,其中该应用程序使用UIImagePickerController从iPhone访问图像和视频。当我选择任何图像或视频时,应用程序会在文档目录中创建其副本(图像或视频)。并使用GCDWebserver在iPhone上创建Web服务器,并需要公开所选的图像或视频。但这行不通。
这是示例代码,不确定我哪里可能出错。
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var btnselect: UIButton!
let videoPicker = UIImagePickerController()
@IBAction func btnSelect(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
myPickerController.allowsEditing = false
self.present(myPickerController, animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
// 1. saving video to documents directory
let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as! NSURL
let videoData …
Run Code Online (Sandbox Code Playgroud) 从iOS8中的WKWebView加载本地Web时遇到问题.有一些建议我应该使用GCDWebServer.我通过GCDWebServer阅读,但不太了解GCDWebServer在移动应用程序方面的用途.
你能给我一些使用这个库的实际案例吗?对不起我的无知.
我有一个客户端 iOS 应用程序,它使用GCDWebServer来提供存储在NSSearchPathDirectory.DocumentDirectory
我设备上应用程序文件夹中的图像和视频。
在应用程序启动时,我启动一个实例GCDWebServer
并为我的请求添加一个文件响应处理程序:
self.addDefaultHandlerForMethod("GET", requestClass: GCDWebServerFileRequest.self) { request in
return GCDWebServerFileResponse(file: self.filePathForResponse(request.URL))
}
Run Code Online (Sandbox Code Playgroud)
我可以验证在应用程序运行时我可以从我的设备成功下载文件:
curl -O http://192.168.0.15:8080/files/IMG_1213-1280x720.MOV
Run Code Online (Sandbox Code Playgroud)
我的应用程序通过将上述 URL 发送到媒体频道来与 Chromecast 设备通信,Chromecast 接收器应用程序加载并播放指定 URL 处的视频 - 到目前为止一切正常。
我的问题是我想为当前播放的视频实现寻求支持,一旦我向媒体频道发送了一个搜索请求,我就会收到“Broken pipe”错误,GCDWebServer
视频播放被中断。来自服务器的日志如下:
....
[DEBUG] Connection sent 32768 bytes on socket 24
[DEBUG] Connection sent 32768 bytes on socket 24
[ERROR] Error while writing to socket 24: Broken pipe (32)
[DEBUG] Did close connection on socket 24
Run Code Online (Sandbox Code Playgroud)
我对这个问题的最佳理解是正常播放是有效的,因为它与从头到尾下载文件相同,这可以使用常规GCDWebServerFileResponse
,但是寻找相当于“跳转”到文件的不同部分,我我不确定读取这样的文件是否适用于我的配置。
GCDWebServer
使这项工作?我知道这个问题可以解决,因为有几个实时应用程序可以做到这一点。我试图从 iOS 设备上的远程服务器流式传输 mp4 视频。我不知何故[ERROR] Error while writing to socket 15: Broken pipe (32)
在写了几篇之后得到
了。
不知道哪里错了。你能给点建议吗?
iOS 使用 GCDWebServer 重新路由请求(不重定向)
谢谢!饶
@property (nonatomic, strong) GCDWebServerBodyReaderCompletionBlock cb;
[_webServer addDefaultHandlerForMethod:@"GET"
requestClass:[GCDWebServerRequest class]
processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
GCDWebServerStreamedResponse* response = [GCDWebServerStreamedResponse responseWithContentType:@"application/octet-stream" asyncStreamBlock:^(GCDWebServerBodyReaderCompletionBlock completionBlock) {
NSString * realURL = @"https://ia800309.us.archive.org/2/items/Popeye_Nearlyweds/Popeye_Nearlyweds_512kb.mp4";
self.cb = completionBlock;
NSURLSessionDataTask * dataTask = [self.session dataTaskWithURL:[[NSURL alloc] initWithString:realURL]] ;
}];
return response;
}];
(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask …
Run Code Online (Sandbox Code Playgroud) 我正在尝试让GCDWebServer显示静态内容.我有代码
import UIKit
import Foundation
class ViewController: UIViewController {
var webServer:GCDWebServer?
let urlpath = NSBundle.mainBundle().pathForResource("index", ofType: "html", inDirectory: "www")
override func viewDidLoad() {
super.viewDidLoad()
initWebServer()
}
func initWebServer() {
webServer = GCDWebServer()
webServer!.addGETHandlerForBasePath("/", directoryPath: urlpath, indexFilename: "index.html", cacheAge: 3600, allowRangeRequests: true)
webServer!.startWithPort(8080, bonjourName: "GCD Web Server")
print("Visit \(webServer!.serverURL) in your web browser")
}
}
Run Code Online (Sandbox Code Playgroud)
我也在我的Xcode项目的根目录中有文件夹www
当服务器启动时,它会在控制台中显示服务器地址为192.168.1.2:8080.但是当我尝试在浏览器中打开该URL时,我在控制台中看不到index.html,黑屏和404.
我究竟做错了什么 ?
当我的应用程序进入后台时,GCDWebServer (3.3.3) 发生崩溃:
#3 0x000000010041ea80 in -[GCDWebServer dealloc] at project/Pods/GCDWebServer/GCDWebServer/Core/GCDWebServer.m:221
#4 0x00000001004248b8 in __destroy_helper_block_ ()
#5 0x000000018dd52a28 in _Block_release ()
#6 0x00000001020ad21c in _dispatch_client_callout ()
#7 0x00000001020b2284 in _dispatch_main_queue_callback_4CF ()
#8 0x000000018ee21f2c in __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ ()
#9 0x000000018ee1fb18 in __CFRunLoopRun ()
#10 0x000000018ed4e048 in CFRunLoopRunSpecific ()
#11 0x00000001907d1198 in GSEventRunModal ()
#12 0x0000000194d28628 in -[UIApplication _run] ()
#13 0x0000000194d23360 in UIApplicationMain ()
#14 0x000000010009243c in main at project/main.m:10
#15 0x000000018dd305b8 in start ()
Enqueued from com.apple.main-thread (Thread 1)Queue : com.apple.main-thread (serial) …
Run Code Online (Sandbox Code Playgroud) 我正在使用 GCDWebServer 在我的设备上创建一个 HTTP 服务器。网页的实际内容放置在我的应用程序的 Documents 文件夹中。内容包含一个带有引用的 css 和 png 文件的 HTML 文件。问题是 css 和 png 文件无法从服务器访问/使用(我只能看到 HTML 文本内容)。
相关代码:
self.server = [[GCDWebServer alloc] init];
NSString *documents = [[[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject] absoluteString];
documents = [documents stringByAppendingPathComponent:@"MyWebsite"];
[self.server addGETHandlerForBasePath:@"/" directoryPath:documents indexFilename:nil cacheAge:0 allowRangeRequests:YES];
__weak typeof(self) weakSelf = self;
[_server addDefaultHandlerForMethod:@"GET" requestClass:[GCDWebServerRequest class] processBlock:^GCDWebServerResponse *(GCDWebServerRequest *request) {
if ([[request path] isEqualToString:@"/status"]) {
return [GCDWebServerDataResponse responseWithText:@"RUNNING"];
}
else if ([[request path] isEqualToString:@"/"]) {
NSString *filePath = [documents stringByAppendingPathComponent:@"mypage.html"];
NSString *html = …
Run Code Online (Sandbox Code Playgroud) gcdwebserver ×9
ios ×8
objective-c ×3
swift ×2
cocoa-touch ×1
crash ×1
iphone ×1
json ×1
streaming ×1
webserver ×1
wkwebview ×1
xcode ×1