2017-01-26 35 views
0

我是iOS新手,我尝试通过自定义URL(foo://)打开应用程序时将应用程序重定向到特定网站。使用这个URL将我带到AppDelegate的这里。尝试重定向到WKWebView中的url

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

这就要求在视图控制器

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的loadRequestExt()功能并执行loadRequest,但它停留在同一页面上。

回答

0

必须设定viewcontrollerappdelegate窗口财产rootviewcontroller(我想你没有使用故事板,在这种情况下,你必须设置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 
} 

在你ViewControllerviewDidLoad,请致电loadRequest功能:

self.loadRequestExt()