Ton Tonii Ton Tonii - 2 years ago 230
Swift Question

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

i have a WebView app in my iPhone but i want when i click in share button in google+ and Facebook i would like to open it in Facebook app *when installed,when not in safari )but another links to open all in web view just from Facebook and google+ \
import UIKit

class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!

static let LOAD_FROM_URL = true

override func viewDidLoad() {

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



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

Answer Source


Have a look at this: How to open fb and instagram app by tapping on button in Swift

1st links on Google. Or are you looking for something different ?

EDIT: You asked me how to make the button trigger an event, here' how:

1) Open the Assistant editor by using ALT-CMD-ENTER.

2) Create an IBAction from your button by CTRL dragging from the button to your code:

enter image description here

3) Put the necessary code in your IBAction, for example:

@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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download