2017-01-26 68 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()