GCDWebServer icon indicating copy to clipboard operation
GCDWebServer copied to clipboard

GCDWebServer: Unable to open home page when using real iPhone

Open Haibo-Zhou opened this issue 4 years ago • 2 comments

I have an old app using GCDWebUploader of GCDWebServer to upload files into the app. And I copy the implementation code into a new project.

The problem: in this new project, the home page of GCDWebServer could be opened in iOS Simulator but cannot connect in real iPhone.

It is weird since I got a worked app already. So I did some checks:

  • first, old app is worked. Means the home page could be opened.
  • all devices are under same wifi.
  • try to open http://192.168.1.5/ or http://192.168.1.5:80 in browser, safari said "Cannot open the page, because the server isn't responding", however I could ping 192.168.1.5 in terminal successfully.
  • restart my iPhone
  • compare info.plist between two projects

No result! Would you guys advice about what I missed, maybe some config in Xcode to allow HTTP communication or some capabilities to enable?

Environment: Xcode 12.2, iOS 14 CocoaPods as dependency manager

What in console:

[DEBUG] Did open IPv4 listening socket 3
[DEBUG] Did open IPv6 listening socket 4
[INFO] GCDWebUploader started on port 80 and reachable at http://192.168.1.5/
Visit http://192.168.1.5/ in your web browser

The code:

import GCDWebServer
import Foundation

class MyWebUploader {
    // move webuploader to outside of init func to fix app crash issue.
    let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    lazy var webUploader = GCDWebUploader(uploadDirectory: self.documentsPath)
    
    func initWebUploader() -> String {
        var ipAddress = String()
        
        webUploader.start()
        webUploader.allowedFileExtensions = ["mp3", "aac", "m4a", "wav"]
        if webUploader.serverURL != nil {
            // retrieve IP address from URL
            let str = webUploader.serverURL!.absoluteString
            let start = str.index(str.startIndex, offsetBy: 7)
            let end = str.index(str.endIndex, offsetBy: -1)
            let range = start..<end
            let mySubstring = str[range]
            ipAddress = String(mySubstring)
            print("Visit \(webUploader.serverURL!) in your web browser")
        } else {
            ipAddress = "No Wifi connected"
        }
        return ipAddress
    }
    
    func stopWebUploader() {
        webUploader.stop()
    }
}

Haibo-Zhou avatar Dec 09 '20 09:12 Haibo-Zhou

Same issue with #525, I used the solution in https://stackoverflow.com/questions/65190065/nsnetservicebrowser-did-not-search-with-error-72008-on-ios-14/65289180#65289180 then this issue is fixed! Simply said, we need to add new permission into info.plist for iOS 14.

Haibo-Zhou avatar Dec 14 '20 12:12 Haibo-Zhou

still not work on ios 14.4.2 and xcode 12.4, I have set local network privacy and bonjourservice in info.plist, and privacy alert window showed , I accept it. but when I put the url into browser ,the index.html not shown.

yxf0103 avatar Apr 18 '21 12:04 yxf0103