Square API + OAuth: How do I sign out of my iPad app? - ios

I am using Swift and the OAuthSwift pod to handle OAuth using an in app SFSafariViewController. Here's what it looks like for sign in:
The thing is, when I try to log out with this code:
#IBAction func signOutButtonPressed(_ sender: AnyObject) {
let svc = SFSafariViewController(url: URL(string: "https://squareup.com/logout")!)
self.present(svc, animated: true, completion: {
print("signed outttt")
appStore.dispatch(AppAction(type: .MerchantLogout()))
})
}
It opens the browser again and shows that page. The user is successfully logged out (the cookies are reset, etc) but I would like the browser to just close instead of showing the sign in form again. Signing in through that form does not call my OAuth callback. Instead it just signs them in and sends them to the square dashboard.
What's the proper way of signing out the user using an in app SFSafariViewController + OAuth?

Eventually I just ended up doing this:
#IBAction func signOutButtonPressed(_ sender: AnyObject) {
let svc = SFSafariViewController(url: URL(string: "https://squareup.com/logout")!)
self.present(svc, animated: true, completion: {
appStore.dispatch(AppAction(type: .MerchantLogout()))
self.dismiss(animated: false, completion: nil)
})
}

Related

Swift YouTube video in Full Screen & Auto Play with SFSafariViewController

Problem:
I would like SFSafariViewController to autoplay a YouTube video in full screen.
Code:
import SafariServices
#objc func handleYTTap(_ sender: UIGestureRecognizer) {
let youtubeVideoURL = "https://youtu.be/d9MyW72ELq0"
let bookmark = NSURL(string: youtubeVideoURL)
let safari = SFSafariViewController(url: bookmark as! URL)
if(UIDevice.current.userInterfaceIdiom == .pad) {
safari.modalPresentationStyle = .fullScreen
}else{
safari.modalPresentationStyle = .fullScreen
}
self.present(safari, animated: true, completion: nil)
}
Tried Solutions:
I have tried to implement
"?playsinline=1?autoplay=1".
to the url but still cant get the SFSafariViewController to autoplay in full screen. is this even possible to do without creating a custom view or using a library of some sort?

How best to remove all UIViewcontrollers and goto a single UIViewController?

I have an application with the option to change a users password. The reset password process is all done via web links. When the process is complete, the user returns to their app. Given that the password has now been changed, i would like to log the user out.
This is done when a user makes a request to the server for information and my server code will respond with a custom code to let the app know that the password has been changed. In this instance the code is '177'.
I have attempted to present the user with the 'login' uiviewcontroller - but without success. the screen just freezes. Please can someone advise?
if code == 177{
// INVALID API KET USED. LOG USER OUT
print("INVALID API KET USED. LOG USER OUT")
GlobalFunction.logout(action: {
// GO TO LOGIN VIEWCONTROLLER
print("GO TO LOGIN PAGE")
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "login") as! MyLoginViewController
self.present(loginVC, animated: true, completion: nil)
})
return
}
SECOND CLASS:
class GlobalFunction{
// LOG USER OUT OF APPLICATION
static func logout(action: #escaping (() -> ())){
// Remove logged in user credentials
UserDefaults.standard.removeObject(forKey: "userId")
UserDefaults.standard.removeObject(forKey: "firstname")
UserDefaults.standard.removeObject(forKey: "firstname")
UserDefaults.standard.removeObject(forKey: "api_key")
UserDefaults.standard.synchronize()
print("user logged out")
}
}
Make sure you have to run the UI events on main thread.
DispatchQueue.main.async {
let loginVC = self.storyboard?.instantiateViewController(withIdentifier: "login") as! MyLoginViewController
self.present(loginVC, animated: true, completion: nil)
}

Problems when adding social media shareing in my app

I am adding social media functions on my app and I want to be able to put my own image in the box on the right when the user shares it on their facebook.
I have attached the code I used below. How do I make it so I can add a image so it will be shown when the user posts it on their facebook?
Thanks in advance
#IBAction func facebook(_ sender: Any)
{
if SLComposeViewController.isAvailable(forServiceType: SLServiceTypeFacebook)
{
let vc = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
vc?.setInitialText("How many of these expressions can you relate to? http://www.example.com")
vc?.add(URL(string: "https://www.facebook.com"))
present(vc!, animated: true, completion: nil)
}
else
{
print("error")
}
}

ios9 - Swift 2.1 - SFSafariViewController

I'm looking to use the SFSafariViewController and have it automatically load a website when the app is opened. Is this possible?
What I have so far (brand new single ios app):
// ViewController.swift
let urlString = "https://stackoverflow.com"
#IBAction func launchSFSafariViewController(sender: AnyObject) {
if let url = NSURL(string: urlString) {
let vc = SFSafariViewController(URL: url, entersReaderIfAvailable: true)
vc.delegate = self
presentViewController(vc, animated: true, completion: nil)
}
}
How do I connect the action to the main view controller via the storyboard? All the articles I see only discuss connecting a button to an action.
Implement viewDidAppear to perform the presentation. (You will need to use a Bool flag, though, to make sure this doesn't happen when you dismiss the presented view controller and viewDidAppear is called again!)

How to access Cloud drives to import files to my app in swift?

I want to get documents from Cloud services like iCloud , google drive and dropbox on a button click (like in WhatsApp screenshot below), does anyone know how to do it in swift ? Thanks in advance
From your project's capabilities. First enable both the iCloud services and the Key-Sharing, import MobileCoreServices in your class and finally extended the following three classes inside your UIViewController :
UIDocumentMenuDelegate,UIDocumentPickerDelegate,UINavigationControllerDelegate
Implement the following functions :
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : /(myURL)")
}
public func documentMenu(_ documentMenu:UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("view was cancelled")
dismiss(animated: true, completion: nil)
}
How to call all of this? Add the following bit of code to your click function..
func clickFunction(){
let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
Click your button. The following menu will pop up ..
In the case of DropBox. Upon clicking on any item. You will be redirected back to your app and the URL will be logged in your terminal.
Manipulate the documentTypes to your need. In my app, Users permitted to Pdf only. So, suit yourself.
kUTTypePDF
Also if you feel like customizing your own menu bar. Add the following code and customize your own function inside the handler
importMenu.addOption(withTitle: "Create New Document", image: nil, order: .first, handler: { print("New Doc Requested") })
Enjoy it.
First enable iCloud documents Capabilitie, see Apple documentation here
The you have to use UIDocumentMenuViewController
let importMenu = UIDocumentMenuViewController(documentTypes: doctypes, inMode: .Import)
importMenu.delegate = self
importMenu.popoverPresentationController?.barButtonItem = self.addButon;
self.presentViewController(importMenu, animated: true, completion: nil)

Resources