Get launchOptions when resuming App - ios

I am able to handle the launchOptions value in the application method (since, obviously, the parameter gets passed to it). What I'm doing is basically receiving an image from a user who imported it by selecting my app in the Share menu:
It works fine if the App hasn't already been launched, but I don't see how I get the input parameters if the App is already running and the application method isn't called.
I tried to find a method that would help me like
applicationWillEnterForeGround(_ application: UIApplication, _ launchOptions: [UIApplicationLaunchOptionsKey: Any]?
but without any success.
I assume it's possible, since you can share images to WhatsApp or Facebook too, even when they've already been launched.
Can someone help me out?
Thanks,
Jan

You should implement the application:openURL:options: method as follows (Swift 2):
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
// Do your stuff and return true if you have handled the URL...
// Else
return false
}
Relevant tutorial in Ray Wenderlich
As of Swift 4.2 the signature is:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
// Do your stuff and return true if you have handled the URL...
// Else
return false
}

I think you're currently watching in the wrong direction. You should refer to Inter-App communication guide, provided by Apple. If generalise this, you simply need this method, that will handle URI link to your app.
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options;

Related

iOS - Initialise WebEngage SDK Conditionally with different Accounts Ids

I am in need of Initialising WebEngage SDK based on some conditions.
if condition {
//initialise with `X` Account ID
} else {
//initialise with `Y` Account ID
}
But as per WebEnage-Documentation there is no method for initialising SDK with accountId.
we just call below code in our AppDelegate's didFinishLaunchingWithOptions method.
WebEngage.sharedInstance().application(application,
didFinishLaunchingWithOptions: launchOptions)
and it initialise the SDK by reading account id from info.plist file.
I tried updating Info.plist at run time but that didn't work.
I am looking for the right approach to do it.
In WebEngage v6.0.0 a new initialising method is introduced that can have licenseCode as parameter.
func application(_ application: UIApplication?, didFinishLaunchingWithOptions launchOptions: [AnyHashable : Any]?, notificationDelegate: WEGInAppNotificationProtocol?, autoRegister apnRegister: Bool, setLicenseCode licenseCode: String?) -> Bool
I initialises SDK with help of above method in AppDelegate's didFinishLaunchingWithOptions method.
if staging {
WebEngage.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions, setLicenseCode: "xyz")
} else {
WebEngage.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions, setLicenseCode: "abc")
}

Get parameters from URL while not launching app for the first time in viewWillAppear

we provide some parameters using URL schema and it works well if we launch app for the first time. I have provided a custom initialization in AppDelegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
if let token = Helper.getQueryStringParameter(url: url.absoluteString, param: AUTH_TOKEN_PARAM) {
tokenFromURL = token
return true
} else {
}
return false
}
However this code does not work if for instance user downloads app, opens it and returns back to the web and presses same button to launch app.
I think I need to check if URL was provided in the viewWillAppear, am I right? How to do that, please help?

How to get rid of Back to Messenger after sharing link thru messenger sharing dialog

Currently here's my code to open the sharing dialog to share a link thru messenger:
let invitationLink = NSURL(string:"http://somelink.com")
if (UIApplication.sharedApplication().canOpenURL(NSURL(string: "fb-messenger-api://")!)){
let share = FBSDKMessageDialog()
share.shareContent = FBSDKShareLinkContent()
share.delegate = self
share.shareContent.contentURL = invitationLink
share.show()
}[![enter image description here][1]][1]
And I'm handling the response as well thru FBSDKSharingDelegate on my view controller
By doing so, the app opens Messenger and it seems like when the messenger sharing is done or cancelled, I still have "Back to Messenger" like if Messenger app was the originator ... I would have expected not to see that "Back to Messenger" button after closing the sharing dialog.. Any ideas?
Thanks in advance,
Try to add this line to AppDelegate
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool
{
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}

What does the Swift 2 method signature for application:openURL:options: look like?

I'm working on the Swift version of an app that handles custom URL schemes.
The method you need to implement changed in iOS 9.
The Objective-C version of the method works fine in an Objective-C app:
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<NSString *,
id> *)options
{
//my code here
}
However, in my Swift app, the equivalent function:
func application(application: UIApplication,
openURL: NSURL,
options: [String : AnyObject]) -> Bool
{
//My code here
}
Is never called when I run the app on an iOS 9 device. When I invoke my custom URL scheme in Safari, I get prompted 'Open in "appname"?', and when I tap open, it brings my app back to the foreground, but the above method does not get called.
There must be some subtle mismatch in my method signature, but I can't see it. What am I doing wrong? I've tried various variations, none of which work.
My problem appears to have been a red herring caused by a corrupted project. I created a new project file and copied the same code in and in the new project, application:openURL:options: is called correctly.
This is a very strange problem. If I delete "AppDelegate.swift" in the malfunctioning project and replace it with an AppDelegate.m/AppDelegate.h, then the application:openURL:options: is called correctly in the Objective-C version.
My suspicion is that there is an intermittent bug in Xcode that causes some projects to fail to cal your app delegate's application:openURL:options: when the app delegate in Swift.
If you are having the same problem you may want to create a new project, set up your info.plist, and copy over the application:openURL:options: method to see if the new project calls your method.
Function signature (iOS9) is:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool
And if you want to test it working, just copy this into your app delegate:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool
{
print("Scheme: \(url.scheme)")
print("Host: \(url.host)")
print("Path: \(url.path)")
print("Query String: \(url.query)")
// DEBUG: get all key-value pairs in options also
for (key, value) in options {
print("Key: \(key), Value: \(value)")
}
return true
}
Also remember to add the "scheme" (app name) to your info.plist file. Call from Safari on the phone like this
scheme://host/path?query

Authenticating with FlickrKit for iOS

I am using FlickrKit to attempt to login to my Flickr account for iOS (iPhone 5 simulator).
A) I specified the following for [[FlickrKit sharedFlickrKit] initializeWithAPIKey:]
- Flickr Key
- Flickr Secret
B) Then I called [[FlickrKit sharedFlickrKit] beginAuthWithCallbackURL:]
- where the callback URL is #"MyTestApp://auth".
- "MyTestApp" is defined under URL Types -> Item 0 -> URL Schemes -> Item 0.
C) Unfortunately after the logging in process, I get this error when I try to login using FlickrKit for iOS.
"An external application has asked to link to your Flickr account, but
failed to include all the necessary information. Specifically:"
(then I see a black bar, so I have no idea what the specific error is).
See screenshot below:
Any ideas?
Are you having to press the "OK, I'LL AUTHORIZE IT" button twice, with this black bar appearing the second time?
I believe what is happening is that you select the "authorize" button, which then sends the callback to your app, but when you select that button again Flickr thinks you are trying to use a duplicate auth token and wont allow it.
As a quick check, implement this method in your app delegate:
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
Then throw an NSLog in there to see what the url is that is being passed. You'll probably get the data you were looking for.
I ran into this problem earlier today and was incredibly frustrated by it.
So for the latest version of swift I had to do the following:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
if (url.host == "oauth-callback") {
OAuth1Swift.handleOpenURL(url)
}
return true
}
Also my plist needed the following:
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>my.bundle.id</string>
<string>oauth-swift</string>
</array>
<key>CFBundleURLName</key>
<string></string>
</dict>
</array>
But that is because I was using the following library: https://github.com/dongri/OAuthSwift
This is coming over a year late. I was able to make it work by making sure the all the info.plist was well configured (with the right schemes. The previous dev removed it. But, it worked fine then.) and also, I override a function in my AppDelegate class. func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool and did this
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
print("DictionaryScheme: ", url.scheme!)
if url.scheme == "schemename" {
NotificationCenter.default.post(name: Notification.Name(rawValue: "UserAuthCallbackNotification"), object: url)
return true
}
return false
}

Resources