I have already read about UIApplication.shared.canOpenURL. But in my case it somehow doesn't work. I have a scheme:
If I have VK app installed, the url vk:// works, and Safari successfully asks me if I want to open this app. On this step, canOpenURL returns true.
If the app is not installed, Safari says, that the url is invalid. But canOpenURL still returns true.
What's going on? Does canOpenURL check only syntax of the URL? Then how must I check if the app is installed?
For open app from deep linking you should also write some lines of code for schema and URL::
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if(url.absoluteString == "appname://app/https://appurl"){
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.objectForKey("access_token") != nil {
// check condition what you want for open specific page
}
else{
let loginVC: LoginViewController = mainStoryboard.instantiateViewControllerWithIdentifier("LoginViewController") as! LoginViewController
self.window?.rootViewController = loginVC
}
}
self.window?.makeKeyAndVisible()
Related
Is there a way to make my app default Siri app for a specified domain? For example in this article:
https://www.smashingmagazine.com/2018/04/sirikit-intents-app-guide/
The author goes to great lengths on letting the system know how the app name is to be expected, e.g. in the phrase "In List-o-Mat, show the grocery store list". But if I omit the app name in my Siri request, iOS default Reminder app seems to always take precedence.
Even if I actually delete iOS Reminder app, Siri is complaining of having no compatible app, instead of using the next logical choice.
Thank You!
No way. Default apps will open firstly if you say common words like "set alarm daily" or "call Zsolt". But you can add your app special shortcut for siri,
Firstly, you should create custom intent that can be used with Siri.
func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
if let _ = userActivity.interaction?.intent as? DoSomethingIntent {
if let windowScene = scene as? UIWindowScene {
self.window = UIWindow(windowScene: windowScene)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
self.window!.rootViewController = initialViewController
self.window!.makeKeyAndVisible()
initialViewController.showMessage()
}
}
}
You need to add Siri from "signing & capacity" also.
I have a problem with the implementation of deep links in my app. I am using a notification called accenggage which can be seen here. Once I Start the SDK with the conf set in the AccengageConfig.plist file in my code as indicated in the documentation, ie I do Accengage.start() in the didFinishLaunchingWithOptions method in AppDelegate, I noticed that the function that opens the url is never called. But it will print the payload in my logs, but I cannot get access to it, in order to use it in my code. Everything works perfectly if I comment it. Please have someone used this before to try to see where I go wrong. when I open the link in safari it will lunch the app but will not print the url as I requested and neither will it open the views concerned. I taught my problem was with the deep links implementation, but that is not the case, it is rather the Accengage that I am not using it properly and will be glad if someone could have an approach on how to get the url when the push is sent with Accengage. Thanks!
func application(_ application: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("url \(url)")
print("url host :\(url.host as String?)")
print("url path :\(url.path as String?)")
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if(url.host == "/details"){
let Ads_details: CollectionDetailViewController = mainStoryboard.instantiateViewController(withIdentifier: "CollectionDetailViewController") as! CollectionDetailViewController
self.window?.rootViewController = Ads_details
} else if (url.host == "/profile"){
let User_profil: UserProfileViewController = mainStoryboard.instantiateViewController(withIdentifier: "UserProfileViewController") as! UserProfileViewController
self.window?.rootViewController = User_profil
}
self.window?.makeKeyAndVisible()
return true
}
I would like to know what is the best way to bypass the login viewcontroller if the user is logged in already. I have integrated Facebook in my app. I tried to check for the FBSDKAccessToken.currentAccessToken if it is nil or not in the appdelegate class so the app could start either from the loginviewcontroller or not, but it is not working. This is what I tried so far.
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("it is logged in")
}else{
print("it is not ")
}
// Override point for customization after application launch.
FBSDKLoginButton.classForCoder()
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
Either the user is logged in or not I get the same message.
What am I doing wrong? Thank you.
NOTE:
I don't know how to solve the issue in your code, but there is another way of doing this, which I consider better!
LOGIN:
SWIFT 2:
You can add this code to the function that does things if the user login attempt was successful:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedin", forKey: "yourKey")
And then, in AppDelegate.swift add this code, inside ApplicationDidFinishLaunchingWithOptions:
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.valueForKey("yourKey") != nil{
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController
self.window.rootViewController = viewController!
}
LOGOUT:
In the logout function, add this:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setValue("loggedout", forKey: "yourKey")
And change the if-statement in didFinishLaunching... to this:
if defaults.valueForKey("yourKey") != nil{
let vc = UIViewController()
let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
let value = defaults.valueForKey("yourKey") as! String!
if value == "loggedin"{
vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as! UIViewController!
}
else if value == "loggedout"{
vc = storyboard.instantiateViewControllerWithIdentifier("loginViewController") as! UIViewController!
}
self.window.rootViewController = viewController!
}
Hope This Helps!
I solved it by moving the following code
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("user is logged in")
}else{
print("user is not ")
}
to my
func applicationDidBecomeActive(application: UIApplication) {}
Thank you for your replies.
You cannot check if the user is logged in device facebook app. You can only check if there is a valid access token or not. If there is one, you don't have to prompt user to login again.Because if accessToken is valid, you can get the user info . Again the currentAccessToken has nothing to do whether you are logged in your phone/computer facebook app or not.
You should always depend on the currentAccessToken.
if ((FBSDKAccessToken.currentAccessToken()) != nil) {
print("user is logged in")
}else{
print("user is not ")
}
If the current access token is nil which is expected by default, you need to initiate the login flow through your app. Please refer to the documentation. Once you have finished the login flow and come back to the app, you will then have a valid access token.
In your case, the access token is nil because you are calling it before the FBSDKApplicationDelegate properly set.
You should do the access token check only after the call to didFinishLaunchingWithOptions is finished. This reason explained in here.
Hello Friends My name is Tony dhand. I am new in IOS development. Now i am trying to fix a problem . I am going to login in ios app and after login display the dashboard.
But when i restart the app it again show the login screen. For this i want to change the initialview at the start of the app after login in other words i want to change the view at the startup .I don't know what to do. please help me for resolving this problem or give your idea for this.
Thanks in Advance
When the user logs in successfully you can save the "Login Status" as -
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(true, forKey: "isUserLoggedIn")
defaults.synchronize()
Then in the "AppDelegate.swift" file you can check the login status when the app launches in the "didFinishLaunchingWithOptions" function as -
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let defaults = NSUserDefaults.standardUserDefaults()
let isUserLoggedIn = defaults.boolForKey("isUserLoggedIn")
if(!isUserLoggedIn) {
self.window?.rootViewController = storyboard.instantiateViewControllerWithIdentifier("LoginViewController")
} else {
self.window?.rootViewController = storyboard.instantiateInitialViewController()
}
return true
}
Finally when the user logs out you can change the logged in status as false as -
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setBool(false, forKey: "isUserLoggedIn")
defaults.synchronize()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("LoginViewController")
Hope that helps!
Keep the status of the login in your NSUserDefaults. When you login successfully, then set it as TRUE for the Key.
So Next time when you launch the app, check the value of the variable stored in NSUserDefaults in AppDelegate.
If its true then your Login SUCCESS, then navigate to your dashboardViewController. Otherwise navigate to your login Page ViewController.
Enter the following code after successful login
[[NSUserDefaults standardUserDefaults]setBool:TRUE forKey:#"isLogin"];
Add the following code in your AppDelegate didFinishLaunchingOptions.
if ([[NSUserDefaults standardUserDefaults]boolForKey:#"isLogin"]) {
// Already Logged in
// Set your Dashboard Page as Root View Controller
}
else{
// Not Logged in
// Set your Login Page as Root View Controller
}
Hope it helps...
I am using deep link to open my app from browser. When my app is in background state i can able to open the app and i can able to move to the respective screen based on the URL. But whenever i quit my app, and open the same URL, it is crashing. I have followed Deep link tutorial for creating all actions. And I am moving to another screen in "triggerImp" method. I have added my code below for navigation. Is it rite or do i need to add anything else. Please help me.
let vc = UIViewController()
(appDelegate.window?.rootViewController as! UINavigationController).pushViewController(vc, animated: true)
Is this exact code that you are using? Not sure the app will know what view controller to open if you set it to UIViewController(). Here is how I have it setup that works for both states, background and inactive (using Storyboards):
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
let storyboard = UIStoryboard(name: mainEntryStoryboardName, bundle: nil)
let newHomeView = storyboard.instantiateViewControllerWithIdentifier("MyView") as! MyViewController
let navigation = MyNavigationController(rootViewController: newHomeView)
self.window!.rootViewController = navigation
return true
}
hope this helps