2

I'm using the code below to show my website in a webview added ?app=ios parameter to the end of the url. But now I want this parameter to be added in all urls on the website. I need to append this somewhere before loading every page. Please guide me through this as I'm a very newbie to swift. Thanks

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate, WKUIDelegate {

@IBOutlet var container: UIView!

var webView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    webView = WKWebView()
    container.addSubview(webView)

    self.webView.navigationDelegate = self
    self.webView.uiDelegate = self
 }

    let urlStr = "https://www.website.com/?app=ios"
    let url = NSURL(string: urlStr)!
    let req = NSURLRequest(url: url as URL)

    webView.load(req as URLRequest)

AppDelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

Note: I've checked almost every topic related to this on the website but couldn't find an answer to my need.

Mert Alnuaimi
  • 342
  • 8
  • 17
  • Where would you like to add the `?app=ios` ? All URLs in your iOS app or all URLs inside `WKWebView` and generated from web? – Eric Zhang Mar 30 '17 at 18:45
  • @EricZhang Hi thanks for your response. I want ?app=ios to be added on every page we surf on our website within the wkwebview application. This parameter to be always appended to the url ending. – Mert Alnuaimi Mar 30 '17 at 18:54
  • I don't think Apple allows developer to modify URL inside `WKWebView`. Let's say the user opens a `WKWebView` and goes to Google.com. If I can change the URLs in the web view, I would change them all to my personal website or AD. I would be very rich because of the advertising revenue. Lol – Eric Zhang Mar 30 '17 at 19:04
  • he can control every click of the user and also redirect as needed. http://stackoverflow.com/questions/36231061/wkwebview-open-links-from-certain-domain-in-safari/36231713#36231713 – Leo Dabus Mar 30 '17 at 19:06
  • @UmidAlnuaimi Whats the purpose of such approach? I think you should just change the user agent to always display the pages in the mobile format – Leo Dabus Mar 30 '17 at 19:20
  • @LeoDabus Hi thanks for your response. Looking at the ?app=ios parameter we are hiding appstore and googe play icons from the footer of the website when viewed from a webview application. But since this parameter doesn't follow on every page, these icons keep showing and its a bad experience while you're already inside the application the website is telling you to download the app :) – Mert Alnuaimi Mar 31 '17 at 06:25

1 Answers1

5

NSURLComponents is a very handy class for parsing and changing URLs.

This should do what you want:

import Cocoa

func addQueryParams(url: URL, newParams: [URLQueryItem]) -> URL? {
    let urlComponents = NSURLComponents.init(url: url, resolvingAgainstBaseURL: false)
    guard urlComponents != nil else { return nil; }
    if (urlComponents?.queryItems == nil) {
        urlComponents!.queryItems = [];
    }
    urlComponents!.queryItems!.append(contentsOf: newParams);
    return urlComponents?.url;
}

let url = URL.init(string: "https://developer.apple.com/documentation/foundation/nsurlcomponents/1416476-init");
if (url != nil) {
    print(addQueryParams(url: url!, newParams: [URLQueryItem.init(name: "a", value: "b")])!);
}
opyh
  • 1,494
  • 14
  • 19
  • Thank you so much just what I was looking for. I restarted working on the project and this news just came in like motivation (: – Mert Alnuaimi Aug 21 '17 at 05:54