Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect my iOS App as a web browser

I'm building a browser app. I want iOS to detect it as a browser. I tried adding http to URL Schemes but in vain.

I tried this answer.

like image 596
Vaibhav Jhaveri Avatar asked Sep 30 '16 06:09

Vaibhav Jhaveri


3 Answers

In iOS, the OS does not keep track of which apps are browsers and as far as the system is concerned, the user should use Safari for everything. There is therefore no API similar to the question you linked, which refers to macOS.

However, if you want your app to show up in the "Open In" pop-up, you can set your app as able to open certain document types. There are lots of system-declared document types, but you may want to take special note of the web-related ones like public.html (kUTTypeHTML).

Unfortunately, I'm fairly certain that iOS does not allow apps to declare support for opening plain http URLs, only custom URL schemes, but you still may want to declare support for certain file types such as HTML, just in case the user tries to open one. Here's an example:

First, you need to tell Xcode which documents you support, you do that by selecting your target, going to info, and adding Document Types:

Adding public.html to Document Types

Then, anytime your app is asked to open a .webarchive file, This document describes where you must open it from. Basically, because you do not own the file type you can just look inside the options dictionary in either application:didFinishLaunchingWithOptions: or application:willFinishLaunchingWithOptions.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

    if let url = launchOptions?[UIApplicationLaunchOptionsKey.url] as? URL {
        // This code would probably exist in one of your controllers
        let request = URLRequest(url: url)
        webview.load(request)
    }

    return true
}

So in reality, the AppDelegate would most likely delegate this to another portion of your app by simply passing it the URL.

But again, the user still probably won't be able to just go to Safari and just share a webpage to your app. Your app would likely only show when the user actually hits share on an actual html file, but it's probably the closest thing iOS offers to what you want.

like image 107
Matthew Seaman Avatar answered Nov 01 '22 10:11

Matthew Seaman


Not possible to define anywhere in app so other app see your app as browser, but if you want to bring URLs from other app to your app You can create App Extension of Type Share for your app. This will allow other apps to share URL with your app. When user will select URL and share, they will see your app. You can customise view of share view for your class from NSExtensionMainStoryboard . You can read more about it from Apple's Developer website

However, This will not open your app when user will select URL. user will have to close other app and open your app to view shared URL in your app. for this, you can display proper message from SharingViewController class.

Whenever user will share URL with your app, didSelectPost() delegate method in your SharingViewController will be called , you can get shared URL from this method and use that in your app.

    override func didSelectPost() {
        if let item = extensionContext?.inputItems.first as? NSExtensionItem {
            if let itemProvider = item.attachments?.first as? NSItemProvider {
                if itemProvider.hasItemConformingToTypeIdentifier(kUTTypeURL) {
                    itemProvider.loadItemForTypeIdentifier(kUTTypeURL, options: nil, completionHandler: { (url, error) -> Void in
                        if let shareURL = url as? NSURL {
                            var defaults = NSUserDefaults(suiteName: "group.com.yourapp.extension")
                            defaults?.setObject(url.absoluteString(), forKey: "sharedURL")
                            defaults?.synchronize()
                        }
                        self.extensionContext?.completeRequestReturningItems([], completionHandler:nil)
                    })
                }
            }
        }
    }

You can get value store in NSUserDefalts with suit name in your app and display URL from it .

    var defaults = NSUserDefaults(suiteName: "group.com.yourapp.extension")
    defaults?.synchronize()

    // Check for null value before setting
    if let restoredValue = defaults!.stringForKey("sharedURL") {

        //create url with sctring and use it as per your requirement
    }
    else {
      // ("Cannot find value")
    }
like image 27
PlusInfosys Avatar answered Nov 01 '22 11:11

PlusInfosys


This can not be done, http is a reserved URL scheme, see Using URL Schemes to Communicate with Apps

Apple provides built-in support for the http, mailto, tel, and sms URL schemes among others. It also supports http–based URLs targeted at the Maps, YouTube, and iPod apps. The handlers for these schemes are fixed and cannot be changed. If your URL type includes a scheme that is identical to one defined by Apple, the Apple-provided app is launched instead of your app.

What you can do is using Universal Links with your app and use your app (if installed) as browser for your web site. You can not achieve this for all URLs, though.

like image 21
dogsgod Avatar answered Nov 01 '22 10:11

dogsgod