Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebViewAuthenticator and UIKit #48

Closed
2 tasks done
BoikoArtem30091994 opened this issue Jul 16, 2020 · 8 comments
Closed
2 tasks done

WebViewAuthenticator and UIKit #48

BoikoArtem30091994 opened this issue Jul 16, 2020 · 8 comments
Assignees
Labels
support Questions and uncertainties (use Discussions instead)

Comments

@BoikoArtem30091994
Copy link

BoikoArtem30091994 commented Jul 16, 2020

  • I've searched past issues and I couldn't find reference to this.
  • I've checked out all provided Examples.

Hello. Can't embed swiftagram usage in UIKit. Should the library work not in swift ui or is it not available at the moment?

@BoikoArtem30091994 BoikoArtem30091994 added the support Questions and uncertainties (use Discussions instead) label Jul 16, 2020
@sbertix
Copy link
Owner

sbertix commented Jul 17, 2020

Of course you can use it with UIKit 😊.

For instance, for logging in, you can rely on LoginViewController from the Followers example, or just build your own custom one.

import UIKit
import WebKit

import Swiftagram
import SwiftagramCrypto

class LoginViewController: UIViewController {
    /// The completion handler.
    var completion: ((Secret) -> Void)?
    /// The web view.
    var webView: WKWebView? {
        didSet {
            guard let webView = webView else { return }
            webView.frame = view.frame
            view.addSubview(webView)
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Authenticate.
        WebViewAuthenticator(storage: KeychainStorage()) { self.webView = $0 }
            .authenticate { [weak self] in
                switch $0 {
                case .failure(let error): print(error.localizedDescription)
                case .success(let secret):
                    self?.completion?(secret)
                    self?.dismiss(animated: true, completion: nil)
                }
            }
    }
}

@BoikoArtem30091994
Copy link
Author

I try this code to implement in my project. But WKWebView does not load Instagram login, you can try to create the project and set up your own login. It's not working

@sbertix sbertix closed this as completed Jul 17, 2020
@sbertix
Copy link
Owner

sbertix commented Jul 17, 2020

What version of Xcode are you on?
What Swift toolchain version are you using?
For what device are you testing it?
Cause I feel like that might be the issue there, since it does work on my part 🥺

If it gives you an error, do you mind pasting it here?
If it doesn't, would you mind showing your implementation a bit more?

@BoikoArtem30091994
Copy link
Author

Xcode 11.5. It seems to me that the problem is in the WebViewAuthenticator, because at first I built my project with UIkit and an example from Followers, and it does not work, and then using swift UI everything is fine.
No mistake just white WKWebView. If I simply create a request with example url, then everything works fine, that is the problem is not in wkwebview.

@sbertix
Copy link
Owner

sbertix commented Jul 17, 2020

I wrote a new UIKit project, added a LoginViewController like I defined it above, and tested it in the simulator, and it still works for me 😢
I'll check your code above to see if there's something missing there.


image

@BoikoArtem30091994
Copy link
Author

I wrote a new UIKit project, added a LoginViewController like I defined it above, and tested it in the simulator, and it still works for me 😢
I'll check your code above to see if there's something missing there.

image

My implementation slightly different. I will try according to your option. Thank you for your quick answer. I will give feedback.
Screenshot 2020-07-17 at 10 46 27

@BoikoArtem30091994
Copy link
Author

It's working. Thank you for your support.

@sbertix
Copy link
Owner

sbertix commented Jul 17, 2020

When you reassign the WKWebView in row 41, you're not adding the new one in the view hierarchy, so it won't display.
Basically everything between 21 and 40 is useless cause it's scrapped once you assign a new webView in 41.

I'll add auto layout constraints to LoginViewController in Followers, so people can just copy and paste it, but for now you can just do:

import UIKit
import WebKit

import Swiftagram
import SwiftagramCrypto

class LoginViewController: UIViewController {
    /// The completion handler.
    var completion: ((Secret) -> Void)?
    /// The web view.
    var webView: WKWebView? {
        didSet {
            guard let webView = webView else { return }
            webView.frame = view.frame
            webView.translatesAutoresizingMaskIntoConstraints = false
            view.addSubview(webView)
            // Add constraints.
            NSLayoutConstraint.activate([
                webView.topAnchor.constraint(equalTo: webView.topAnchor),
                webView.leadingAnchor.constraint(equalTo: webView.leadingAnchor),
                webView.trailingAnchor.constraint(equalTo: webView.trailingAnchor),
                webView.bottomAnchor.constraint(equalTo: webView.bottomAnchor)
            ])
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Authenticate.
        WebViewAuthenticator(storage: KeychainStorage()) { self.webView = $0 }
            .authenticate { [weak self] in
                switch $0 {
                case .failure(let error): print(error.localizedDescription)
                case .success(let secret):
                    self?.completion?(secret)
                    self?.dismiss(animated: true, completion: nil)
                }
            }
    }
}

@sbertix sbertix changed the title Use Swiftagram without swiftUI. WebViewAuthenticator and UIKit Jul 17, 2020
@sbertix sbertix changed the title Use Swiftagram without swiftUI. WebViewAuthenticator and UIKit Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions and uncertainties (use Discussions instead)
Projects
None yet
Development

No branches or pull requests

2 participants