Load page into webview swift 2 from java class - swift

I am developing an App for the iPhone using Xwebview which enables me to download a page then interact with the javascript on the downloaded page.
All works, but if the internet connection drops, a default local page is loaded, informing the user there is no internet connection. The page displays a retry button that, when pressed checks, the internet connection: if the connection is made the app tries to connect again to the external page and load the page into the webview.
I cannot get this to work: the code downloads the page (I can see this in my session data) but I can't get that page to load back into the webview.
override func viewDidLoad() {
super.viewDidLoad()
login()
}
func login()
{
// *********** Get stored hashkey **************
let hashcode = getHashcode()
// ********** Check network connection *********
let netConnection = Connection.isConnectedToNetwork()
print("net connection: ", netConnection)
if netConnection == true
{
if hashcode != "00000"
{
print("local key found", hashcode)
// We dont have local key
let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
//webview.loadRequest(NSURLRequest(URL: NSURL(string: "about:blank")!))
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")
let url:NSURL = NSURL(string: serverLocation + onlineLoginApi)!
let session = NSURLSession.sharedSession()
let request = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringCacheData
let paramString = "/?username=username&password=password"
request.HTTPBody = paramString.dataUsingEncoding(NSUTF8StringEncoding)
let task = session.downloadTaskWithRequest(request) {
(
let location, let response, let error) in
guard let _:NSURL = location, let _:NSURLResponse = response where error == nil else {
print("error")
return
}
let urlContents = try! NSString(contentsOfURL: location!, encoding: NSUTF8StringEncoding)
guard let _:NSString = urlContents else {
print("error")
return
}
print(urlContents)
}
task.resume()
// you must tell webview to load response
webview.loadRequest(request)
}
else{
print("local key found", hashcode)
// ********* Found local key go to site pass key over ************
let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")
let req = NSMutableURLRequest(URL: NSURL(string:serverLocation + onlineLoginApi + "?hashcode=\(hashcode)")!)
req.HTTPMethod = "POST"
req.HTTPBody = "/?hashcode=\(hashcode)".dataUsingEncoding(NSUTF8StringEncoding)
NSURLSession.sharedSession().dataTaskWithRequest(req)
{ data, response, error in
if error != nil
{
//Your HTTP request failed.
print(error!.localizedDescription)
} else {
//Your HTTP request succeeded
print(String(data: data!, encoding: NSUTF8StringEncoding))
}
}.resume()
webview.loadRequest(req)
}
}
else{
// No connection to internet
let webview = WKWebView(frame: view.frame, configuration: WKWebViewConfiguration())
view.addSubview(webview)
webview.loadPlugin(jsapi(), namespace: "jsapi")
let root = NSBundle.mainBundle().resourceURL!
let url = root.URLByAppendingPathComponent("/www/error-no-connection.html")
webview.loadFileURL(url, allowingReadAccessToURL: root)
print("No internet connection")
}
}
class jsapi: NSObject {
// Reconnect button on interface
func retryConnection()
{
print("Reconnect clicked")
dispatch_async(dispatch_get_main_queue())
{
let netConnections = Connection.isConnectedToNetwork()
if netConnections == true {
let netalert = UIAlertView(title: "Internet on line", message: nil, delegate: nil, cancelButtonTitle: "OK")
netalert.show()
let url = self.serverLocation + self.onlineLoginApi
let hashcode = ViewController().getHashcode()
if(hashcode != "00000") {
let url = url + "?hashcode=\(hashcode)"
print("url: ", url)
}
ViewController().loadPagelive(url)
}
else{
let netalert = UIAlertView(title: "Internet off line", message: nil, delegate: nil, cancelButtonTitle: "OK")
netalert.show()
}
}
print("retryConnect end")
}
}

You try to perform the loadPagelive(url) on a new instance of your ViewController, not on the current one shown on the screen, that's why you don't see any update.
You should create a delegate or a completion block in order to execute code on you ViewController instance loaded on the screen: every time you do ViewController(), a new object is created.
You can try using the delegate pattern, which is simple to achieve. I will try to focus on the important part and create something that can be used with your existing code:
class ViewController: UIViewController {
let jsapi = jsapi() // You can use only 1 instance
override func viewDidLoad() {
super.viewDidLoad()
// Set your ViewController as a delegate, so the jsapi can update it
jsapi.viewController = self
login()
}
func loadPagelive(_ url: URL) {
// Load page, probably you already have it
}
}
class jsapi: NSObject {
weak var viewController: ViewController?
func retryConnection() {
// We check if the delegate is set, otherwise it won't work
guard viewController = viewController else {
print("Error: delegate not available")
}
[... your code ...]
// We call the original (and hopefully unique) instance of ViewController
viewController.loadPagelive(url)
}
}

Related

macos swift helper app not working when launched automatically

I'm making an app that's meant to automatically log into school internet, and I'm trying to make it so that you save the credentials in the main app, and the helper app gets registered to launch at login, which works.
However, when the helper app opens from logging in, it doesn't send a web request to log into the internet, but when I open the helper app manually, it does. Keep in mind that it's meant to send a request whenever the internet status is changed to "connected"
(it's also meant to send the request every time you're connected to internet, but it only fires once)
import Cocoa
import Network
#main
class AppDelegate: NSObject, NSApplicationDelegate {
let defaults = UserDefaults.init(suiteName: "replaced userdefaults group name thing")
func applicationDidFinishLaunching(_ aNotification: Notification) {
let usernamestored = defaults!.string(forKey: "username")!
let passwordstored = String(decoding: kread(service: "detnsw-autologin", account: usernamestored)!, as: UTF8.self) // kread() refers to a function i have in another file for reading values from keychain
let url = URL(string:"the login page url")
guard let requestUrl = url else { fatalError() }
var request = URLRequest(url: requestUrl)
request.httpMethod = "POST"
let poststring = "csrfmiddlewaretoken=&username=\(usernamestored)&password=\(passwordstored)"
request.httpBody = poststring.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
if let error = error {
print("Error took place \(error)")
return
}
if let data = data, let _ = String(data: data, encoding: .utf8) {
//print("Response data string:\n \(dataString)")
//print(response.statusCode)
}
}
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
task.resume() // this is supposed to fire every time the app is connected to the internet
}
}
let queue = DispatchQueue(label: "Monitor")
monitor.start(queue: queue)
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func applicationSupportsSecureRestorableState(_ app: NSApplication) -> Bool {
return true
}
}
I've fixed the issue by putting all of the code that loads the username and password into the function that runs when the internet is connected, so the final code looks something like this, instead of the code being outside of the function so that it only loads once.
monitor.pathUpdateHandler = { path in
if path.status == .satisfied {
let usernamestored = defaults!.string(forKey: "username")!
...
let task = URLSession.shared.dataTask(with: request) { (data, response, error) in {...}
task.resume()
}
}

Download pdf file from Firebase Storage

I'm trying to link a button from storyboard to my viewcontroller code which supposed to get or download a file from Firebase Storage which is already linked to my app. but no luck.
ViewController first code
#IBAction func downloadButtonPressed(_ sender: Any) {
let userID = Auth.auth().currentUser?.uid
guard let url = URL(string: "https://console.firebase.google.com/project/rent-to-own-93ff1/storage/rent-to-own-93ff1.appspot.com/files/users/userinformation/\(userID!)/folder/Document1.pdf") else { return }
let urlSession = URLSession(configuration: .default, delegate: self, delegateQueue: OperationQueue())
let downloadTask = urlSession.downloadTask(with: url)
downloadTask.resume()
}
second code
#IBAction func downloadButtonPressed(_ sender: Any) {
let userID = Auth.auth().currentUser?.uid
let storageRef = Storage.storage().reference().child("users").child("userinformation").child(userID!).child("folder/Document1.pdf");
storageRef.downloadURL { (URL, error) -> Void in
if (error != nil) {
// Handle any errors
} else {
// Get the download URL for 'images/stars.jpg'
}
}
}
Firebase Storage
none of the codes are working for me even after following firebase steps from their website.
Also after pressing download button, the conosole shows the following
022-06-07 22:15:32.241908+0200 Rent To Own Namibia[38234:1806546] GTMSessionFetcher invoking fetch callbacks, data {length = 665, bytes = 0x7b0a2020 226b696e 64223a20 22696465 ... 7d0a2020 5d0a7d0a }, error (null)

'WeatherManagerDelegate' cannot be constructed because it has no accessible initializers I'm getting this error, when I'm trying to run my code

I'm getting this error on line var delegate = WeatherManagerDelegate()
import Foundation
protocol WeatherManagerDelegate {
func didUpdateWeather(weather:WeatherModel)
}
struct WeatherManager {
let weatherURL = "https://api.openweathermap.org/data/2.5/weather?&appid=d73ab8784f3b294976fc6189b3e6eba2&units=metric"
var delegate = WeatherManagerDelegate()
func fetchWeather(cityName: String) {
let urlString = "\(weatherURL)&q=\(cityName)"
performRequest(urlString: urlString)
}
func performRequest(urlString: String)
{
//Create URL
if let url = URL(string: urlString){
//Create a URL Session.
let session = URLSession(configuration: .default)
//Give session a task
let task = session.dataTask(with: url) { (data, response, error) in
if error != nil{ //on selecting url 2nd option the seletors you get select from data onwards to error and press enter and it will be set to closure format.
print(error!)
return
}
if let safeData = data {
/* let dataString = String(data: safeData, encoding: .utf8)
print(dataString!)*/
if let weather = parseJSON(weatherData: safeData) {
self.delegate.didUpdateWeather(weather:weather)
}
}
}
//Start task
task.resume()
}
}
func parseJSON(weatherData: Data) ->WeatherModel? {
let decoder = JSONDecoder()
do{
let decodeData = try decoder.decode(WeatherData.self, from: weatherData)
let name = decodeData.name
let temp = decodeData.main.temp
print(decodeData.main.temp_max)
print(decodeData.main.temp_min)
print(decodeData.sys.country)
print(decodeData.weather[0].description)
let id = decodeData.weather[0].id
let weather = WeatherModel(conditionId: id, cityName: name, temperature: temp)
print(weather.conditionName)
print(weather.temperatureString)
}
catch{
print(error)
return nil
}
}
}
and when I'm trying to make it an optional
var delegate = WeatherManagerDelegate?()
I'm getting this error
No exact matches in call to initializer
Replace
var delegate = WeatherManagerDelegate()
with
weak var delegate: WeatherManagerDelegate?
and update the calls to read self.delegate?.didUpdateWeather()
WeatherManager should not be responsible for creating its own delegate, that is something that should come from wherever it is begin used.
The weak attribute is almost always necessary when using delegates to avoid retain cycles.
Since weak can only be applied to class objects, you also need to indicate that in the protocol definition:
protocol WeatherManagerDelegate: AnyObject { ... }

Get user authentication before next UI View will appear

I am working on user authentication process but i stuck in the moment when reciving data from rest with token. Whenever i create the new task it does not enter on the first time into the function but after creating it skipping doing smth else which is showing a next hooked up UIViewController to segue.
My rest service with post method hashing user password, creating json, URL request and at the end creating URLSession. How could i wait for finish of this task ? To not let to do anything else before it is not complited ?
EDIT
I've added OpeartionQueue to liquidate nil's from next view.
func postLogin(name:String, pass:String, completion: #escaping (Bool) -> () ) {
let md5Data = self.MD5(string:pass)
let hashPass = md5Data!.map { String(format: "%02hhx", $0) }.joined()
let json: [String: Any] = ["username": name,
"passwordHash": hashPass ]
let jsonData = try? JSONSerialization.data(withJSONObject: json)
// create post request
let url = URL(string: LOGIN_URL)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
// insert json data to the request
request.httpBody = jsonData
request.setValue("application/json;charest=utf-8", forHTTPHeaderField: "Content-Type")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print(error?.localizedDescription ?? "No data")
return
}
let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
if let responseJSON = responseJSON as? [String: Any] {
print(responseJSON)
let message:String = responseJSON["message"] as! String
if !(message.range(of: "ERROR") != nil){
SessionMenager.Instance.token = message
completion(true)
}
} else{
print(error.debugDescription)
}
}
task.resume()
}
Then simply in my LoginViewController action with button :
#IBAction func LoginButton(_ sender: Any) {
let username = usernameTextField.text
let password = passwordTextField.text
if username == "" {
AlertWindow(title: "Username", message: "Wrong username")
} else if password == "" {
AlertWindow(title: "Password", message: "Wrong password")
} else {
let usernameToUpper = username!.uppercased()
RestService.Instance.postLogin(name: usernameToUpper, pass: password!, completion: { sth in
if sth {
OperationQueue.main.addOperation {
[weak self] in
self?.performSegue(withIdentifier: "mapSegue", sender: self)
}
} else {
return
}
})
}
}
The segue was hooked up into LoginButton which took me instantly to the next page. I've changed it into hooking up all view controllerr.
Thanks!
Because your segue is hooked up into LoginButton, it will automatically show the next viewController once you press the button.
Just hoop up the segue to the whole viewController and it should work.

Accessing variables in protocol methods in swift

can anyone please help me with this swift code, i'm a swift beginner.
i'm trying to create a login page using MVC and it looks like this enter image description here
so when the user enters their login information and the status from the JSON data is "s" for a successful login it should display some test information, and when the login status is "f" for a failed login it should stay in the same login page, i'm trying to access the status in the protocol method in the code below:
so whenever the user presses the button it should take the required action but the status in the code below is nil so whenever i run the application the information is displayed wether the login was successful or not
//this piece of code is in the LoginViewController
func didReceivedLoginInfo(info : [LoginModel]) -> String
{
self.loginData = info
print("didReceivedLOOGIN count is \(loginData!.count)")//the print is successful here
for i in 0..<loginData!.count
{
print("LoginData Info : \(loginData![i])")//the print is successful here
}
statuss = loginData![0].status
print("HERE YOU GO, THIS IS WHAT I FOUND: \(statuss!)")//the print is successful here
return statuss!
}
//this piece of code is in the LoginViewController
func buttonAction(sender:UIButton!)
{
var btnsendtag: UIButton = sender
var uName = username!.text as? String!
var uPass = password!.text as? String!
parser = LoginParser()
parser!.getLoginInfo(uName!, pass: uPass!)
var status = loginData?[0].status
print("from View Controller this is the msg: \(status)")
parser!.delegate = self
if status == "s" {
print("from View Controller this is the status: \(status)")
let vc = ViewController()
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
}
else {
let vc = LoginViewController()
let navigationController = UINavigationController(rootViewController: vc)
self.presentViewController(navigationController, animated: true, completion: nil)
}
}
////this piece of code is in the LoginParser
import UIKit
#objc protocol LoginParserDelegate : NSObjectProtocol
{
func didReceivedLoginInfo (info : [LoginModel]) -> String
optional func didRecivedError()
}
class LoginParser: NSObject {
// var webData: NSMutableData?
var webData: NSData?
weak var delegate : LoginParserDelegate?
var name = String ()
var model : LoginModel?
var alertView:UIAlertView?
var MainLogin : [LoginModel] = []
func getLoginInfo (usern : String, pass : String){
var post:NSString = "LID=\(usern)&PWD=\(pass)&Passcode=s#v#"
var url:NSURL = NSURL(string: "some link")!
var postData:NSData = post.dataUsingEncoding(NSUTF8StringEncoding)!
var postLength:NSString = String( postData.length )
var request:NSMutableURLRequest = NSMutableURLRequest(URL: url)
request.HTTPMethod = "POST"
request.HTTPBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var response: NSURLResponse?
let connection = NSURLConnection(request: request, delegate: self)
}
func connection(connection: NSURLConnection, didFailWithError error: NSError){
if delegate != nil {
if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
delegate!.didRecivedError!()
}
}
}
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse){
webData = NSData()
}
func connection(connection: NSURLConnection, didReceiveData data: NSData){
webData! = data
}
func connectionDidFinishLoading(connection: NSURLConnection){
let responseString = NSString(data: webData!, encoding: NSUTF8StringEncoding)
print("responseString \(responseString!)")
processUserTypes ()
}
func processUserTypes ()->[LoginModel]
{
model = LoginModel()
// Process JSON
do {
let result = try NSJSONSerialization.JSONObjectWithData(webData!, options: NSJSONReadingOptions.MutableContainers)as? NSDictionary
var Login = LoginModel()
let status = result?.objectForKey("Status") as? String
let msg = result?.objectForKey("En") as? String
let employeename=result?.objectForKey("DisplayName")as? String
print("STATUS IN PARSER IS \(status!)")
if status == "s"
{
print(status!)
Login.name = employeename
Login.status = status
Login.message = msg
alertView = UIAlertView()
alertView!.title = "Login successful"
alertView!.message = "Welcome \(employeename!)"
alertView!.delegate = self
alertView!.addButtonWithTitle("OK")
alertView!.show()
MainLogin.append(Login)
print("count in login is \(MainLogin.count)")
if delegate != nil
{
delegate!.didReceivedLoginInfo(MainLogin)
}
}
else
{
Login.message = msg
alertView = UIAlertView()
alertView!.title = "Login failed"
alertView!.message = msg!
alertView!.delegate = self
alertView!.addButtonWithTitle("OK")
alertView!.show()
MainLogin.append(Login)
print("this is the main login\(MainLogin)")
}
}
catch {
if delegate != nil {
if delegate!.respondsToSelector(Selector("didRecivedLoginInfo")){
delegate!.didRecivedError!()
}
}
}
return MainLogin
}
}
can anyone help me with this?
thanks
you call didReceivedLoginInfoinside processUserTypeswhich is called inside connectionDidFinishLoading. buttonAction calls getLoginInfowhich does an async request that will call back connectionDidFinishLoading when it is done.
this getLoginInfowill return a long time before the request is send out and a respone is received and connectionDidFinishLoading is called.
=> you cannot do anything after you call getLogininfo
=>
for you button method
create the parser
set the delegate
call getLoginInfo
dont do anything with potential results here
for the didReceiveLoginInfo.
you can do further logic and maybe even ui stuff there (you have to wrap logic that affects the ui in a
dispatch_async(dispatch_get_main_queue()){
//here
}
call