I'm looking for some help on how to make the webview of my swift 2 app appear to be Safari. At least I think that's what I need to do!
The root problem here is that the URL I am opening does not uses cookies as it should (a cookie should be set on load). Because the cookie is not being set when the page loads the ASP.NET based website is redirecting me to the landing page. The URL I am using has a query string which tells the site it's fine to land on the page I am linking to and creates the cookie to prevent the landing page redirect. All of this works fine on the desktop, and in safari on the iPhone, but when I open it in a webview inside my app it fails to create the cookie and redirects me to the landing page of the site.
Now that you have the background, I'll move on to what I believe is the fix for this exact problem that I found here: Enable Cookies in UIWebView (iPhone)
The answer given is objective C based, but it does imply that this problem is not in my imagination, and indeed related to ASP.NET websites and webviews in iOS apps.
Effectively, the following is what I am trying to achieve in swift, but I am still in the early stages of learning swift and currently have no idea how to tackle this.
// DON'T try to reuse a UIWebView for this.
UIWebView *wv = [[UIWebView alloc] initWithFrame:CGRectZero];
// This webview has already decided to use the default user agent string.
// let's use javascript to get the existing user agent string
NSString *userAgent = [wv stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// let's tack on some stuff to make ASP.NET happy
userAgent = [userAgent stringByAppendingString:@" Version/7.0 Safari/9537.53"];
[[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent": userAgent}];
// New UIWebViews inited after here will use the user agent string you made.
I tried to do something like this but I must be doing it wrong as it didn't make any difference.
let userAgent = UIWebView().stringByEvaluatingJavaScriptFromString("navigator.userAgent")! + " Version/7.0 Safari/9537.53"
NSUserDefaults.standardUserDefaults().registerDefaults(["UserAgent" : userAgent])
Many thanks in advance for any advice on this.