2017-01-26 12 views
0

私はiOS開発が新しく、カスタムURL(foo://)でアプリを開いたときに特定のウェブサイトにリダイレクトしようとしています。このURLを使用するとAppDelegateでここに来ます。それはloadViewメソッドに入るとloadRequestを実行ViewControllerをWKWebViewのURLにリダイレクトしようとしています

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); 
} 

loadRequestExt()関数を呼び出す

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

、しかし、それは同じページにとどまります。

答えて

0

あなたは(私はあなたがストーリーボードを使用していないと仮定し、その場合には、あなたが最初のviewcontrollerとしてあなたviewcontrollerを設定する必要がありますし、appdelegateにコードを追加していない)appdelegateであなたのviewcontroller windowプロパティのrootviewcontrollerように設定する必要があります。

あなたの ViewController viewDidLoad、あなたの呼び出し loadRequest機能で
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 
} 

self.loadRequestExt() 
関連する問題