Manually add Alamofire with swift 3 - swift3

I want to add Alamofire V4 in my swift 3 project. I want to add it manully , like I do with other normal library ,simply add their source code in my project. How to maually add Alamofire , manually , I dont want to add it as framwork/target dependency.

You can easily integrate with POD as well.
and simple code to call API and handle response.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.API_Formation_Function("email#email.com", password: "password12345")
}
func API_Formation_Function(_ userName : String , password : String) {
EMReqeustManager.sharedInstance.apiLogin(userName, password: password) {
(feedResponse) -> Void in
// Show your progress HUD here
if let downloadError = feedResponse.error{
// Hide progress HUD here and show error if comes
} else {
if let dictionary = feedResponse.responseDict as? Dictionary<String, AnyObject>{
// Hide progress HUD here and show response
let responseModel = EMResponseModel.init(jsonDict: dictionary)
print(responseModel)
}
}
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Refer

Is there a specific reason why you don't want to do it automatically with CocoaPods? Don't let setting up CocoaPods turn you off to the idea. Once you get it installed, its wonderful for updating and managing your addons. here is a GREAT video tutorial on how to install CocoaPods.
https://www.youtube.com/watch?v=iEAjvNRdZa0&t=2s
Do yourself a favor and take the time to install.

Related

Cannot Connect Client with StreamIO

I'm following the latest tutorial from Stream Chat, which looks great.
Looks like I followed it to the letter except for I replaced the apiKey with one created for me in the dashboard. This was provided when I registered my free trial.
Unfortunately, I'm unable to connect.
Here's my code
SwiftUIChatDemo
import SwiftUI
// 1 Add imports
import StreamChat
import StreamChatSwiftUI
#main
struct SwiftUIChatDemoApp: App {
// 2 Add Adapter
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ChatChannelListView()
}
}
}
App Delegate
import StreamChat
import StreamChatSwiftUI
import UIKit
import SwiftUI
class AppDelegate: NSObject, UIApplicationDelegate {
// Add context provider
var streamChat: StreamChat?
var chatClient: ChatClient = {
// Low-level client variable with a hard-coded apikey
var config = ChatClientConfig(apiKey: .init("[key I created in dashboard]"))
// Set to use the chat in offline mode
config.isLocalStorageEnabled = true
// Pass the low-level client variable as a parameter of the ChatClient
let client = ChatClient(config: config)
return client
}()
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions:
[UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Initialize the stream chat client
streamChat = StreamChat(chatClient: chatClient)
connectUser()
return true
}
// The `connectUser` function we need to add.
private func connectUser() {
// This is a hardcoded token valid on Stream's tutorial environment.
let token = try! Token(rawValue: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoibHVrZV9za3l3YWxrZXIifQ.kFSLHRB5X62t0Zlc7nwczWUfsQMwfkpylC6jCUZ6Mc0")
// Call `connectUser` on our SDK to get started.
chatClient.connectUser(
userInfo: .init(id: "luke_skywalker",
name: "Luke Skywalker",
imageURL: URL(string: "https://vignette.wikia.nocookie.net/starwars/images/2/20/LukeTLJ.jpg")!),
token: token
) { error in
if let error = error {
// Some very basic error handling only logging the error.
log.error("connecting the user failed \(error)")
return
}
}
}
}
SwiftUIChatDemo.app
import SwiftUI
// 1 Add imports
import StreamChat
import StreamChatSwiftUI
#main
struct SwiftUIChatDemoApp: App {
// 2 Add Adapter
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ChatChannelListView()
}
}
}
When you change the apiKey, you will need to generate new tokens with the secret that's provided to you in the dashboard. For development, you can generate tokens with the tool here: https://getstream.io/chat/docs/ios-swift/tokens_and_authentication/?language=swift#manually-generating-tokens. The user id also needs to match to your user. Note that for production usage, you should generate this tokens on a backend.
In case of additional questions, you can also reach our support channel: https://getstream.io/contact/support/
UPDATE
So, I got it working with the apiKey and token values from the tutorial. Well done. How I do this with my own values probably requires my own users.

I'm trying to convert a MSAL login from UIKit to SwiftUI and not sure how I can implement the MSALWebviewParameters into SwiftUI

I've implemented a lot of the MSAL into SwiftUI, but I'm running into a hiccup with MSALWebviewParameters which needs aUIViewController.
This is the current code portion that works with UIKit.
#objc func buttonTapped(_ snder: UIButton) {
do {
<Some Code that works>
let webViewParameters = MSALWebviewParameters(authPresentationViewController: self)
let interactiveParameters = MSALInteractiveTokenParameters(scopes: ["user.read"], webviewParameters: webViewParameters)
application.acquireToken(with: interactiveParameters) { (result, error) in
}
What I'm trying to do is move that function into an ObservableObject, but these three lines depend on a UIViewController. My end goal is to move this function into a classObservableObject

Using a built in model with CoreML

Is it possible to use an already built in model (e.g. Food) from the Watson Studio in your CoreML app? I can't figure out how to do so if it is. All the documentation there is tells me to train my own custom model, but I want to use the already existing food model.
This is what I have done so far:
let apiKey = "xyz"
let classifierId = "food"
let version = "2017-12-07"
var visualRecognition: VisualRecognition!
override func viewDidLoad() {
super.viewDidLoad()
self.visualRecognition = VisualRecognition(apiKey: apiKey, version: version)
}
override func viewDidAppear(_ animated: Bool) {
// Pull down model if none on device
let localModels = try? visualRecognition.listLocalModels()
print(localModels)
if let models = localModels, models.contains(self.classifierId) {
self.currentModelLabel.text = "Current Model: \(self.classifierId)"
} else {
self.invokeModelUpdate()
}
}
But running this gives me a 404: Status code was not acceptable: 404
This is not possible. I got a response back from the IBM team. Hope this helps someone else.

how to move from one view Controller to another without using a button

I am using the following code snippet:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
I want to execute this code in the viewDidLoad. I will be using something like this in a series of if statements.
I used some code I found here that utilized a label. It did not work. Can a program move from one viewController without using a button? Almost every example, youtube video uses a button. I will be using a button when it is appropriate
You can not push view controllers before your current view controller loads. You should call your code in your viewDidAppear.
If you plan on directing to a particular view when the app launches I suggest you moving your logic to the AppDelegate in the method: didFinishLaunching. This way you don't need to unnecessarily load views.
Example of a ViewController being pushed in the viewDidAppear:
///
/// FUNCTION - VIEW DID APPEAR
///
override func viewDidAppear(_ animated: Bool) {
// GET VIEW CONTROLLER
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
// PUSH VIEW ONTOP OF CURRENT NAVIGATION CONTROLLER
self.navigationController.pushViewController(secondViewController, animated: true)
} // END - FUNCTION

Pass variable to next View Controller

I want to pass a variable from one view controller to another.
First View Controller looks like this and the segue gets triggered correctly and the new view controller gets displayed.
#IBAction func testButton(_ sender: Any) {
let timerVC = TimerViewController()
timerVC.secondsPassed = textfield_seconds.text!
navigationController?.pushViewController(timerVC, animated: true)
}
On the next View Controller (TimerViewController) I declared a variable in the class header
var secondsPassed:String!
and in viewDidLoad I just want to print the value to the console and all I receive is a 'nil'.
I looked through various tutorials but I seem not to find the correct answer for this to get it work.
Anyone around with a clue?
Thanks in advance...
If you already are using a segue, you can remove this line that presents the controller (you don't seem to have a navigationController anyway):
navigationController?.pushViewController(timerVC, animated: true)
Now the correct way to pass data to another VC using segues is this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? TimerViewController {
vc.secondsPassed = textfield_seconds.text!
}
}