How to close SFSafariViewController automatically when reaching a certain page - ios

I want to close SFSafariViewController automatically upon reaching the "thank you" page of the Dropbox site after the user uploads something; it needs to automatically dismiss. How can I do that?
Here is what I have so far:
#IBAction func Singles5(_ sender: Any) {
let safariVC = SFSafariViewController(url: NSURL(string: "https://www.dropbox/Upload")! as URL)
self.present(safariVC, animated: true, completion: nil)
safariVC.delegate = self
}

One way I can think of is using the Custom URL scheme. You can specify your app's custom URL in the callback parameter of Dropbox (if Dropbox has callback). So when the user has finished uploading his/her file, dropbox executes the callback. In this case your app will receive the callback, with any parameters have you specified. This will call the function application(app, open, options)->Bool in your AppDelegate. Now, you can use a reference to the ViewController which presents the SFSafariViewController and call SafariViewController.dissmissViewController().

Related

Open settings URL from user notification?

Opening settings URLs works for me from a UIAlertController but not from a UNUserNotificationCenter notification. Both approaches use the same URLs and the same code, namely this function:
func open() {
if let url = self.url {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, completionHandler: { (success) in
Log.log("Opened settings url: \(url), \(success)", true)
})
}
}
}
My logging line shows the completionHandler gets called with true from the alert and false from the notification. At first I thought it might be an issue with the phone being locked, but I also got false through a notification interacted with on the home screen.
What do I need to do to get this to work from notifications?
Try it with ".foreground" in options while declaring UNNotificationAction.
(this action should cause the application to launch in the foreground.)

Session completion handler not being called for ASWebAuthenticationSession

I am trying to use ASWebAuthenticationSession web view. After the authentication is complete, the session completion handler is not being called. Hence, the web view doesn't dismiss.
guard let authURL = URL(string: "https://github.com/login/oauth/authorize?client_id=<client_id>/")
else { return }
let scheme = "octonotes"
session = ASWebAuthenticationSession.init(url: authURL, callbackURLScheme: scheme, completionHandler: { callbackURL, error in
// Handle the callback.
print(callbackURL!)
print(error!)
})
session?.presentationContextProvider = self
session?.start()
I have set the callback url scheme in info.plist. The same is updated in Targets -> info -> URL Types
It looks like:
URL Types
After running the above code, ASWebAuthenticationSession web view is presented, which provides user with sign in page. Once the authentication is complete, web view does not dismiss unlike WKWebView.
There is cancel option on top left of the web view, it calls the completion handler with error.
Is there a way to dismiss webview after the authentication session is complete?
It looks like you're missing the redirect URI in your authURL - so GitHub doesn't know where to redirect the successful auth to.
Try this:
let scheme = "octonotes"
guard let authURL = URL(string: "https://github.com/login/oauth/authorize?client_id=<client_id>&redirect_uri=\(scheme)://authcallback")
Bear in mind you may be missing some other parameters too, take a look here for the other ones you can provide https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/#redirect-urls

How to know if user shared my app or not?

My app is all about getting points and winning money, and what I need to do is let the user share my app then give him some points.
The problem is that I don't know how to detect if the user truly shared the app or not
I'm using the following code:
func shareTapped(){
let text = "example"
let url = URL(string: "example.com")
let image = UIImage(named: "example_image")
let shareViewController = UIActivityViewController(activityItems: [text, image!, url!] ,applicationActivities: nil)
self.present(shareViewController, animated: true, completion: {() in
print("done")
})
}
The sharing method is working perfectly, but I was wondering if there is any delegate we can call in this situation.
Thanks.
So there is 2 scenarios where user can cancel share.
One is when UIActivityViewController present then there is a cancel button on UIActivityViewController from where user can cancel it and yes you can detect it with method
shareViewController.completionWithItemsHandler = { activity, completed, items, error in
}
In above method completed will be false if user canceled from UIActivityViewController cancel button. and it will return true if user share successfully but here comes second case with it.
So for second case suppose user want to share via watsapp and click on watsapp icon from UIActivityViewController and watsapp user list appear.
But there is a cancel button on that screen from where user can cancel sharing but still you will get completed flag true so there is no way you can detect if user click on cancel button from watsapp user list.

IOS swift how to know when activityViewController has been successfully used

I have a tableView that utilizes the UIActivityViewController so users can share content on other 3rd party networks Facebook, twitter, text message etc. I have a function called IncreaseShareByOne() and it's purpose is to count the number of times that something has been shared . My problem is that right now that function fires of every time the UIActivityViewController is clicked . Is there someway that I can find out if a user really shared something so that I can use that function correctly ? becomes sometimes you open the UIActivityViewController and decide not to share anything so I want to catch and not use that function. I am new to swift and am using version 3 .
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true,completion: nil)
IncreaseShareByOne()
}
You can add a completion handler to your UIActivityViewController. In the completion handler, check whether the user submitted using the completed value. You'll probably want to do something like this:
func sharedShareAction(controller: UIViewController, sender: UIButton) {
controller.present(activityViewController,animated: true, completion: nil)
activityViewController.completionWithItemsHandler = { activity, completed, items, error in
if !completed {
// handle task not completed
return
}
IncreaseShareByOne()
}
}
Check the API docs for more info.

swift share with function on completion

My app can share files with other apps, but the problem is that I need to delete the files after sharing them... I tried using the onCompletion function as below:
let activityVC = UIActivityViewController(activityItems: objects, applicationActivities: nil)
view.present(activityVC, animated: true) {
try! FileManager.default.removeItem(at: targetURL)
}
The problem is that the onCompletion function executes after the action view disappears not after the whole process of sharing is finished, that's why if I delete the file and the sharing process is still ongoing it will be aborted.. an example is when using telegram for sharing; since telegram asks you to select a contact to send the file to, by that time the view has already disappeard (the function is executed and deleted the file before sharing it)...
It's far too soon to do anything in the completion handler of presenting the controller.
Set the completionWithItemsHandler property of the UIActivityViewController. This will get called when the sharing process is complete.
activityVC.completionWithItemsHandler = { (activityType: UIActivityType?, completed: Bool, returnedItems: [Any]?, error: Error?) -> Void in
if completed == true {
try! FileManager.default.removeItem(at: targetURL)
}
}

Resources