I want to open my iOS app from and URL but when I put NSURL into fund application print() doesn't working.
please refer here
import UIKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var openUrl:NSURL? //This is used when to save state when App is not running before the url trigered
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//print("Host: \(url.host!)")
//let url = url.standardized
//NotificationCenter.default.post(name: NSNotification.Name(rawValue: "HANDLEOPENURL"), object:url!)
print("bbb")
return true
}
}
There is no AppDelegate method with that signature, so its not getting called
func application(_ application: UIApplication, openURL url: NSURL, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
The correct method signature is:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool`
You are confusing it with this method
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool
EDIT
To respond to links from the web to iOS, use
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: #escaping ([Any]?) -> Void) -> Bool {
Related
Currently, this is my code-snippet, in handling tap action, of local notification.
The following code will launch a UIViewController, when user taps on the local notification.
import UIKit
extension UIApplication {
//
// https://stackoverflow.com/a/58031897/72437
//
var keyWindow: UIWindow? {
// Get connected scenes
UIApplication
.shared
.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
}
}
#main
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
// MARK: UISceneSession Lifecycle
func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}
func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
// This method is called when user clicked on the notification
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: #escaping () -> Void)
{
let newViewController = NewViewController.instanceFromNib()
newViewController.postInit(response.notification.request.identifier)
UIApplication.shared.keyWindow?.rootViewController?.present(newViewController, animated: false, completion: nil)
completionHandler()
}
}
What I am puzzling is, should I install UNUserNotificationCenterDelegate during willFinishLaunchingWithOptions, or didFinishLaunchingWithOptions?
Install during willFinishLaunchingWithOptions
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
Install during didFinishLaunchingWithOptions
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
return true
}
Based on https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate
You must assign your delegate object to the UNUserNotificationCenter
object before your app finishes launching. For example, in an iOS app,
you must assign it in the
application(:willFinishLaunchingWithOptions:) or
application(:didFinishLaunchingWithOptions:) method of your app
delegate. Assigning a delegate after the system calls these methods
might cause you to miss incoming notifications.
It seems like there is no difference between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions
I was wondering, do you find any differences, between willFinishLaunchingWithOptions and didFinishLaunchingWithOptions, as far as UNUserNotificationCenterDelegate installation is concerned?
Thanks.
I would like to use simple_auth_flutter into my FLutter app to authenticate user against instagram. So I followed the documentation page and applied all changes. I try to add the mentioned lines into my AppDelegate.swift as below:
import UIKit
import Flutter
import SimpleAuth;
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any]?) -> Bool{
return SimpleAuth.CheckUrl(url);
}
}
When I try to execute flutter run I see the error:
no such module 'SimpleAuth' import SimpleAuth;
So what should I do to solve the import in the AppDelegate? My pubspec.yml uses:
simple_auth: ^2.0.7
simple_auth_flutter: ^2.0.7
I think i've found a solution that allows my app to build.
import UIKit
import Flutter
import simple_auth_flutter;
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
override func application(_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any]?) -> Bool{
return SimpleAuthFlutterPlugin.check(url);
}
}
I'm trying to put Facebook posting functionality into an iOS app of mine. I'm following the Facebook tutorial, and it tells me to put this code into my AppDelegate, which I've just migrated from Objective-C:
import UIKit
import FBSDKCoreKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
}
}
There is no "ApplicationDelegate" in my code; what should I do?
I'm trying to add the Facebook SDK in my flutter project but i don't know the proper Swift code to put in the AppDelegate.swift. Following this guide I should paste this code:
// AppDelegate.swift
import UIKit
import FBSDKCoreKit
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
func application(
_ app: UIApplication,
open url: URL,
options: [UIApplication.OpenURLOptionsKey : Any] = [:]
) -> Bool {
ApplicationDelegate.shared.application(
app,
open: url,
sourceApplication: options[UIApplication.OpenURLOptionsKey.sourceApplication] as? String,
annotation: options[UIApplication.OpenURLOptionsKey.annotation]
)
}
}
But the Flutter project has a different default code than the native:
import UIKit
import Flutter
#UIApplicationMain
#objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Does anybody knows the proper way to merge them together?
Recently converted a project to Swift 2.2 and running into issues that didn't exist prior. I've tried searching for a workaround but haven't found any similar posts.
Use of undeclared type 'UIApplicationLaunchOptionsKey'.
I can't tell what's wrong with this code in the AppDelegate.swift:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
//UIToolbar.appearance().tintColor = UIColor.clearColor()
return true
}
Try changing the method signature to
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return true
}
The signature is modified to func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool in swift 3
In Swift 3 the method is changed to:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}