如何在Web视图中点击应用程序URL方案后返回应用程序

时间:2017-02-20 04:15:23

标签: ios swift oauth callback url-scheme

我希望在将应用方案作为OAuth流程后再回来。

我已经在info.plist文件

中输入了app中的URL方案
<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>com.flow.tones</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>flowTonesApp</string>
            </array>
        </dict>
    </array>

在webview / SFSafariViewController的URL中点击flowTonesApp://但是没有回到app并且没有调用方法

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool

所以请让我知道怎么做,我已经尝试了很多教程,但他们没有工作。

我已经检查过,当我尝试在模拟器或设备的Safari浏览器中点击URL时,它会打开我的应用程序,但是当我尝试在应用程序内点击时,它无效。

2 个答案:

答案 0 :(得分:0)

您可以使用UIWebView的委托方法。

    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {    
        let surl = request.URL?.absoluteString    
        if surl.contains("flowTonesApp") {    
           // RETURN TO APP
           return false
        }    
        return true
}

答案 1 :(得分:0)

针对Swift 3进行了更新:

import UIKit

class ViewController: UIViewController, UIWebViewDelegate{

    @IBOutlet weak var mWebView: UIWebView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mWebView.delegate = self
    }

    override func viewWillAppear(_ animated: Bool) {
          mWebView.loadRequest(URLRequest(url: URL(string: "https://stackoverflow.com/")!))
    }

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        print("request: \(request.description)")
        if request.description == "https://stackoverflow.com/users/login"{
            stopLoading()
            return false
        }
        return true
    }

    func stopLoading() {
        mWebView.removeFromSuperview()
        self.comeBackToApp()
    }

    func comeBackToApp()  {
        _ = self.navigationController?.popViewController(animated: true)
            //   **or Used Push for where you want to go in your app**
      /*
         vc = 
         self.storyboard?.instantiateViewController(withIdentifier: 
          "ViewController") as! ViewController
        self.navigationController?.pushViewController(vc, animated: true)
       */
    }
}