```webAuthSession.presentationContextProvider = self``` ERROR: cannot find self in scope - swift

I am trying to implement a web authentication service in my app however, when I try to put webAuthSession.presentationContextProvider = self , I get an error saying Cannot find 'self' in scope. All the turorials I have followed did the same thing without any errors. I am truly desperate so any help would be appreciated.
Thank you very much in advance!
import Foundation
import AuthenticationServices
import CryptoKit
typealias ASPresentationAnchor = UIWindow
class SignInViewModel: NSObject, ObservableObject, ASWebAuthenticationPresentationContextProviding {
func presentationAnchor(for session: ASWebAuthenticationSession) -> ASPresentationAnchor {
return ASPresentationAnchor()
}
}
public func UrlGenerator() -> URL {
func randomString(length: Int) -> String {
let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
return String((0..<length).map{ _ in letters.randomElement()! })
}
let code_verifier = randomString(length: 86)
let hashdata = Data(code_verifier.utf8)
let code_challenge_data = SHA256.hash(data: hashdata)
let code_challenge_generated = code_challenge_data.compactMap { String(format: "%02x", $0) }.joined()
let url = "auth.tesla.com/oauth2/v3/authorize"
let client_id = "ownerapi"
let code_challenge = "\(code_challenge_generated)"
let code_challenge_method = "S256"
let redirect_uri = "https://auth.tesla.com/void/callback"
let response_type = "code"
let scope = "openid+email+offline_access"
let state = "\(code_verifier)"
let TeslaLink = "\(url)?client_id=\(client_id)&code_challenge=\(code_challenge)&code_challenge_method=\(code_challenge_method)&redirect_uri=\(redirect_uri)&response_type=\(response_type)&scope=\(scope)&state=\(state)"
let TeslaLinkURL = URL(string: TeslaLink)!
return TeslaLinkURL
}
let webAuthSession = ASWebAuthenticationSession.init(
url: UrlGenerator(),
callbackURLScheme: "TESRIUM",
completionHandler: { (callback:URL?, error:Error?) in
})
public func TeslaSignIn() {
webAuthSession.presentationContextProvider = self
webAuthSession.prefersEphemeralWebBrowserSession = false
webAuthSession.start()
}

To answer your question, the reason self is not available is because you're referring to it in the global context and self has no meaning in the global context. self must refer to an object and self in the global scope is an environment, not an object.
The reason your specific code doesn't work is because you've separated the instantiation of webAuthSession from the work you perform on it. In the tutorial, webAuthSession = ASWebAuthenticationSession.init... and webAuthSession?.presentationContextProvider = self are in the same scope. Therefore, simply merge them together into the same function. Now, where that function is also matters because if it's a global function you'll have the same problem. You need to place this function into the right object so self is referring to what it's supposed to refer to.
public func TeslaSignIn() {
let webAuthSession = ASWebAuthenticationSession.init(
url: UrlGenerator(),
callbackURLScheme: "TESRIUM",
completionHandler: { (callback:URL?, error:Error?) in
})
webAuthSession.presentationContextProvider = self
webAuthSession.prefersEphemeralWebBrowserSession = false
webAuthSession.start()
}

Related

Assign value to Class Member within Function

I have a class that has two member variables and a function. The function is called when the user navigates to a new storyboard and uses a http GET request with member variable y to assign a value to member variable x. After the function is finished, I try to assign x to the new storyboard's variable, however it is nil. How do I assign a value to x within the function and then pass x to the new storyboard?
import UIKit
import os.log
class testViewController: UIViewController {
var x: XClass!
var y = “1234”
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
switch(segue.identifier ?? "") {
case “ZViewController”:
guard let zviewcontroller = segue.destination as? ZViewController else{
fatalError("Unexpected Destination: \(segue.destination)")
}
loadXDetail()
zviewcontroller.x = x
default:
os_log("Not ZViewController Segue", log: OSLog.default, type: .debug)
}
}
private func loadX(){
// credentials encoded in base64
let username = “***”
let password = “***”
let loginData = String(format: "%#:%#", username, password).data(using: String.Encoding.utf8)!
let base64LoginData = loginData.base64EncodedString()
// create the request
let url = URL(string: "https://example.com")!
var request = URLRequest(url: url)
request.httpMethod = "GET"
request.setValue("Basic \(base64LoginData)", forHTTPHeaderField: "Authorization")
//making the request
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("\(error)")
return
}
if let httpStatus = response as? HTTPURLResponse {
// check status code returned by the http server
print("status code = \(httpStatus.statusCode)")
do {
let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as! [String:Any]
let items = json["items"] as? [[String: Any]] ?? []
for dic in items{
//guard let api = dic["Type"] as? String else {return}
let a = dic[“A”] as! String
let b = dic[“B”] as! String
let c = dic[“C”] as! String
self.x = XClass(y: y, a: a, b: b, c: c)!
}
} catch let error as NSError {
print(error)
return
}
}
}
task.resume()
}
}
You can't prevent a segue inside prepare(for segue until your request finishes , you need to start the call to the asynchonous method when you need to navigate
loadX()
then inside the completion of let task = URLSession.shared.dataTask(with: request) do
self.x = XClass(y: y, a: a, b: b, c: c)!
}
DispatchQueue.main.async {
self.performSegue(withIdentifier:"SegueID",sender:nil)
}
and at that moment the x var has a correct value according to the response
another thing you may need x as any array as it will contain the last value from this loop
for dic in items{
//guard let api = dic["Type"] as? String else {return}
let a = dic[“A”] as! String
let b = dic[“B”] as! String
let c = dic[“C”] as! String
self.x = XClass(y: y, a: a, b: b, c: c)!
}
plus consider using Codable to decode your response instead of JSONSerialization
swift 4.2 / Xcode 10.1:
There are few ways to pass data between viewControllers or classes.
The easiest one is using Global variable. For Example:
import UIKit
var myGlobalVariable = "" //Or any Type you need
class testViewController: UIViewController {
private func loadX(){
//Do some stuff ...
myGlobalVariable = x
}
import UIKit
class ZViewController: UIViewController {
override func viewDidLoad() {
print(myGlobalVariable)
}
Or, using singleton pattern. For Example:
Create a class like:
import Foundation
class SetVariable {
var test: String? //Or any type you need
private init () {}
static let shared = SetVariable.init()
}
class testViewController: UIViewController {
private func loadX(){
//Do some stuff ...
SetVariable.shared.test = x
}
class ZViewController: UIViewController {
override func viewDidLoad() {
print(SetVariable.shared.test)
}

Swift execute functions sequentially

I have the below code which obtains the access token from a facebook login and returns it as 'accessToken'. Once I have the access token I wish to pass this to my server in a request and return the response as an array.
The issue I have is that the request to the server executes before the accessToken is obtained. I have looked into closure statements but I cannot see a way where I can order the execution of the functions without ending up nesting. I don't mind nesting in this instance but in future if I have say 5 functions this will begin to look messy.
Am I approaching this in the best way by using classes and functions? Usually when I code in swift all the code relevant to the viewController would be contained in 1 file, but as the project gets larger I am looking to implement a more OOP approach to make the project more manageable. How would I best achieve this?
import Foundation
import UIKit
class registrationPage: UIViewController {
#IBAction func facebookButton(_ sender: Any) {
// Get the access token from facebook
let accessToken = facebookLogin().login()
// Get the users currency, langage and locale.
let currency = Locale.current.currencyCode ?? "GBP"
let language = Locale.current.languageCode ?? "GB"
let region = Locale.current.regionCode ?? "GB"
let params = "accessToken=\(accessToken)&currency=\(currency)&language=\(language)&region=\(region)"
let resultArray = database().connect(endPoint: "loginfb.php?", params: "\(params)")
print(resultArray)
}
}
class facebookLogin {
var response = ""
func login(completion: (_ result: String) -> Void) {
let loginManager = LoginManager()
loginManager.logIn(readPermissions:[ReadPermission.publicProfile, ReadPermission.email], viewController: registrationPage() as UIViewController) {
loginResult in switch loginResult {
case .failed:
self.response = "ERROR"
case .cancelled:
self.response = "ERROR"
case .success:
self.response = "\(String(describing: FBSDKAccessToken.current().tokenString!))"
print(self.response)
}
}
completion(self.response)
}
}
loginManager.logIn is asynchronous, thats why it takes a closure. You can synchronize the call or as you said use nested closures where one calls the next.
To make let accessToken = facebookLogin().login() synchronous with DispatchGroup:
class facebookLogin {
func login() -> String {
let loginManager = LoginManager()
var response = ""
let group = DispatchGroup()
group.enter() // loginManager.logIn
loginManager.logIn(readPermissions:[ReadPermission.publicProfile, ReadPermission.email], viewController: registrationPage() as UIViewController) {
loginResult in switch loginResult {
case .failed:
self.response = "ERROR"
case .cancelled:
self.response = "ERROR"
case .success:
self.response = "\(String(describing: FBSDKAccessToken.current().tokenString!))"
print(self.response)
}
group.leave() // loginManager.logIn
}
group.wait()
return response
}
}
If you don't like the facebookLogin().login() { accessToken in ... } syntax, you could put the { accessToken in ... } part into its own function
func callServer(accessToken: String) {
// Get the users currency, langage and locale.
let currency = Locale.current.currencyCode ?? "GBP"
let language = Locale.current.languageCode ?? "GB"
let region = Locale.current.regionCode ?? "GB"
let params = "accessToken=\(accessToken)&currency=\(currency)&language=\(language)&region=\(region)"
let resultArray = database().connect(endPoint: "loginfb.php?", params: "\(params)")
print(resultArray)
}
and call it with
#IBAction func facebookButton(_ sender: Any) {
// Get the access token from facebook
facebookLogin().login(completion: callServer(accessToken:))
}

Alamofire using value outside completion handler

I made a request to server with Alamofire and get data from it with completion handler, but I want to use some value outside of it, is it possible to do that?
let retur = Json().login(userName: param1, password: param2) { (json) in
print(json)
let jsonDic = JSON(json)
for item in jsonDic["result"].arrayValue {
let token = item["ubus_rpc_session"].stringValue
}
print(jsonDic["result"][1]["ubus_rpc_session"].stringValue)
}
If you want to access result outside the Alamofire then follow below code.
override func viewDidLoad()
{
super.viewDidLoad()
let retur = Json().login(userName: param1, password: param2) { (json) in
print(json)
let jsonDic = JSON(json)
for item in jsonDic["result"].arrayValue
{
let token = item["ubus_rpc_session"].stringValue
self.myFunction(str: token)
}
print(jsonDic["result"][1]["ubus_rpc_session"].stringValue)
}
}
func myFunction(str: String)
{
//Here it will print token value.
print("Token value ====%#",str)
}
Solution 2:
Here I'll use NSUserDefault, to know more about it you can search about it, there are multiple examples are available on google.
Now here, we will use NSUserDefault.
//Use below code in alamofire block to save token value.
let defaults = UserDefaults.standard
defaults.set(token, forKey: "tokenValue")
//Use below code to print anywhere.
let defaults = UserDefaults.standard
if let myToken = defaults.value(forKey: "tokenValue") as? String
{
print("defaults savedToken: \(myToken)")
}
Hope it works for you.
For more details you can refer this answer https://stackoverflow.com/a/41899777/5886755
As I mention in comment that you want to access your token everywhere in app you can do it this way:
Create a separate class for that like
import UIKit
class UserSettings: NSObject {
class func setAuthToken(authToken: String?) {
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(authToken, forKey: "authToken");
}
class func getAuthToken() -> String? {
let defaults = NSUserDefaults.standardUserDefaults()
return defaults.stringForKey("authToken")
}
}
Now you can set token this way:
let token = item["ubus_rpc_session"].stringValue
UserSettings.setAuthToken(token)
and you get token anywhere in app this way:
if let token = UserSettings.getAuthToken() {
//do your stuff here with token
}

Property declared in my class isn't recognized when attempting to use it inside a function?

I've checked for the misspelling of the property, that's definitely not the case. I'm trying to use the property mySong that I declared in my class inside the parseSongs() function.
That function isn't inside the class but it's in the same file. And the target membership of that class is set to the project name as are the other files as well.
I'm very confused why the compiler isn't recognizing the name of my property in the parseSongs()?
I can declare the property outside of the class but I should be able to use it even if it's declared inside the class.
import UIKit
class SongsTableViewController: UITableViewController {
//A property that is an array of type 'Song'
var mySong = [Song]()
private let cache = NSCache()
private func fetchMyData(){
let myUrl = NSURL(string: "http://itunes.apple.com/search?term=beatles&country=us")!
let mySession = NSURLSession.sharedSession()
//The work to be queued initiates
let myTask = mySession.dataTaskWithURL(myUrl){
//This closure right here is the Completion Handler
data, response, error in
if error != nil{
//Handle error
}else{
let myHttpResponse = response as! NSHTTPURLResponse
switch myHttpResponse.statusCode {
case 200..<300:
print("OK")
print("data: \(data)")
default: print("request failed: \(myHttpResponse.statusCode)")
}
}
}
myTask.resume()
}
}
func parseJson(myData data: NSData){
do{
let json: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data, options: [])
if let unwrappedJson: AnyObject = json{
parseSongs(unwrappedJson)
}
}catch{
}
}
func parseSongs(json1: AnyObject){
mySong = []
//Optional Binding
if let array = json1["results"] as? [[String:AnyObject]]{
//For-In loop
for songDictionary in array{
if let title = songDictionary["trackName"] as? NSString{
if let artist = songDictionary["artistName"] as? NSString{
if let albumName = songDictionary ["collectionName"] as? NSString{
if let artWorkUrl = songDictionary["artWorkUrl100"] as? NSString {
let song = Song(artist: (artist as String), title: (title as String), albumName: (albumName as String), artWorkUrl: (artWorkUrl as String))
mySong.append(song)
}
}
}
}
}
dispatch_async(dispatch_get_main_queue()){
self.tableView.reloadData()
}
}
}
To use the property outside which declared inside a class you have to follow this
SongsTableViewController().theProperty
If you declare it outside class then you can access it in function of outside class

How to get Unauthenticated identity using Swift

I have initialized the credentials provider per this AWS Developer Guide. I'm not sure if it worked, and how to check. I can't seem to find any documentation on how to use Cognito with Swift. I'm running it as a unit test, and the test passes and the line print("identityId", identityId) outputs:
identityId <AWSTask: 0x17d5fde0; completed = NO; cancelled = NO; faulted = NO;>
However, during debug the property identityProvider.identityId is nil.
Here are my files:
// MyAuth.swift
import Foundation
import AWSCognito
class MyAuth {
func getUnauthCognitoId()->Bool {
let identityProvider = MyIdentityProvider()
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityProvider: identityProvider, unauthRoleArn: Constants.ARNUnauth.value, authRoleArn: Constants.ARNAuth.value)
let defaultServiceConfiguration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration
if let identityId = identityProvider.getIdentityId() {
print("identityId", identityId)
return true
} else {
return false
}
}
}
And
// MyIdentityProvider.swift
import Foundation
import AWSCognito
class MyIdentityProvider: AWSAbstractCognitoIdentityProvider {
var _token: String!
var _logins: [ NSObject : AnyObject ]!
// Header stuff you may not need but I use for auth with my server
/*let acceptHeader = "application/vnd.exampleapp-api+json;version=1;"
let authHeader = "Token token="
let userDefaults = NSUserDefaults.standardUserDefaults()
let authToken = self.userDefaults.valueForKey("authentication_token") as String*/
// End point that my server gives amazon identityId and tokens to authorized users
let url = "https://api.myapp.com/api/amazon_id/"
func authenticatedWithProvider()->Bool {
if let logins = _logins {
return logins["ProviderName"] == nil
}
else {
return false
}
}
override var token: String {
get {
return _token
}
}
override var logins: [ NSObject : AnyObject ]! {
get {
return _logins
}
set {
_logins = newValue
}
}
override func getIdentityId() -> AWSTask! {
if self.identityId != nil {
return AWSTask(result: self.identityId)
}
else if(!self.authenticatedWithProvider()) {
return super.getIdentityId()
}
else{
return AWSTask(result: nil).continueWithBlock({ (task) -> AnyObject! in
if self.identityId == nil {
return self.refresh()
}
return AWSTask(result: self.identityId)
})
}
}
override func refresh() -> AWSTask! {
let task = AWSTaskCompletionSource()
if(!self.authenticatedWithProvider()) {
return super.getIdentityId()
}
else {
// TODO: Authenticate with developer
return task.task
}
/*let request = AFHTTPRequestOperationManager()
request.requestSerializer.setValue(self.acceptHeader, forHTTPHeaderField: "ACCEPT")
request.requestSerializer.setValue(self.authHeader+authToken, forHTTPHeaderField: "AUTHORIZATION")
request.GET(self.url, parameters: nil, success: { (request: AFHTTPRequestOperation!, response: AnyObject!) -> Void in
// The following 3 lines are required as referenced here: http://stackoverflow.com/a/26741208/535363
var tmp = NSMutableDictionary()
tmp.setObject("temp", forKey: "ExampleApp")
self.logins = tmp
// Get the properties from my server response
let properties: NSDictionary = response.objectForKey("properties") as NSDictionary
let amazonId = properties.objectForKey("amazon_identity") as String
let amazonToken = properties.objectForKey("token") as String
// Set the identityId and token for the ExampleAppIdentityProvider
self.identityId = amazonId
self._token = amazonToken
task.setResult(response)
}, failure: { (request: AFHTTPRequestOperation!, error: NSError!) -> Void in
task.setError(error)
})*/
return task.task
}
}
And
import XCTest
#testable import My
class MyTests: XCTestCase {
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
func testPerformanceExample() {
// This is an example of a performance test case.
self.measureBlock {
// Put the code you want to measure the time of here.
}
}
func testGetUnauthCognitoId() {
let myAuth = MyAuth()
XCTAssertTrue(myAuth.getUnauthCognitoId())
}
}
It turns out that if you create a default service configuration within the application:didFinishLaunchingWithOptions: application delegate method in your app delegate file as described here:
let credentialsProvider = AWSCognitoCredentialsProvider(
regionType: AWSRegionType.USEast1, identityPoolId: cognitoIdentityPoolId)
let defaultServiceConfiguration = AWSServiceConfiguration(
region: AWSRegionType.USEast1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration
The SDK will use an unauthenticated identity whenever you try to use any of the AWS services, and you don't necessarily need to create a cognitoIdentity object.
getIdentityId returns an AWSTask. Since AWSTask is essentially BFTask with a different name, you can get the identityId using the continueWithBlock syntax shown on the BFTask page. Something like:
credentialProvider.getIdentityId().continueWithBlock {
(task: AWSTask!) -> AWSTask in
if task.error() {
// failed to retrieve identityId.
} else {
print("identityId", task.result())
}