WebView link to launch Safari? - html

I am using UI webview in xcode in my iphone mobile application..i am loading my website in webview but all link in website like(facebook,twitter,youtube etc..)are open in same webview not in mobile safari..
please suggest me how to do it
thanks in advance

see: UIWebView Launch in Safari
you need to examine the url to see if it belongs to facebook,twitter,youtube etc and if so launch accordingly
-(BOOL) adBox:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest
navigationType:(UIWebViewNavigationType)inType {
if (![inRequest.URL.host isEqualToString:#"www.mydomain.com"]) { //Test if it's NOT my site
if ( inType == UIWebViewNavigationTypeLinkClicked ) {
[[UIApplication sharedApplication] openURL:[inRequest URL]];
return NO; //Open in Safair
}
}
return YES; //Open in UIWebView
}

Related

How can i create an automatic redirect to AppStore or PlayStore based on Device?

For example: xyz.com/app-download
This domain (and only THIS!) shoudl redirect Android users in the PlayStore and iOS users in the AppStore.
If this domain is opened by Windows or Linux it should not redirect, but show the content, which is on the site!
This should preferrably be done by .htaccess file editing.
Can anyone come up with a nice and understandable solution for this?
Thanks a lot!
with a little Javascript you can achieve this, just add this code to the page
<script>
function redirectByMobileOS() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
//for Android
if (/android/i.test(userAgent)) {
window.location.href = "http://www.link-for-android.com";
}
//For iPhone / iPad / iPod (iOS
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
window.location.href = "http://www.link-for-iphone.com";
}
}
</script>
and call the function on page load from your <body> tag like this
<body onload="redirectByMobileOS()">
</body>

iOS: Excluding certain URLs when running webview

I am using Swift and have web-based content (an external webpage) embedded in WebView for my native app. On that webpage, again, which is in-app, I need to make one link open in Safari and not in the App. HTML target _blank code on the webpage doesn’t work (I wish it was that easy), looking for the right code to do it in Swift.
I have used this code for uiwebview:
#IBOutlet var news: UIWebView!
var theURL = "http://"
override func viewDidLoad() {
super.viewDidLoad()
loadWebPage()
}
func loadWebPage(){
let requestURL = NSURL (string: theURL)
let URLrequest = NSURLRequest (URL: requestURL!)
news.loadRequest(URLrequest)
I have used this code for WKNaviagtionDelegate:
func webView(webView: WKWebView, decidePolicyForNavigationAction navigationAction: WKNavigationAction, decisionHandler: ((WKNavigationActionPolicy) -> Void)) {
if (navigationAction.navigationType == WKNavigationType.LinkActivated && !navigationAction.request.URL!.host!.lowercaseString.hasPrefix("http://")) {
UIApplication.sharedApplication().openURL(navigationAction.request.URL!)
decisionHandler(WKNavigationActionPolicy.Cancel)
} else {
decisionHandler(WKNavigationActionPolicy.Allow)
}
Best, Drew
Because you need to create a button that looks like a link.
that should do the trick:
UIApplication.sharedApplication().openURL(NSURL(string: "http://...")!)
If the button is actually on a webpage in WebView... the link is not really controlled by the App unless you manipulate the link in iOS. It looks like the HTML target="_new" tag on the button might work in a later version of iOS. It's a bug in iOS 7 and was fixed in 7.0.3. Try a higher iOS version target for the App with the HTML target tag on the button.
How to open Safari from a WebApp in iOS 7

MoSync Reload captureImage working on html but not in webview

I have a html page that uses the camera on my iphone and returns an alert. It works fine as a web app but when i try to use native ui webview the camera still gets called but there is no alert. Any idea why?
function getImage()
{
navigator.device.capture.captureImage(
function(success){
alert('getImageSuccess');
},
function(error){
alert('getImageFailure');
}
);
}

What is the proper way to unload views in iOS 6 in a memory warning (Apple doc flaw)?

In iOS 6, viewWillUnload and viewDidUnload are deprecated and UIViewControllers no longer unload views that are not visible on screen during a memory warning. The View Controller Programming Guide has an example of how to manually restore this behavior.
Here is the code sample:
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Add code to clean up any of your own resources that are no longer necessary.
if ([self.view window] == nil)
{
// Add code to preserve data stored in the views that might be
// needed later.
// Add code to clean up other strong references to the view in
// the view hierarchy.
self.view = nil;
}
}
Below the code sample is the following note:
The next time the view property is accessed, the view is reloaded
exactly as it was the first time.
There is an obvious flaw here. If a view controller that has not loaded its view receives a memory warning it will load its view in the line if ([self.view window] == nil) and then proceed to clean up and release it again. At best, this is inefficient. At worst, it makes the memory conditions worse if a complex view hierarchy and supporting data are loaded. I verified this behavior in the iOS simulator.
I can certainly code around this but seems odd for Apple docs to have such an error. Am I missing something?
The correct check in a view controller for the view being loaded and on screen is:
if ([self isViewLoaded] && [self.view window] == nil)
My full solution in iOS 6 to have a view controller unload views and cleanup similar to iOS 5 is the following:
// will not be called in iOS 6, see iOS docs
- (void)viewWillUnload
{
[super viewWillUnload];
[self my_viewWillUnload];
}
// will not be called in iOS 6, see iOS docs
- (void)viewDidUnload
{
[super viewDidUnload];
[self my_viewDidUnload];
}
// in iOS 6, view is no longer unloaded so do it manually
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
if ([self isViewLoaded] && [self.view window] == nil) {
[self my_viewWillUnload];
self.view = nil;
[self my_viewDidUnload];
}
}
- (void)my_viewWillUnload
{
// prepare to unload view
}
- (void)my_viewDidUnload
{
// the view is unloaded, clean up as normal
}

How to make links shown in phonegaps childbrowser open in Safar?

I have a phone gap app which loads an HTML 5 app in a child browser. Links which are on the pages loaded in the child browser also load in the child browser. I would like these links to open in Safari. How could i do that?
Thanks
You just need to add this in your AppDelegate.m
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:#"http"] || [[url scheme] isEqualToString:#"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
}
Hope this helps