Prevent UIWebView Pop Out to Safari, But Still Load Page - ios

I'm working on an iOS application that uses a lot of UIWebViews. Is there are way to prevent the UIWebView from popping out to Safari (iFrame code) but still load the page?
In other words return YES in webView:shouldStartLoadWithRequest:navigationType: but not let the web page pop out to Safari.
EDIT:
The web view side has processes that need to be executed, which is why I need to return YES. But the processes also inadvertently cause the application to launch the URL in Safari. I'm looking for a way to intercept that, and choose whether or not to launch the URL in Safari.

Apple introduced a Safari View Controller on iOS 9. Safari View Controller is a pre-built browser that has all the bells and whistles of Mobile Safari, but can be presented without leaving your app.
First we need to import Safari Services
#import <SafariServices/SafariServices.h>
For Objective C:-
NSString *yourUrl = #"http://yourURL";
SFSafariViewController *svc= [[SFSafariViewController alloc] initWithURL:[NSURL URLWithString: yourUrl]];
[self presentViewController:svc animated:YES completion:nil];
For Swift:-
let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
self.presentViewController(svc, animated: true, completion: nil)

Related

How do I open a single URL with no tabs shown with Objective-C, but with an OK button?

I'm trying to open a webpage in order to let users register in my app, but when doing so using openURL
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://example.com"]];
A full window of safari is opened, and my URL added to the open tabs.
Is it there any way I can open this single URL, with no tabs or URL bar, but still with navigation controls and a OK button to close the window?
I'm trying to do something similar to how Chrome Custom Tabs work on Android
You can try SFSafariViewController (swift syntax):
let safari = SFSafariViewController(url: url)
self.present(safari, animated: false, completion: nil)
Just import SafariServices for it.

SFSafariViewController doesn't open url in mobile mode

I have simple code
NSURL *url = [NSURL URLWithString:#"https://en.wikipedia.org/wiki/Cat"];
if ([SFSafariViewController class] != nil) {
SFSafariViewController *sfvc = [[SFSafariViewController alloc] initWithURL:url];
[self presentViewController:sfvc animated:YES completion:nil];
} else {
[[UIApplication sharedApplication] openURL:url];
}
I've tested on iOS 9.3. When I first open url I can see the page in mobile mode.
(Picture 1)
Next I click Desktop. And I can see this page (Picture 2)
(Picture 2)
I restarted application and SFSafariViewController still opens page in Desktop mode (Picture 2).
Can I force open the url in the mobile mode using SFSafariViewController and How Can I do this?
The "Desktop" link in this context means the user will see what s/he would normally see when clicking on their Macintosh or PC. This is expected behavior.
If you suspend and then resume the application, especially when SFSafariViewController is visible to the user, the app will resume on the same page where it was left off, unless you tell it to re-open the original URL (which you can do by having your application delegate watch for applicationWillEnterForeground: and then force reload the view controller).
if it were working fine, then all you need to do is to click mobile view in wikipedia footer, otherwise rebuild your app and clean it, it will restore the default behavior
The final solution that I found, It's using specific links with a prefix (m.) that the server supports, It will force open the urls with the mobile mode:
NSURL *url = [NSURL URLWithString:#"https://en.m.wikipedia.org/wiki/Cat"];

iOS in-App browser

Hey so I am building an iOS RSS Reader News app. Some long RSS articles end with a "Read more" link. After clicking "Read more", Safari opens up. I want it to - instead of having Safari open that link - open it inside the app.
Note: I need this to function permamently with every link in the app and without me having to manually change what links will be opened inside the app.
Create a WebViewController, just a regular ViewController with a webView with the size of the screen. So then you just
let webViewController = WebViewController()
webViewController.title = yourRSSItem.title
webViewController.request = NSURLRequest(URL: NSURL(string: yourRSSItem.url)!)
self.navigationController?.pushViewController(webViewController, animated: true) //you can present it as well

Iphone app that launches a website without url bar

I need to make a simple app for iOS. After opening it, i want it to open up safari with a certain website and that the address bar wouldn't show.
I'm completely new to iOS apps and I couldn't find anything similar from google. Maybe somebody has done it and can share the code or point me to somewhere where I can find it?
You can simply use a UIWebView, which by default doesn't include any controls or an address bar.
Class documentation here
Here is a small example of usage:
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"www.google.ie"]]];
[self.view addSubview:webView];
This will effectively give you a browser view inside your application, without invoking Safari itself.
You can embed this UIWebView in a UINavigationController, or use it anywhere else in your applications view hierarchy.
One final point, the UIWebView class has many delegate methods which you can implement, these methods will be called by the system when a given event happens (URL loads, has errors, etc).

open url with back button

I have one little truble.
I want to open URL in my iOS application.
The way I found on the web was :
[[UIApplication sharedApplication] openURL:url];
and it works fine, but this approach close my app and open safari without back button to return in my app. How to achieve this?
I would like something like
MFMessageComposeViewController * picker = [[MFMessageComposeViewController alloc] init];
[self presentModalViewController:picker animated:YES];
Rather than opening the URL in Safari why not just create a new view controller containing a UIWebView?
Show the view controller, pass in the URL, and load the URL into the UIWebView. Something like this:
MyWebViewController *mwvc = [[MyWebViewController alloc] initWithURL:myURL];
[self.navigationController pushViewController:mwvc animated:YES];
Then in MyWebViewController:initWithURL
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];
[myWebView loadRequest:request];
Safari is the application provided by Apple itself, so one cannot make change in behaviour of Sarari.
If you require a back button then create your custom UIViewController using UIWebView and place your back button over there.
For back functionality, you can either use UINavigationController or UIViewAnimation.
To get the control back to main native iOS application, you can URLScheme concept. URLScheme concept gives you unique identifier url of a native iOS application.
Check this http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/
It gives you fair idea on the procedure to achieve your requirement.

Resources