Swift - Wrap a website into an Ios app? - ios

I'm quite new to swift & IOS development.
Is it possible to create an app in swift that will run a website inside of the app?
If so could someone point me in the right direction?
Thanks in advance.

You need to add UIWebView component or WKWebView component - tutorial can be found here: http://www.kinderas.com/technology/2014/6/7/getting-started-with-wkwebview-using-swift-in-ios-8

It is possible but it is going to be rejected by Apple in all probability unless you provide some more functionality, i.e., don't JUST wrap a website in an app and submit it.
Having said that, all you need to do is use an instance of UIWebView in your app and set the URL to load your website in viewDidLoad function
So your viewDidLoad function would look something like this:
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.apple.com")
var request = NSURLRequest(URL: url)
self.webView.loadRequest(request)
}

Related

How to set the default url for webview app in Xcode

I am pretty new to Xcode and app development.
I have a platform that has a homepage with a login form and the designated Login/Join page(s).
My webview app is built to open the main URL where the homepage is located, e.g domain.com.
How can I set up to show the login page when the app is open instead of the main URL, e.g. domain.com/signin
I assume I should be able to set it up in the Xcode. Also, if this change is made in the Xcode will it sync with my App Dev Account?
Thank you in advance!!
Assuming you are using a WKWebView you can load a new page as so:
var webView = WKWebView()
if let loginUrl = URL(string: "domain.com/signin") {
webView.load(URLRequest(url: url))
}
A couple resources for you to learn more about WebKit:
https://medium.com/swift-productions/create-a-web-view-xcode-12-swift-5-3-9806d41cc9b
https://www.hackingwithswift.com/read/4/2/creating-a-simple-browser-with-wkwebview

WKWebView loading too slow

I am working with WKWebView and it is loading page so slow. I have a link that I want to open in the app, so I searched for the solution and got that WKWebView is the right choice. but when I implemented it it takes 10 to 15 minutes with the loading.
however the time that is taken on android is about 20 seconds. I do not know what is the problem here. Is it the configurations problem or something else.
This is how I am doing it:
let url = URL(string: "https://www.google.com")!
webView = WKWebView()
webContainer.addSubview(webView)
webView.navigationDelegate = self
webView.load(URLRequest(url: url))
Here webContainer is a UIView that will be containing webView.
Please let me know what I am doing wrong and what could be problem here.
UPDATE 1:
THE LINK I AM USING IS OF WEBSITE THAT STREAMS A SECURITY CAMERA VIDEO
. I CANT SHARE IT SINCE THERE IS A SECURITY REASONS.
UPDATE 2:
However If I open www.google.com it also takes time.

how to access a sound file stored in another app in swift?

this question is a lot like Share data between two or more iPhone applications except:
I'm looking for a way to do this in an iOS 8+ (or 9+) application using swift
I want to be able to use the sound file contained in the first app so the easter egg I'm making is only available when the user has both apps installed
since this is part of an easter egg, i don't want to use any method that would cause anything extra to be displayed on screen such as a browser redirect or some kind of permission popup
(this basically rules out using the share sheet and custom url's to pass data as described in the post above)
I am using AVAudioPlayer and AVAudioSession in the first app to play the sound if that is at all helpful.
Use App Group
You can share actual NSData through the NSUserDefaults:
if let userDefaults = NSUserDefaults(suiteName: <group>) {
userDefaults.setObject(obj, forKey: key)
}
and retrieve from another app in the same group like so:
if let userDefaults = NSUserDefaults(suiteName: <group>) {
if let obj = userDefaults.objectForKey(key) {
// magic
}
}
It appears that the only limitation for passing data through the user defaults is the device storage capacity, and since NSUserDefaults accepts NSData as a storage format, it makes a prime candidate for sharing tidbits information.
If both apps are yours you can implement a custom url scheme in the second app, and then from the first app ask if it knows how to open an URL with that scheme. If the answer is yes, the app is installed. The function is called canOpenURL. It's an instance method of UIApplication.
I vaguely remember that in iOS 9 and later, Apple added a restriction that you have to register the URLs you are going to ask about in your info.plist, but I don't remember the details. That won't prevent this scheme from working, but it is an extra step you have to take.

How to Enable Cookies in UIWebView for iOS Swift

I have an app that uses Instagram Integration. The App has been in the App Store for a few months with multiple version all working fine. Suddenly, the latest version I submitted to Apple was rejected because apple doesn't like the fact that Safari is launched for a User to Login to Instagram. As per their words, they care about the user experience.
The problem is I need to request an Access Token and that is how I know it has to be done. Since my App was rejected, I can't be the only one who may have faced this so I thought I should ask. Does any one know of some other method perhaps I don't know of whereby you can authenticate a users Instagram and get the required Access Token, without Launching Safari?
EDIT #1: The only other option I can think of is to use a UIWebView within my app to open the Instagram authentication process. I tried this and it just seems to be mad slow and the callback process to my app doesn't seem to initiate. So I don't know if I'm doing something wrong.
EDIT #2: Doing more research as to why the Instagram login is not working in my UIWebView, it seems to be something with how the UIWebView handles Cookies, apparently it does not recognize Cookies or something along those lines. I still don't know what exactly to do to fix this, so if anyone has a solution please provide.
Looks like this is something that could help you. You haven't given many infos about your app so I can't think of something outside the box, but try looking into Instagram iPhone Hooks and see if you can use them to trigger login when the user does something.
I'm sure this can be optimized a little bit, I probably do not need the for loop. But either way, I've spent almost a day on this issue and just don't want to touch it anymore since I was finally able to get this to work by doing the following before loading the UIWebView:
var req = NSURLRequest(URL: NSURL(string: urlString)!)
var cookies = NSHTTPCookieStorage.sharedHTTPCookieStorage().cookies as! [NSHTTPCookie]
var reqCookies:[AnyObject] = []
for aCookie in cookies {
reqCookies += [aCookie]
}
var headers = NSHTTPCookie.requestHeaderFieldsWithCookies(reqCookies)
self.webView.loadRequest(req)

Change Safari URL with App Extension

When the icon of my Safari app extension icon is touched, I want to change the URL without showing a new view. Doing this comes down to two issues:
How can I use an safari app extension without showing a new view? I just want to perform a background action when the extension icon is pressed.
How can I change the current URL in Safari?
Actually, you might be able to change the URL at the finalize function of your ExtensionPreprocessingJS file
// Note that the finalize function is only available in iOS.
finalize: function(arguments) {
// arguments contains the value the extension provides in
[NSExtensionContext completeRequestReturningItems:completion:].
// In this example, the extension provides a color as a returning item.
document.URL = arguments["newUrl"];
}
It seems like what you're describing is a bookmark, and not an app extension. I would highly doubt that app extensions would allow you to interface with Safari and update the URL, this would be wrong in many regards, including privacy and security. In short, not possible (sorry).

Resources