Facebook iOS SDK not storing cookies for access - ios

This is a really weird error. When login, the facebook object is not storing any cookies on safari on the device, but it does on the simulator. How do I know?
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
NSString* domainName = [cookie domain];
NSLog(domainName);
NSRange domainRange = [domainName rangeOfString:#"facebook"];
if(domainRange.length > 0)
{
[storage deleteCookie:cookie];
}
}
While running on Simulator I can see two cookie's name. I get zero on the device. This is happening even with the DemoApp from facebook.
I don't have a clue about where is this coming from, so I really don't know what info could I provide in order to make the post more complete. Please, request me anything I'm missing now. The code is just from the DemoApp, I haven't changed/added/removed anything.
Thanks.

You may be running into an odd thing with the iOS library.
Remember that every iOS app has separate storage space - they are kept that way so that another app can't touch things that do not belong to it. This sandboxing prevents malicious or badly made apps from hurting anything other than themselves.
This is true for Safari on the iPhone - cookies set for a session in Safari ARE NOT available to your app. Cookies set in a UIWebView in your app ARE NOT available to Safari. There is some confusion about this since on the desktop OSX system, you can share cookies. You can not do so on iOS, despite the class method's tempting name, SharedHTTPCookieStorage.
The Facebook SDK authentication code can try up to three ways to authenticate on a device... It can call the Facebook app, and ask to authenticate through it, allowing users of that app to only log in once. If the FB app is not installed, it opens a window in the mobile safari browser and tries to login there, setting cookies in the browser's sandbox if it can. If it can open neither, it opens a UIWebView in your app's space and stores its cookies local to your app.
I would guess that the authentication is happening in different places in your simulator and on your iPhone, and so the cookies live somewhere else.
What is really frustrating is that if you utilize the nice single sign on feature, you cannot then open UIWebviews to Facebook in your app without forcing a second logon. The cookies are not available to you.

Related

ios app open safari in background

I have an app that logs you in via opening safari and redirecting you back to the app - however on "Log out" I need to open safari in order to log you out - is there a way to do this in the background instead?
For Log in:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[DataService loginURL]]];
However, I would like to not have to do the same for Logout - and simply get safari to run in the background or something similar. The problem is that the cookies are saved, and I need to get rid of them. Can I get safari to open a website without having to open Safari through the application?
Can you open Safari in the background? I'm pretty certain that the answer to that is no. In any case, if your main concern is to delete the cookies then you may not get the chance - the user could just kill the app, and the cookies would sit in Safari.
Is it an option to use a UIWebView or WKWebView to perform authentication? So rather than taking them off to Safari, the user would see the browser content actually inside the app.
That might improve the user experience, and you'd get a lot more control. For example, you could point the web view to your log out url when the time came for it. That might save you some cookie hassles too.
Besides which, I think openURL: is now deprecated because it was involved in some shenanigans.

Webpage cookies are not detected by UIWebView

I am developing iOS app that loads a webpage using UIWebView
The web page sets four cookies:
Two for Google Analytics, _ga and _gat
One for my web application unique vistors counter
One for my webpage to detect whether user has voted or not.
If this cookie available then display the poll results.
From Firefox's Storage Inspector I see all my cookies as expected, see screenshot:
However, from iOS I printed NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies from inside webViewDidFinishLoad and there is only two of four expected cookies, also iOS users can vote as many times as they wanted because cookies are not detected (or not stored in iOS device):
if let cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies {
for c in cookies {
print("*******************")
print("name: \(c.name)")
print("domain: \(c.domain)")
print("value: \(c.value)")
print("path: \(c.path)")
print("expiresDate: \(c.expiresDate)")
}
}
Why my iOS app does not detect my custom cookies and only detects my Google Analytics ones?
NOTE 1: I have also added this code to my app:
func applicationDidBecomeActive(application: UIApplication) {
NSHTTPCookieStorage.sharedHTTPCookieStorage().cookieAcceptPolicy = NSHTTPCookieAcceptPolicy.Always
}
NOTE 2: My backend is SharePoint web application
A few ideas:
The device's Safari "Block Cookies" settings override any changes to the cookieAcceptPolicy. The default policy of this setting on the device is "Allow from Websites I Visit," which depending on your iOS device's version, allows third-party cookies. Testing with iOS 9.3.x shows that third party cookies are persisted in a UIWebView with the default settings, but do not persist with the settings "Allow from Current Website Only" or "Always Block."
Check if the device persists your cookie with the different Safari cookie settings.
Additionally, does your cookie have an expiration date set? I've found that cookies without an expiration date set are treated as session cookies in a UIWebView, which only persist with the lifecycle of the web view that received it.

FBSessionLoginBehaviorWithNoFallbackToWebView opens safari if no app is installed

I want to log the user into my app with Facebook using (in this order) these methods:
The account stored in settings (via the Accounts Framework)
Switching to the Facebook App
Displaying my own UIWebView inside the app (and sending the requests
manually)
If one method doesn't work, I want to fallback to the next.
The problem I'm having is that
[self.fbsession openWithBehavior:FBSessionLoginBehaviorForcingWebView
completionHandler:
opens Safari or shows a popup that I can't customise.
I absolutely need to follow this order because I use a webview for other social networks (ie Twitter, LinkedIn and GooglePlus).
Is there any way to manually open the Facebook App for the Oauth flow ?
To manually check if the Facebook app is installed you can use:
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fb://requests"]]

URL shemes for apps

I wanted to open Facebook and Twitter apps from my app. I find very simple way like:
NSURL *url = [NSURL URLWithString:#"fb://profile"];
[[UIApplication sharedApplication] openURL:url];
And there are many others url for open different page of Facebook. How can I check if Facebook app exist on iPhone? What happens if the application is not install on device? Now I try to use this method on iOS Simulator but nothing happened. It's bug of iOS Simulator? Only tomorrow I will have chance to check on device.
You can check if a URL would likely open ahead of time using the canOpenURL method of UIApplication.
http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/canOpenURL:
Also! You can pass values for the facebook page you'd like. This Stack Overflow includes a link to a wiki with all the variations:
Launching Facebook and Twitter application from other iOS app
If you know the URL scheme that the other app is using (e.g. fb:// for the Facebook app), you can first ask whether your UIApplication object can open such URLs in the first place, with canOpenURL:. You can use this, for instance, to decide whether to display a link at all, or whether to direct the user to Safari (with a normal web URL) instead.
As third-party apps cannot be installed on the Simulator as far as I know, the only way to test compatibility with them is on an actual device.

Is it possible to track cookies etc on an Apple Device i.e. Iphone, Ipad

I run an affiliate site (voucher code) and I am in the process of looking at developing an IOS app that will potentially display the vouchers etc on the app. Does anyone know if affiliate tracking links run as they are intended when opening a link on an Iphone device. Also can the link open the main Safari app or does the browsing have to be within the voucher app?
So an example being you click the link, it refers you to the affiliate network. The network stores a cookie on the device and you are directed to the site. Do cookies store as intended on a mobile device?
Thanks
Look at NSHTTPCookieStorage, it may help you.
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [storage cookiesForURL: [NSURL URLWithString: #"http://google.com/"]];

Resources