How to session Expire in UIWebview in Swift 3? - ios

i want to logout from the webview after logged in uiwebview but some Sites Provides Logout Button to Logout ,
and Some Website Or Blogs have not Logout button So i want To switch User With Same Device And Same Webview Without Delete App.
So Any Idea About To Clear Session Or Expire Session From WebView. Because M using UiWebview

If you want to clear the cashed data in the web view:
func clearCookies()
{
if let cookies = HTTPCookieStorage.shared.cookies
{
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
for cookie in cookies
{
HTTPCookieStorage.shared.deleteCookie(cookie)
}
}
}

Related

How to return to today extension from app

When the user taps the today extension it opens the app where they can modify its settings. But when the app is exited, the user is returned to the home screen. Is there any way I return focus to the today extension so the user can view the changes immediately? Or is this just not possible in iOS?
I recommend making a button to go to the app. Or add transition functionality after applying settings.
like this
var instagramHooks = "instagram://user?username=johndoe"
var instagramUrl = NSURL(string: instagramHooks)
if UIApplication.sharedApplication().canOpenURL(instagramUrl!)
{
UIApplication.sharedApplication().openURL(instagramUrl!)
} else {
//redirect to safari because the user doesn't have Instagram
UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!)
}
User can click on the top left button to return if the application has been opened from another application. You can update the data on his return.

Clear Cache in a web view Swift 3.3

New to programming, learning from Youtube and from You Guys ! Thanks a lot.
After intentionally entering wrong credentials on the webpage login inside the web view , the webpage normally shows an error of login. I added a reset button on the app interface to reload the MAIN WebApp web view.
It reloads and shows the same webpage login error. (instead of reloading the main webpage cache free to allow entering the proper credentials again)
I tried to add clear cache function with no success.
#IBAction func RefreshApp(_ sender: UIButton) {
viewdidload()
}
In viewdidload, i have included :
URLCache.shared.removeAllCachedResponses()
URLCache.shared.diskCapacity = 0
URLCache.shared.memoryCapacity = 0
with no success
This is working in Swift 3.1 , XCode 8.3.1 :
//Delete Cookies
if let cookies = HTTPCookieStorage.shared.cookies {
for cookie in cookies {
NSLog("\(cookie)")
}
}
let storage = HTTPCookieStorage.shared
for cookie in storage.cookies! {
storage.deleteCookie(cookie)
}

Twitter / Fabric login button not behaving as expected

I have a very simple iOS project where I'm using Twitter/Fabric login button for user login to my app.
I've managed to get the Fabric login button working. When the user clicks on the Twitter login button they are automatically authenticated (that's if they are logged into the Twitter app) otherwise the user is presented with a Twitter login screen.
I'm not sure why the user is automatically authenticated when they are logged into the Twitter app on their phone.
Is there a way to use the Twitter/Fabic API to open the Twitter app and ask for permission to grant access to my app similar to Facebook login even if the user is logged into the Twitter App.
This is what my AppDelegate looks like:
Twitter.sharedInstance().start(withConsumerKey: "someKey", consumerSecret: "someSecret")
Fabric.with([Twitter.self])
This is what my ViewController looks like:
#IBOutlet private weak var twitterLoginButton: TWTRLogInButton!
// and
twitterLoginButton.logInCompletion = {(session, error) in
if error != nil {
print("ERROR: \(error)")
} else {
if let unwrappedSession = session {
print(unwrappedSession.userName)
}
}
}
Twitter.sharedInstance().logIn { (session, error) in
if let unwrappedSession = session {
print("Signed in as: \(unwrappedSession.userName)")
} else {
print("ERROR: \(error)")
}
}
Fabric documentation says that the first default for login is to go through the Twitter app (that may be why your user is automatically authenticated if they're already logged in the app), otherwise it will go through the webAuth login flow.
"To force the log in flow to use the web OAuth flow pass the TWTRLoginMethodWebBased method to the relevant log in methods."
// If using the TWTRLoginButton
let logInButton = TWTRLogInButton() { session, error in
}
logInButton.loginMethods = [.webBased]
So if you want to force the user to go through the web flow, try adding to your code: twitterLoginButton.loginMethods = [.webBased]

Login impossible with Spotify iOS 9

I can't login with ios sdk spotify.
I followed the Brian's tutorial (https://www.youtube.com/watch?v=GeO00YdJ3cE) and there is a difference with the current spotify tutorial :
In the video it talks about token exchange and show a spotify webpage. However on current spotify webpage the paragraph is missing.
Does this exchange token must be installed ?
I defined all elements in my spotify app account.
I also defined in the URL schemes : "spotify-action", "my-app-Name" but I can't sucess login.
Anyone help please ?
There are two options:
1) Implicit Grant Flow - grants users access tokens that will expire in 60 minutes - it is much simpler but has it's limits if you want to build a usable application. I will give you a simple example with spotify's updated sdk framework (you don't have to use safari)..
class ViewController: UIViewController, SPTAuthViewDelegate {
let kclientID = ""
let kcallbackURL = ""
#IBAction func loginSpotify(sender: AnyObject){
SPTAuth.defaultInstance().clientID = kclientID
SPTAuth.defaultInstance().redirectURL = NSURL(string: kcallbackURL)
SPTAuth.defaultInstance().requestedScopes = [SPTAuthStreamingScope]
SPTAuth.defaultInstance().sessionUserDefaultsKey = "SpotifySession"
SPTAuth.defaultInstance().tokenSwapURL = NSURL(string: ktokenSwapURL) //you will not need this initially, unless you want to refresh tokens
SPTAuth.defaultInstance().tokenRefreshURL = NSURL(string: ktokenRefreshServiceURL)//you will not need this unless you want to refresh tokens
spotifyAuthViewController = SPTAuthViewController.authenticationViewController()
spotifyAuthViewController.delegate = self
spotifyAuthViewController.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
spotifyAuthViewController.definesPresentationContext = true
presentViewController(spotifyAuthViewController, animated: false, completion: nil)
}
func authenticationViewController(authenticationViewController: SPTAuthViewController!, didLoginWithSession session: SPTSession!) {
print("Logged In")
}
func authenticationViewController(authenticationViewController: SPTAuthViewController!, didFailToLogin error: NSError!) {
print("Failed to Log In")
print(error)
authenticationViewController.clearCookies(nil)
}
func authenticationViewControllerDidCancelLogin(authenticationViewController: SPTAuthViewController!) {
print("User Canceled Log In")
authenticationViewController.clearCookies(nil)
}
}
2) Authorization Code Flow - Spotify's authentication server sends an encrypted refresh token which you store, for example,
SPTAuth.defaultInstance().sessionUserDefaultsKey = "SpotifySession". When that sessions expires you must trigger a function on your server... Hope this helps you get started

After signing into Instagram in my iOS app how can I sign in as a different user?

So I am building an iOS with instagram integration. I registered my app with instagram and have everything working so far. When the user clicks the ADD ACCOUNT button, I load the instagram auth url (https://instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=token) into a webview and the instagram login page appears.
I can then successfully login, my redirect then redirects correctly, and I can then exchange the code for an access token, close the webview, and display an alert saying that login was successful. But when the user clicks the ADD ACCOUNT button again and the webview loads the auth url, instead of seeing the instagram login page, it automatically authorizes and the successful login alert appears.
Is the webview caching something? How would I prevent this?
try clear caches and cookies
clear caches:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
to remove cookies check those two questions:
How to delete all cookies of UIWebView?
Delete cookies UIWebView
In Swift :
class func clearInstagramCookies() {
let cookieStore = NSHTTPCookieStorage.sharedHTTPCookieStorage()
guard let cookies = cookieStore.cookies where cookies.count > 0 else { return }
for cookie in cookies {
if cookie.domain == "www.instagram.com" || cookie.domain == "api.instagram.com" {
cookieStore.deleteCookie(cookie)
}
}
}
You must clear cookies of Instagram if you want to log the user out or if you need the sign-in page to appear again in UIWebView.
EDIT : Swift3
let cookieStore = HTTPCookieStorage.shared
guard let cookies = cookieStore.cookies, cookies.count > 0 else { return }
for cookie in cookies {
if cookie.domain == "www.instagram.com" || cookie.domain == "api.instagram.com" {
cookieStore.deleteCookie(cookie)
}
}
You can clear session and cookies by:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {
[[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
}
it took me sometime to figure this out too.
I am working on an web app, so i got no option of clearing the cache. It seems however got very little to do with caching or cookies. Once your Instagram app is authorised by the particular user, pointing to that Auth URL will just automatically bring you back to your redirect URL.
Your user will have to manual revoke your apps rights at https://instagram.com/accounts/manage_access to be able to view the login screen again.

Resources