当我在 swift 网络视图中单击共享按钮时重定向到 Facebook 应用程序

redirect to the facebook app when I click share button in swift webview

我的 iPhone 中有一个 WebView 应用程序,但我想在 google+ 和 Facebook 中单击共享按钮时我想在 Facebook 应用程序中打开它 *安装时,未安装时在 safari 中)但另一个链接仅从 Facebook 和 google+ \ 导入 UIKit

class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!





static let LOAD_FROM_URL = true






override func viewDidLoad() {
    super.viewDidLoad()


    if ViewController.LOAD_FROM_URL {
        // URLからロードする
        if let requestURL = NSURL(string: "http://google.com/") {  // TODO: URLを指定する
            let req = NSURLRequest(URL: requestURL)
            self.webView.loadRequest(req)
        }

    }

}






func WebView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
    // [SDK] 広告クリックの時の処理を行う
    if !WebView(webView, shouldStartLoadWithRequest: request, navigationType: navigationType) {
        return false;
    }
    return true;
 }
}

解决方案:

看看这个:

Google 上的第一个链接。或者您正在寻找不同的东西?


编辑:你问我如何让按钮触发一个事件,这里是如何:

1) 使用 ALT-CMD-ENTER 打开助手编辑器。

2) 通过 CTRL 从按钮拖动到您的代码,从您的按钮创建一个 IBAction:

3) 将必要的代码放入您的 IBAction 中,例如:

@IBAction func Fb(sender: UIButton) {

        if SLComposeViewController.isAvailableForServiceType(SLServiceTypeFacebook) {
            let fbShare:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)

            self.presentViewController(fbShare, animated: true, completion: nil)

        } else {
            let alert = UIAlertController(title: "Accounts", message: "Please login to a Facebook account to share.", preferredStyle: UIAlertControllerStyle.Alert)

            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
        }
    }