[GH-ISSUE #406] Facebook example #261

Closed
opened 2026-03-03 16:47:09 +03:00 by kerem · 11 comments
Owner

Originally created by @dtomicAZ on GitHub (Sep 4, 2017).
Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/406

Description:

I have problem with Facebook login using OAuthSwift.
I've found this "Handle a specific URL into delegate":
https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL

I don't know how to setup internal webview, can someone give me step by step, or some example, thank you.

OAuth Provider

Facebook

OAuth Version:

Version 2

OS (Please fill the version) :

iOS :

Installation method:

CocoaPods

Library version:

v1.1.0

Xcode version:

8.3.3 (Swift 3.1)

Originally created by @dtomicAZ on GitHub (Sep 4, 2017). Original GitHub issue: https://github.com/OAuthSwift/OAuthSwift/issues/406 ### Description: I have problem with Facebook login using OAuthSwift. I've found this "Handle a specific URL into delegate": https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL I don't know how to setup internal webview, can someone give me step by step, or some example, thank you. ### OAuth Provider Facebook ### OAuth Version: Version 2 ### OS (Please fill the version) : iOS : ### Installation method: CocoaPods ### Library version: v1.1.0 ### Xcode version: 8.3.3 (Swift 3.1)
kerem 2026-03-03 16:47:09 +03:00
  • closed this issue
  • added the
    question
    label
Author
Owner

@phimage commented on GitHub (Sep 4, 2017):

You can extends OAuthWebViewController
and set oauthswift.authorizeURLHandler=...

in demo there is an example of web view controller
https://github.com/OAuthSwift/OAuthSwift/blob/master/Demo/Common/WebViewController.swift

<!-- gh-comment-id:326884546 --> @phimage commented on GitHub (Sep 4, 2017): You can extends `OAuthWebViewController` and set `oauthswift.authorizeURLHandler=...` in demo there is an example of web view controller https://github.com/OAuthSwift/OAuthSwift/blob/master/Demo/Common/WebViewController.swift
Author
Owner

@dtomicAZ commented on GitHub (Sep 4, 2017):

@phimage Thank you for fast respond.

Please if you can tell me what modifications in WebViewController I need make ?

My URL_scheme name is autoworld.

Also what should be my callback url in authorize method ?

<!-- gh-comment-id:326888905 --> @dtomicAZ commented on GitHub (Sep 4, 2017): @phimage Thank you for fast respond. Please if you can tell me what modifications in WebViewController I need make ? My URL_scheme name is autoworld. Also what should be my callback url in authorize method ?
Author
Owner

@phimage commented on GitHub (Sep 4, 2017):

If you use a web view delegate you never use a custom url scheme

If you read this page https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL it's because the callback url must an HTTP one, not a custom one

You can see in the page YOUR_URL, you can put any http url that facebook allow
https://autoworld/callback

<!-- gh-comment-id:326995553 --> @phimage commented on GitHub (Sep 4, 2017): If you use a web view delegate you never use a custom url scheme If you read this page https://github.com/OAuthSwift/OAuthSwift/wiki/API-with-only-HTTP-scheme-into-callback-URL it's because the callback url must an HTTP one, not a custom one You can see in the page `YOUR_URL`, you can put any http url that facebook allow https://autoworld/callback
Author
Owner

@dtomicAZ commented on GitHub (Sep 5, 2017):

@phimage Thank you for help.

I have another problems.

Here is my code:

func doOAuthFacebook() {
let oauthswift = OAuth2Swift(
consumerKey: "XXXXX",
consumerSecret: "XXXXX",
authorizeUrl: "https://www.facebook.com/dialog/oauth",
accessTokenUrl: "https://graph.facebook.com/oauth/access_token",
responseType: "code"
)

let webVC = OAuthFBWebViewController()
oauthswift.authorizeURLHandler = webVC
let state = generateState(withLength: 20)
let _ = oauthswift.authorize(
  withCallbackURL: URL(string: "https://autoworld.org/callback")!, scope: "public_profile", state: state,
  success: { credential, response, parameters in
    print("credential \(credential)")
    print("credential \(String(describing: response))")
    print("credential \(parameters)")
}, failure: { error in
  print(error.localizedDescription, terminator: "")
}
)

}

I got this message in webview:
Can't Load ULR: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.

I tried add autoworld.org domain in the Apps domain field on Facebook panel, but I got this error:

App domains must match the domain of the Facebook Web Games URL (https), Mobile Site URL, Unity Binary URL, Site URL or Secure Page Tab URL. Please correct these domains: autoworld.org

What should I do ? Thank you for your time.

<!-- gh-comment-id:327086828 --> @dtomicAZ commented on GitHub (Sep 5, 2017): @phimage Thank you for help. I have another problems. Here is my code: func doOAuthFacebook() { let oauthswift = OAuth2Swift( consumerKey: "XXXXX", consumerSecret: "XXXXX", authorizeUrl: "https://www.facebook.com/dialog/oauth", accessTokenUrl: "https://graph.facebook.com/oauth/access_token", responseType: "code" ) let webVC = OAuthFBWebViewController() oauthswift.authorizeURLHandler = webVC let state = generateState(withLength: 20) let _ = oauthswift.authorize( withCallbackURL: URL(string: "https://autoworld.org/callback")!, scope: "public_profile", state: state, success: { credential, response, parameters in print("credential \(credential)") print("credential \(String(describing: response))") print("credential \(parameters)") }, failure: { error in print(error.localizedDescription, terminator: "") } ) } I got this message in webview: Can't Load ULR: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings. I tried add autoworld.org domain in the Apps domain field on Facebook panel, but I got this error: App domains must match the domain of the Facebook Web Games URL (https), Mobile Site URL, Unity Binary URL, Site URL or Secure Page Tab URL. Please correct these domains: autoworld.org What should I do ? Thank you for your time.
Author
Owner

@phimage commented on GitHub (Sep 5, 2017):

add https://autoworld.org


Then do not do that

func doOAuthFacebook() {
let oauthswift = OAuth2Swift(

oauthswift object will be released from memory, put the object into class variable

func doOAuthFacebook() {
self.oauthswift = OAuth2Swift(
<!-- gh-comment-id:327090521 --> @phimage commented on GitHub (Sep 5, 2017): add https://autoworld.org --- Then do not do that ``` func doOAuthFacebook() { let oauthswift = OAuth2Swift( ``` oauthswift object will be released from memory, put the object into class variable ``` func doOAuthFacebook() { self.oauthswift = OAuth2Swift( ```
Author
Owner

@dtomicAZ commented on GitHub (Sep 5, 2017):

@phimage

I tried to add https://autoworld.org but still Facebook give me same error:

App domains must match the domain of the Facebook Web Games URL (https), Mobile Site URL, Unity Binary URL, Site URL or Secure Page Tab URL. Please correct these domains: autoworld.org

<!-- gh-comment-id:327091567 --> @dtomicAZ commented on GitHub (Sep 5, 2017): @phimage I tried to add https://autoworld.org but still Facebook give me same error: App domains must match the domain of the Facebook Web Games URL (https), Mobile Site URL, Unity Binary URL, Site URL or Secure Page Tab URL. Please correct these domains: autoworld.org
Author
Owner

@phimage commented on GitHub (Sep 5, 2017):

please make screenshot
and give facebook documentation link about configuring the http callback url

<!-- gh-comment-id:327113184 --> @phimage commented on GitHub (Sep 5, 2017): please make screenshot and give facebook documentation link about configuring the http callback url
Author
Owner

@dtomicAZ commented on GitHub (Sep 5, 2017):

@phimage

It'l looks like I had to add website platform. After that I am allowed to set App Domains.
Now that is ok.

I get this screen in mobile app:
simulator screen shot 5 sep 2017 12 07 43

There is Cookies header on top, and there is no Done button.
Should it be like this ?
Do I need to add manually navigation bar so User can cancel this process?

<!-- gh-comment-id:327132801 --> @dtomicAZ commented on GitHub (Sep 5, 2017): @phimage It'l looks like I had to add website platform. After that I am allowed to set App Domains. Now that is ok. I get this screen in mobile app: ![simulator screen shot 5 sep 2017 12 07 43](https://user-images.githubusercontent.com/29227349/30056462-196e54d4-9233-11e7-92bb-f2a48d40016a.png) There is Cookies header on top, and there is no Done button. Should it be like this ? Do I need to add manually navigation bar so User can cancel this process?
Author
Owner

@dtomicAZ commented on GitHub (Sep 5, 2017):

@phimage

This is now working.

This is my viewDidLoad of my OAuthFBWebViewController which extends OAuthWebViewController:

override func viewDidLoad() {
super.viewDidLoad()

let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 50))
let navigationItem = UINavigationItem(title: "")
let cancelBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(cancelClicked))

navigationItem.leftBarButtonItem = cancelBtn
navigationBar.setItems([navigationItem], animated: false)
self.view.addSubview(navigationBar)

self.webView.frame = CGRect(x: 0, y: navigationBar.bounds.maxY, width: view.bounds.width, height: view.bounds.height - navigationBar.bounds.maxY)
self.webView.scalesPageToFit = true
self.webView.delegate = self
self.view.addSubview(self.webView)
loadAddressURL()

}

func cancelClicked() {
self.dismissWebViewController()
}

Also I added this, so cookie header is not visible:

func webViewDidFinishLoad(_ webView: UIWebView) {
webView.stringByEvaluatingJavaScript(from: "document.getElementById('header-notices').style.display = 'none';")
}

Just one more question.
Is drawback of this Library that It can't recognise if user is already logged in Facebook with some of this methods (I mean with this Lib user will have to always input credentials):
-with native Facebook app,
-through Settings app,

  • with Safari.
<!-- gh-comment-id:327138221 --> @dtomicAZ commented on GitHub (Sep 5, 2017): @phimage This is now working. This is my viewDidLoad of my OAuthFBWebViewController which extends OAuthWebViewController: override func viewDidLoad() { super.viewDidLoad() let navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: view.bounds.width, height: 50)) let navigationItem = UINavigationItem(title: "") let cancelBtn = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(cancelClicked)) navigationItem.leftBarButtonItem = cancelBtn navigationBar.setItems([navigationItem], animated: false) self.view.addSubview(navigationBar) self.webView.frame = CGRect(x: 0, y: navigationBar.bounds.maxY, width: view.bounds.width, height: view.bounds.height - navigationBar.bounds.maxY) self.webView.scalesPageToFit = true self.webView.delegate = self self.view.addSubview(self.webView) loadAddressURL() } func cancelClicked() { self.dismissWebViewController() } Also I added this, so cookie header is not visible: func webViewDidFinishLoad(_ webView: UIWebView) { webView.stringByEvaluatingJavaScript(from: "document.getElementById('header-notices').style.display = 'none';") } Just one more question. Is drawback of this Library that It can't recognise if user is already logged in Facebook with some of this methods (I mean with this Lib user will have to always input credentials): -with native Facebook app, -through Settings app, - with Safari.
Author
Owner

@phimage commented on GitHub (Sep 5, 2017):

You can store the credential for next run
https://github.com/OAuthSwift/OAuthSwift/wiki/Store-credential

You cannot access info from settings or facebook app
for facebook app you must use facebook sdk or do some stuff with x-callbackurl

<!-- gh-comment-id:327146687 --> @phimage commented on GitHub (Sep 5, 2017): You can store the credential for next run https://github.com/OAuthSwift/OAuthSwift/wiki/Store-credential You cannot access info from settings or facebook app for facebook app you must use facebook sdk or do some stuff with x-callbackurl
Author
Owner

@dtomicAZ commented on GitHub (Sep 5, 2017):

@phimage

Ok, Thank you for your help.

<!-- gh-comment-id:327147145 --> @dtomicAZ commented on GitHub (Sep 5, 2017): @phimage Ok, Thank you for your help.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/OAuthSwift#261
No description provided.