试图在WKWebView中重定向到url

时间:2017-01-26 10:05:33

标签: ios swift

我是iOS开发的新手,当我通过自定义网址(foo://)打开应用时,我尝试将我的应用重定向到特定网站。使用此URL可以引导我进入AppDelegate。

func application(app: UIApplication, openURL url: NSURL, options:[String: AnyObject]) -> Bool {
    let viewController = ViewController();
    viewController.loadRequestExt()
    return true
}   

调用ViewController中的loadRequestExt()函数

func loadRequestExt() {
    super.viewDidLoad()
    self.loadView()
    let url = NSURL (string: "http://www.google.com");
    let requestObj = NSURLRequest(URL: url!);
    self.webView!.loadRequest(requestObj);
    self.loadView()
}

override func loadView() {
    print("loadView")
    super.loadView()

    let contentController = WKUserContentController();
    contentController.addScriptMessageHandler(
        self,
        name: "iOS"
    )

    let config = WKWebViewConfiguration()
    config.userContentController = contentController

    self.webView = WKWebView(
        frame: self.view.frame,
        configuration: config
    )
    self.view = self.webView
    self.webView?.navigationDelegate = self

    // configure the activity view
    activityView.center = self.view.center
    activityView.hidesWhenStopped = true
    activityView.startAnimating()

    self.view.addSubview(activityView)
}

override func viewDidLoad() {
    print("viewDidLoad")

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(registerPushToken), name: "didRegisterForRemoteNotificationsWithDeviceToken", object: nil)

    let url = NSURL (string: "http://charlycares-frontend.herokuapp.com/#/app/login");

    let requestObj = NSURLRequest(URL: url!);
    self.webView!.loadRequest(requestObj);
}

它进入loadView并执行loadRequest,但它保持在同一页面上。

1 个答案:

答案 0 :(得分:0)

您必须在var text = Console.ReadLine(); while(!String.IsNullOrEmpty(text)) { TextInfo ti = CultureInfo.CurrentCulture.TextInfo; Console.WriteLine(ti.ToTitleCase(text).Replace(" ", string.Empty)); text = Console.ReadLine(); } 中将viewcontroller设置为rootviewcontroller窗口属性(我认为您没有使用故事板,在这种情况下,您必须设置appdelegate作为初始viewcontroller并且不向viewcontroller添加代码):

appdelegate

func application(app: UIApplication, openURL url: NSURL, options:[String: AnyObject]) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) self.window?.rootViewController = ViewController() self.window?.makeKeyAndVisible() return true } ViewController中,拨打viewDidLoad功能:

loadRequest