Cannot change PlivoLoginAppDelegate to swift in iOS SDK - plivo

In your iOS SDK PlivoLogin, there are PlivoLoginAppDelegate.h and PlivoLoginAppDelegate.m.
I want to change it to swift file.
I changed it to the following.
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
class var shared: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
var providerDelegate: ProviderDelegate?
var window: UIWindow?
#IBOutlet var viewController: ViewController?
var phone: Phone?
// MARK: UIApplicationDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
print("Finished launching with options: \(launchOptions)")
// // Phone
self.phone = Phone()
self.viewController = self.window?.rootViewController as? ViewController
self.viewController?.phone = self.phone
self.phone?.setDelegate(self.viewController)
return true
}
...
}
But the log only says
Creating endpoint
Generating random password
and stops, fails to get a delegate response to - (void)successWithResponse:.
How can we get a delegate response?

Plivo Sales Engineer here.
Plivo iOS SDK has Objective C interface and you can access the Objective-C library from the Swift 3.0. This is a sample app in the Swift 3.0 using the iOS SDK's Objective-C interface. This app makes outgoing calls and gets delegate callbacks in Swift 3.0.

Related

SwiftUI: AppDelegate on MacOS

I'm porting a SwiftUI iOS app to macOS. It uses the #UIApplicationDelegateAdaptor property wrapper to bind a UIApplicationDelegate class. Unfortunately, the UIApplicationDelegate class isn't available on macOS, so I'm wondering how I bind my custom AppDelegate.
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
...
}
}
struct MyApp: App {
#UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
...
}
}
it's almost the same, but then use NS instead of UI. I did it slightly different than you:
#main
struct MyApp: App {
// MARK: - Properties
// Used variables
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
#Environment(\.openURL) var openURL
var body: some Scene {
WindowGroup {
MainControlView()
}
.commands {
FileMenuCommands(listOfContainers: listOfContainers)
}
}
}
Then you can write your application delegate like:
import AppKit
import SwiftUI
class AppDelegate: NSObject, NSApplicationDelegate {
// Whatever you want to write here
}
This all worked for me.
The macOS equivalent has the NS prefix
import AppKit
class AppDelegate: NSObject, NSApplicationDelegate {
...
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
and the DidFinishLaunching delegate method has a different signature
func applicationDidFinishLaunching(_ aNotification: Notification) [ ...

Xcode 11 beta: AppDelegate file doesn't have window global variable

I am facing an issue in Xcode 11 beta.
The problem is that I am not getting default window variable declared in AppDelegate file.
Is anybody facing this same issue?
In Xcode 11 this is now done in SceneDelegate
EDIT: if you’re still supporting iOS 12 or prior (or want to support 13 plus prior)
Add the UIWindowSceneDelegate to ApplicationDelegate
Add : var window:UIWindow?
Then proceed as you would to setting up in didFinishLaunching
The default var window: UIWindow? Is now moved in SceneDelegate.swift. To set a rootViewController in Xcode 11 you can work within SceneDelegate.swift file, In the scene delegate, you must create the window instance and the root view controller like below:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// set or create your viewController here
let yourViewController = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "yourViewController") as! YourViewController
// set the rootViewController here using window instance
self.window?.rootViewController = yourViewController
}
Also this answer is helpful : Why is manually setup root view controller showing black screen?
Hope it will help you!
My approach:
first, in AppDelegate create a static property with the class of the view controller you want to access, like this
class AppDelegate: UIResponder, UIApplicationDelegate {
// MARK: Home
static var homeViewController: HomeViewController?
...
}
then, in the view controller
// MARK: - Managing the view
override func viewDidLoad() {
super.viewDidLoad()
// Singleton
AppDelegate.homeViewController = self
...
}
how to use:
extension UIViewController {
var homeViewController: HomeViewController? {
if let controller = self as? HomeViewController {
return controller
}
return AppDelegate.homeViewController
}
}
In my case, that's all it takes.
var window: UIWindow? // add this by yourself
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: Device.bounds)
let root = DDTabBarController()
window?.rootViewController = root
window?.makeKeyAndVisible()
return true
}

No such module 'UIKit'

All I want to do is make the most BASIC mac OS X app that just shows a WKWebView...
import UIKit
import WebKit
class ViewController: UIViewController, WKUIDelegate {
var webView: WKWebView!
override func loadView() {
let webConfiguration = WKWebViewConfiguration()
webView = WKWebView(frame: .zero, configuration: webConfiguration)
webView.uiDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let myURL = URL(string: "https://www.apple.com")
let myRequest = URLRequest(url: myURL!)
webView.load(myRequest)
}}
https://developer.apple.com/documentation/webkit/wkwebview
UIKit is for mobile apps, not MacOS apps. You want AppKit.
if you already selected iOS while creating Playground file
but still getting error No such module 'UIKit', then follow following steps.
Xcode 13
enter image description here
Open utilities from the top right corner
Change the platform to IOS
Then it should be okay to use UIKit
Check the 'show utilities' window. If the platform is set to MacOS, you'll get this error if you're calling UIKit. Same thing goes if your wanting IoS and your calling AppKit.
http://iosbrain.com/blog/2018/08/16/xcode-9-playground-error-no-such-module-uikit-or-appkit/

Xcode Mac application (Swift) problems

I am working on a Mac application using Swift.
And I got a lot of problems there cause I thought it would be the same as I did it in the iOS application
I am really sorry about this long post :(
The first problem:
The error: "Method does not override any method from its superclass"
The code:
import Cocoa
class CreateAccountViewController: NSViewController {
#IBOutlet weak var emailTextField: NSTextField!
#IBOutlet weak var passwordTextField: NSSecureTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
The next one:
Error:Value of type 'CreateAccountViewController' has no member 'dismissViewControllerAnimated'
Code:
self.dismissViewControllerAnimated(true, completion: nil)
The last one:
The error: Use of unresolved identifier 'UIAlertController'
The code:
let alert = UIAlertController(title: "Error", message: "Enter Email and Password", preferredStyle: .Alert)
I am so sorry but this is my first time I wrote a Mac application.
UIAlertController is part of UIKit Library which is only available on iOS.
You'll need to use NSAlert to pop an alert in a window on Mac OS X. You also need to do an import AppKit at the top of your Swift file.

Detection of WebView load finish

I am creating a simple app with a WebView basing on this tutorial.
I would like to display a progress indicator while loading the page, but the methods didStartProvisionalLoadForFrame and didFinishLoadForFrame are never called. What am I doing wrong?
ViewController.swift
import Cocoa
import WebKit
class ViewController: NSViewController {
#IBOutlet weak var webView: WebView!
#IBOutlet weak var webViewProgressIndicator: NSProgressIndicator!
let messengerUrl = "https://www.messenger.com/"
override func viewDidLoad() {
super.viewDidLoad()
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
}
override var representedObject: AnyObject? {
didSet {
// Update the view, if already loaded.
}
}
func webView(sender: WebView!, didStartProvisionalLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.startAnimation(self)
}
func webView(sender: WebView!, didFinishLoadForFrame frame: WebFrame!)
{
self.webViewProgressIndicator.stopAnimation(self)
}
}
I am using Xcode 7 Beta and OS X 10.11 Beta.
Rich's answer works on Xcode 7.0.1 ElCaptain Swift2
BUT I had to connect it in the connections inspector as Swift2 did not accept
self.view.frameLoadDelegate = self
as it gave the following error
Cannot assign a value of type 'ViewController' to a value of type
'WebFrameLoadDelegate!'
So as long as you connect it up as follows, all works great.
The tutorial missed out something, Need to add:
self.webView.frameLoadDelegate = self
(can be done in connections inspector)
then call the load..
self.webView.mainFrame.loadRequest(NSURLRequest(URL: NSURL(string: messengerUrl)!))
Also add the WebFrameLoadDelegate interface to your ViewController.. (thanks tjv)
class ViewController: NSViewController, WebFrameLoadDelegate{ .....
You need to assign the delegate of your UIWebview ie: webview.delegate = self

Resources