Redirect back to app from Share Extension - ios

Is it possible to redirect a user back to my app from my share extension (after finishing the posting/other action)?
I couldn't get UIApplication.sharedApplication() to work - method was unavailable.
Any ideas if this is possible/if Apple even allows it?

Actually this is possible by searching for UIApplication up in the responder chain and invoking openURL on it, like described in this response : https://stackoverflow.com/a/28037297/554203.
This is the code that works for XCode 8, Swift 3.0 and iOS10 (again, extracted from Julio Bailon's response above):
let url = NSURL(string:urlString)
let context = NSExtensionContext()
context.open(url! as URL, completionHandler: nil)
var responder = self as UIResponder?
while (responder != nil){
if responder?.responds(to: Selector("openURL:")) == true{
responder?.perform(Selector("openURL:"), with: url)
}
responder = responder!.next
}

As of iOS 9.3, this does not seem to be possible for a Share Extension.

Related

WKWebView does not open https://itunes.apple.com/app/id123456 link

Here is a little problem I just noticed in one of my iOS apps.
I use Xcode Version 10.1 and Swift 4.2.
The app has a button which when pushed brings up a view controller, this VC is in charge of opening a link to the app itself in itunes. I have done this many times with no problems in the past.
But this time, a blank pages opens up and nothing else. I have tried to replace only the URL I am interested in by "https://www.google.com/" and it works perfectly as expected (i.e. the Google page shows up). Of course I have verified that my URL is correct.
Can anybody see what could be the issue?
Thanks in advance for any relevant tip.
Here is the code for the whole view controller.
import UIKit
import WebKit
class appStore_ViewController: UIViewController, WKNavigationDelegate {
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
var webView: WKWebView!
override func loadView() {
webView = WKWebView()
webView.navigationDelegate = self
view = webView
}
override func viewDidLoad() {
super.viewDidLoad()
let appStoreURL = URL(string: "https://itunes.apple.com/app/id\ (appDelegate.applicationID)?mt=8")
//let appStoreURL = URL(string: "https://www.google.com/") // This works as expected.
print("The link: \(appStoreURL!.absoluteString)") // This shows what is expected (a working URL).
webView.load(URLRequest(url: appStoreURL!))
}
}
You should see what happen while visiting the AppStore Url with safari browser in your iPhone. then you will find the it still show blank page but show an alert to open the Appstore.
If you want to do the same function in your app with webView. you have to handle the alert the by yourself.
You need to implement the decidePolicyForNavigationAction method of WKNavigationDelegate for this to work.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)
(WKNavigationActionPolicy))decisionHandler {
/* Code to handle other schemes such as file, https, http, etc. if required
*/
if ([[[navigationAction.request URL] scheme] isEqual:#"itms-appss"])
{
[[UIApplication sharedApplication] openURL:[navigationAction.request URL]];
decisionHandler(WKNavigationActionPolicyCancel);
return;
}
}
WKWebview does not inherently support different schemes such as "itms-appss" and you need to check and implement the url scheme you want to support.
You also need to check if you are running in iPadOS and set WKWebpagePreference to use mobile content mode if you do so.

branch io clicked_branch_link is false

I have integrated branch.io deep linking in my iOS app (Swift 4.2). What I want is canonical_url when user taps on branch.io short link.
When app is running in background I get all required parameters in following block. but when app is not running and try to tap branch.io short link then app launches but I do not get required parameters even clicked_branch_link is false.
Branch.getInstance().initSession(launchOptions: launchOptions)
{ (params, error) in
print(params as? [String: AnyObject] ?? {})
guard error == nil else { return }
guard let userDidClick = params?["+clicked_branch_link"] as? Bool else { return }
}
Please make sure you have all the basic implementation code as shown in our docs here: https://branchmetrics.github.io/docs/pages/apps/ios/#initialize-branch.
In addition, you can find a working implementation of the Swift Branch SDK in our Testbed application here: https://github.com/BranchMetrics/ios-branch-deep-linking/tree/master/Branch-TestBed-Swift.

How to auto close a Safari View Controller or Chrome Custom Tab and return to the mobile app?

My mobile app (React Native) uses a Web View (Chrome Custom Tabs on Android and Safari View Controller on iOS) for a small user flow within the app.
At the end of the process the web view should close and return to the app. I've managed to make it work on Android by using a custom scheme URL redirect at the end (myapp://home), but that doesn't seem to work on iOS. In fact, I haven't found a way to do it on iOS at all.
What approach is generally used for this type of scenario?
For iOS 11 and above, you can use SFAuthenticationSession.
Example:
let url = URL(string: "https://www.example.com")
let callbackURLScheme = "myapp"
let webSession = SFAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: { (url, error) in
//Will be triggered when a URL of scheme 'myapp' is launched
//or if the user clicks on cancel button in SFAuthenticationSession window (error case)
})
webSession.start()
Note: This is deprecated by apple
For iOS 12 and above, use ASWebAuthenticationSession. Example:
let url = URL(string: "https://www.example.com")
let callbackURLScheme = "myapp"
let webAuthSession = ASWebAuthenticationSession(url: url, callbackURLScheme: callbackURLScheme, completionHandler: { (url, error) in
//Will be triggered when a URL of scheme 'myapp' is launched
//or if the user clicks on cancel button in SFAuthenticationSession window (error case)
})
//available only in iOS 13 and above (reference: https://developer.apple.com/documentation/authenticationservices/aswebauthenticationsession/3237232-presentationcontextprovider)
webAuthSession.presentationContextProvider = presentationContextProvider // example: UIViewController
webAuthSession.start()

Add function for cancel in UIAlert of Phone Call function

I have a function that places a phone call when a button is pressed.
private func callNumber(phoneNumber:String) {
if let phoneCallURL = URL(string: "tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
application.open(phoneCallURL, options: [:], completionHandler: nil)
}
}
}
I want to add some functionality if the users hit cancel when the alert pops up to confirm the call. How would I do that?
According to this question: Prompt when trying to dial a phone number using tel:// scheme on iOS 10.3
This alert is actually a bug in iOS 10.3 and should be removed at some point in the future. It isn't supposed to come up for "tel:" links in native apps.
That said, I don't believe there is a way to detect the alert and how the user interacts with it.

Open URL from button action in Swift

In my app I am retrieving an URL from a parse.com Object.
I want to open the device's browser and show the URL received from Parse when the user clicks on the button.
This is the action:
#IBAction func botonNuevos(sender: AnyObject) {
let query = PFQuery(className: "datos_contacto")
query.getObjectInBackgroundWithId("H52ZxGm3U8", block: {
(questionObject: PFObject?, error: NSError?) -> Void in
let webNuevos: AnyObject! = questionObject!.valueForKey("dato_contacto")
print(webNuevos) //= http://www.touchemotors.com
if let webCallURL = NSURL(string: webNuevos as! String ) {
let application = UIApplication.sharedApplication()
if application.canOpenURL(webCallURL) {
application.openURL(webCallURL)
}
else{
print("failed")
}
}
})
}
when the button is clicked, the log shows the received URL, but the browser is not launched and any error is shown.
Please tell what is wrong in my code. Thank you
UPDATE
Finally after discussion with OP it turned out that value returned by questionObject!.objectForKey("dato_contacto") had whitespace at the end so NSUrl(string:) did not parse it well.
UPDATE
You are using wrong method NSObject.valueForKey(). Use PFObject.objectForKey() instead.
For iOS 9:
From here:
If your app is linked on or after iOS 9.0, you must declare the URL
schemes you want to pass to this method. Do this by using the
LSApplicationQueriesSchemes array in your Xcode project’s Info.plist
file. For each URL scheme you want your app to use with this method,
add it as a string in this array.
If your (iOS 9.0 or later) app calls this method using a scheme you
have not declared, the method returns false, whether or not an
appropriate app for the scheme is installed on the device.
Add http scheme to LSApplicationQueriesSchemes array in Info.plist.

Resources