Check if UIWebView login was successful - ios

I am creating an app that would first make people login through a UIWebView to access the rest of the content of the app. More specifically they would be logging in through a web email server set through the company I work with. I want to know if it's first possible to do this, and if so how to confirm whether the user log in was successful?
For example I have the following code:
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "https://exchsvr02.companyname.com/owa/")
let request = NSURLRequest(URL: url!)
webView.loadRequest(request)
// user entered valid credentials in the login screen
// so push next page
// invalid credentials, ask user to retry, do not push screen
}

Related

Retrieve json from a website after login on webview in swift

I am working on an app which involves pulling course information from student's portal. On the user's side:
Student is brought to a webview showing the login page from university's website. (https://acorn.utoronto.ca/)
Student puts in the credentials (Utorid and password), clicks login, webview is closed, and student is brought back to the app and will see his/her courses nicely imported and arranged.
Meanwhile, the app should:
Go into user's personal portal page upon successful login and take the user back to app while process the following in background
Click on "Enrol & Manage" in the left sidebar (not sure if it is necessary, but when I view the source on my browser upon login, sometimes XHRs folder doesn't show unless click on that link)
Go to XHRs folder and find the json "enrolledCourses" (https://acorn.utoronto.ca/sws/rest/dashboard/courseRegistration/enrolledCourses)
Retrieve and parse the user's course info and make them available for the app
Source view when student just logged in
Source view when student click on "Enrol & Manage" in the left sidebar, as you can see now "XHRs" is displayed
The file "enrolledCourses" in "XHRs" which contains the course information
Here's my code for the ViewController so far:
import UIKit
import HTMLKit
import WebKit
class UtoridViewController: UIViewController, WKNavigationDelegate {
private let webView: WKWebView = {
let webView_result = WKWebView(frame: .zero)
return webView_result
}()
override func viewDidLoad() {
print("Acorn loading")
super.viewDidLoad()
view.addSubview(webView)
webView.frame = view.bounds
webView.navigationDelegate = self
// Covert URL_string into URL
guard let url = URL(string: Constants.ACORN_URL) else {
return
}
webView.load(URLRequest(url: url))
}
}
extension ViewController: WKNavigationDelegate {
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!){
}
}
So far, I can get the user to the website, but I have no idea how to proceed...
Any help or guidance will be appreciated! Thanks in advance!!!
its impossible, data on another process...
why don't you want to send an http request to the server and get the required data? if I'm not mistaken, you can take cookies from the webView and use them already for the request
there is of course an option through getting html code or executing arbitrary JS code, but this is a very unstable solution

Testing passwordless auth in Firebase test lab for iOS

I am trying to figure out how to perform e2e test via firebase test lab for iOS that allow to check passwordless authentication flow, which essentially should do following
Enters email within my app
Firebase sends auth link to such email
Somehow I need to be logged into such email somewhere in firebases test device, I assume either in mail app, or gmail?
I need to know when new email arrives and open it
Once I opened an email I need to click on auth link
This should bring me back into the app and authenticate
My biggest issue at the moment is figuring out steps that happen outside my app i.e. how can I prepare for this test and log in under my email address (is it better to log into gmail in safari for example or somehow add this acc to apples mail app?).
Testing email
In my experience, testing your own code to see if an email was sent is not straightforward beyond checking if the method call you expect to send the email has happened.
Add on top of that using Firebase, which does not expose its underlying email send code, and that looks like a challenge to me.
In terms of testing, I suggest you assert that your method calls to send email happened or that the relevant code path was reached. In Firebase web, this looks like:
firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings)
.then(function() {
// The link was successfully sent. Inform the user.
// Save the email locally so you don't need to ask the user for it again
// if they open the link on the same device.
window.localStorage.setItem('emailForSignIn', email);
// TODO save email to something accessible in your iOS tests
// TODO In your tests, confirm that email was saved after it was sent
})
.catch(function(error) {
// Some error occurred, you can inspect the code: error.code
});
See: https://firebase.google.com/docs/auth/web/email-link-auth#send_an_authentication_link_to_the_users_email_address
Another option:
You could setup a test user with an email address on a mail server that you manage, and check for incoming mail for that test user with your own custom mail reading code.
I would use Firebase Admin tools for this: https://firebase.google.com/docs/auth/admin/manage-users#create_a_user
I think you should first take a look at firebase docs for iOS on how to create dynamic links that you can use for email auth.
https://firebase.google.com/docs/auth/ios/email-link-auth
https://firebase.google.com/docs/auth/ios/passing-state-in-email-actions#configuring_firebase_dynamic_links
After you're done with those two check out the following code:
func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
// [END old_delegate]
if handlePasswordlessSignIn(withURL: url) {
return true
}
}
func handlePasswordlessSignIn(withURL url: URL) -> Bool {
let link = url.absoluteString
// [START is_signin_link]
if Auth.auth().isSignIn(withEmailLink: link) {
// [END is_signin_link]
UserDefaults.standard.set(link, forKey: "Link")
(window?.rootViewController as? UINavigationController)?.popToRootViewController(animated: false)
window?.rootViewController?.children[0].performSegue(withIdentifier: "passwordless", sender: nil)
return true
}
return false
}
This is just an example on how you can handle the deep link in your app after the user taps the link. The delegate method
func application(_ application: UIApplication, open url: URL,
sourceApplication: String?, annotation: Any) -> Bool
in AppDelegate is used for all deep links into an app. You could set up for example your own scheme that your app conforms to. And you can send url type links with your custom scheme into your app from the browser for example.
To do this just Open Xcode, go to Project Settings -> Info, and add inside ‘The URL Types” section a new URL scheme. Add something of the sort of com.myApp in order for it to be as unizue as possible. Then you can just type into a browser com.myApp://main and handle that in the appDelegate.
Edit: It says so in their docs that you can present a prompt inside the app for the user to input the email. Where the user opens his email from isn't really your concern as long as your dynamic link is set up properly.

How do I report error messages from PayPal iOS SDK to users?

The app I am administering has used the PayPal SDK for iOS, and this works fine for the most part, except that if a user tries to login and gets an error, the error is not reported to the user.
For example, if I use incorrect login credentials, I'll get this error message:
2018-07-12 10:21:28.316449-0400 AppName[52601:6030601] PayPal SDK: Request has failed with error: invalid_user - Incorrect username/password. Please try again. (401) | PayPal Debug-ID: a1f66774362f, a1f66774362f [live, PayPal iOS SDK 2.17.0] | Details: (
Ultimately, when I try to login with these credentials, it gets as far as this function, and then seems to go no further.
func segueToConfigurePaypal(sender: UIButton?) {
print("DEBUG Segue to configure paypal")
let scopeValues: Set = [kPayPalOAuth2ScopeOpenId, kPayPalOAuth2ScopeEmail, kPayPalOAuth2ScopeAddress]
let config = PayPalConfiguration()
config.merchantName = "example"
config.merchantPrivacyPolicyURL = URL(string: "http://www.example.com/privacy")
config.merchantUserAgreementURL = URL(string: "http://www.example.com/terms")
let vc = PayPalProfileSharingViewController.init(scopeValues: scopeValues, configuration: config, delegate: self)
self.present(vc!, animated: true, completion: nil)
}
All of which connects to the SDK.
I am wondering if there's a way to make an event listener that will let me know if there's been a problem with the user logging in. This way a user can have some idea about why they're having trouble rather than thinking it is an issue with the app.
Basically I want an alert on user login issues, that reports the issue to the user.

Instagram don't redirect to localhost in Swift but does in navigator

A week ago I had my Instagram login system working really fine. I don't know what happened (recent limitation in iOS or change something with Instagram API...) but I'm not able to login.
In my Instagram application, I have two URIs saved: http://localhost and http://localhost/
I've got my function to make the login. It's something like this:
func instagramLogIn()->Void{
webViewOutlet.loadRequest(URLRequest(url: URL(string: "https://api.instagram.com/oauth/authorize/?client_id=MY_CLIENT_ID&redirect_uri=http://localhost&response_type=token")!))
}
In run this method in viewDidAppear where Instagram login appears:
override func viewDidAppear(_ animated: Bool) {
instagramLogIn()
}
Of course webView outlet's delegate is set in self so I can listen to every change of the webView. So I do this now:
func webViewDidFinishLoad(_ webView: UIWebView){
let result = webView.request!.mainDocumentURL
print(result)
let result2:URL = URL(string: "/", relativeTo: result)!.absoluteURL
if result2.absoluteString == "http://localhost/"{
This is supposed to be run every time a request ends loading so the first method should redirect to http://localhost/#authToken=MY_AUTH_TOKEN. So in webViewDidFinishLoad() I check if the main URL is localhost and if so I keep doing things. I print "result" and "result2" and what I get is the first url to load (https://api.instagram.com/oauth/authorize/?client_id=MY_CLIENT_ID&redirect_uri=http://localhost&response_type=token) and "result2" prints "http://www.instagram.com".
It's not redirecting to http://localhost. A days ago it did.
What happen if I open the first url in Safari (in my Mac, not iOS)? It redirects perfectly and I get my auth_token.
So the question is: why is not redirecting in iOS? In Mac I've already an open session.
I don't know what's wrong with this. A couple or days it worked fine.

swift 2.0 UIWebview displays white page only

I write this code:
#IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL (string: "http://google.com");
let requestObj = NSURLRequest(URL: url!);
webView.loadRequest(requestObj);
self.view.addSubview(webView)
}
run the code, iPhone simulator display white page only but no warning error
and I change the code to:
let url = NSURL (string: "http://apple.com");
just change google to apple, then it works - simulator displayed apple site
So I put the other site url but it's same like first one, only it displays apple.com
I don't know what is wrong about this code please help me! thank you :)
You should get the error:
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
Find Info as the image below shows and do the steps:
1.On the last line add the +
2.Enter the name of the group: App Transport Security
3.Right click on the group and select Add Row
4.Enter Allow Arbitrary Loads
5.Set the value on the right to YES
no need this line if you have connect it from storyboard
self.view.addSubview(webView)
App Transport Security has prevented your web view from loading the page over http. You can either disable it, per William Hu's answer, or switch over to using https links (which is the recommended option).

Resources