Building42 / Telegraph

Secure Web Server for iOS, tvOS and macOS

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Server route - redirect URL is not working

PrasanthGokiladas opened this issue · comments

First I thanking you for the great tutorial.
My requirement(in iOS) - I want to redirect particular url from device browser while enter the localhost url "https://localhost:9000/redirect" and it should redirect to "https://www.goggle.com" from the device browser at first launch after setup the localhost server(after launch the application). Below the point's I did;

  1. I run the application successfully
  2. Call the TelegraphDemo at ViewController.Swift
  3. The server is also start successfully and I added the code for "redirect" in "setupServer()"

the code should be in "setupServer()" method;

other code...
server.route(.GET, "redirect", serverHandleRedirect)
try! server.start(port: 9000, interface: "localhost")
...

The code in HandleRedirect;

private func serverHandleRedirect(request: HTTPRequest) -> HTTPResponse {
    let response = HTTPResponse(.temporaryRedirect)
    response.headers.location = "https://www.google.com/"
    return response
}

But the problem is, after I successfully launched the application, I am opening the default iOS device browser(Safari) and enter the localhost url like "https://localhost:9000/redirect" and now the url is not redirecting into mentioned URL, which I pointed in the HandleRequest delegate method (https://www.google.com/) . Now if we move back to our "Telegraph" application and again back to device browser(safari, and here no need to enter the localhost redirect url again) then the delegate method is trigger and successfully redirecting the "https://www.google.com/" webpage in the browser.
So my question is;

  1. Why should its not working at first time after set up the server successfully in device browser(I checked the server status using "server.isRunning" - return true)
  2. Why it's working just(back & forth) pop back to the Telegraph application(not launching once again - just opening the telegraph app which is already launched - app state as background ) and come back to device browser
  3. How should I launch the localhost url in device browser, without launching the iOS Telegraph application

Please help me to resolve this issue, past 3 day's I am trying to resolve, but still not able to find the route cause of this issue. Also I set the background task running, Allow Arbitrary Loads to YES, tried to running in main thread - starting server, but no result.

Also I tried all the possible way's to resolve as mentioned in document, but not working.

Environment:
Xcode 12.2
Install Telegraph library via - Cocoapod
Tested device: iPhone SE - OS Version (13.4.1)
iPhone 6S - OS Version (13.3.1)
iPhone X - OS Version (14.1)

NOTE: Uploaded video also for reference
https://user-images.githubusercontent.com/79190418/108175677-0ff04580-7127-11eb-883c-393915b8c5d2.mp4

Hi, sorry it took me a while to respond,

The problem you are running into is that when you send an app to the background, Apple is going to suspend it and it will no longer accept incoming connections.

Unfortunately there is no 100% solution for this, but you can usually keep the app active for about 3 minutes by starting a background task.

Here's an example of what the code would look like in the AppDelegate:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  var taskID: UIBackgroundTaskIdentifier?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    return true
  }

  func applicationDidEnterBackground(_ application: UIApplication) {
    taskID = UIApplication.shared.beginBackgroundTask(withName: "Keep-Alive") {
      if let taskID = self.taskID { UIApplication.shared.endBackgroundTask(taskID) }
      self.taskID = .invalid
    }
  }
}

See also: https://github.com/Building42/Telegraph#what-if-the-device-goes-on-standby