Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 10    Views: 718

Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
03/10/14 08:41 AM (10 years ago)

UIWebView open ads link in Safari

HI Everyone. I need some help with programming UIWebView in Xcode. I have a XIB file there I have I UIWebView. In my viewController.h @interface Ml_radio : BT_viewController <UIWebViewDelegate>{ IBOutlet UIWebView *adbanner; } @property(nonatomic, retain)IBOutlet UIWebView *adbanner; in my ViewController.m -(void)viewDidLoad{ [BT_debugger showIt:self theMessage:@"viewDidLoad"]; [super viewDidLoad]; adbanner.delegate = self; NSString *adbannerURL = @"http://raggarradio.se/mobilads/iphone.html"; NSURL *adbannersurl = [NSURL URLWithString:adbannerURL]; NSURLRequest *requestObj2 = [NSURLRequest requestWithURL:adbannersurl]; [adbanner loadRequest:requestObj2]; } I have try this - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { static NSString *reguler_exp = @"^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9-]*[a-zA-Z0-9])[.])+([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$"; //Regular expression to detect those certain tags..You can play with this regular expression based on your requirement.. NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", reguler_exp]; //predicate the matched tags. if ([resultPredicate evaluateWithObject:request.URL.host]) { [[UIApplication sharedApplication] openURL:request.URL]; return YES; } else { return NO; } } AND I HAVE TRY THIS -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType { if ( inType == UIWebViewNavigationTypeLinkClicked ) { [[UIApplication sharedApplication] openURL:[inRequest URL]]; return NO; } return YES; } No of them work. My google ads banner open in my webView but when i click on it, it open the link in my web view. I want it to open in Safari. I working on BT source code 2.0 I hope someone have done this before.
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/10/14 11:25 AM (10 years ago)
So close yet so far here is the code @interface Ml_radio : BT_viewController <UIWebViewDelegate>{ IBOutlet UIWebView *adbanner; } @property(nonatomic, retain)IBOutlet UIWebView *adbanner; in my ViewController.m -(void)viewDidLoad{ [BT_debugger showIt:self theMessage:@"viewDidLoad"]; [super viewDidLoad]; adbanner.delegate = self; NSString *adbannerURL = @"http://raggarradio.se/mobilads/iphone.html"; NSURL *adbannersurl = [NSURL URLWithString:adbannerURL]; NSURLRequest *requestObj2 = [NSURLRequest requestWithURL:adbannersurl]; [adbanner loadRequest:requestObj2]; } - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if ([request.URL.absoluteString rangeOfString:@"www.googleadservices.com"].location != NSNotFound) { // Should launch ads aslong as it remains loading from googleadservices return [[UIApplication sharedApplication] openURL:request.URL]; } else { //just a normal page do not go to safari do not pass go return YES; } }
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/10/14 11:29 AM (10 years ago)
Let's run through The method above is what happens when you click on a link normally you wouldn't need to touch this. What we have set up is an if statement that checks if the url contains the domain name of googleadservices. If it doesn't it's a normal site and will load normally as it did already, so you can use this over a mother web view if you liked. Where did i get google ad services from well it was obvious by the little cheeky adsense circle that is was a google add. If you copy and paste the link into a web browser and copy the link on the banner not press it you will find every ad generated starts with www.googleadservices.com Any way that should work for BT 3.0 or BT 2.0 as the web view hasn't changed All the very best
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
03/10/14 11:50 AM (10 years ago)
Nice it works. It open the banner ads link in safari. BUT when I open the app again the WebView open even the ads link in the Webview.
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/10/14 11:58 AM (10 years ago)
Apologies me taking a shortcut - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"Yippee %@",request.URL.absoluteString); if ([request.URL.absoluteString rangeOfString:@"googleadservices.com"].location != NSNotFound) { // Should launch ads aslong as it remains loading from googleadservices [[UIApplication sharedApplication] openURL:request.URL]; return NO; } else { //just a normal page do not go to safari do not pass go return YES; } }
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/10/14 11:59 AM (10 years ago)
Please note how it returns NO and the launch is done the line above
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
03/10/14 12:02 PM (10 years ago)
Now we talking!! Nice done kittsy, works great. Thank you for your support!
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/10/14 12:04 PM (10 years ago)
No problem happy to help. How often are you using these lines of code how many different types of screen.
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
03/19/14 10:13 AM (10 years ago)
Hi Kittsy. I wonder if I will pass all links to safari how can I do that? In your code you only pass googleseverice to safari. I need to pass all links. //Macki
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
03/20/14 03:53 AM (10 years ago)
HI Kittsy I have fix it. here is the code if someone else will use this. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { NSLog(@"Yippee %@",request.URL.absoluteString); if ([request.URL.absoluteString rangeOfString:@"googleadservices.com"].location != NSNotFound) { // Should launch ads aslong as it remains loading from googleadservices [[UIApplication sharedApplication] openURL:request.URL]; return NO; } if(navigationType == UIWebViewNavigationTypeLinkClicked) { [[UIApplication sharedApplication] openURL:[request URL]]; // We also need to tell our UIWebView not to do anything so we need to return NO. return NO; } else{ //just a normal page do not go to safari do not pass go return YES; } }
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
03/21/14 09:22 AM (10 years ago)
Kittsy I NEED HELP. My code didn't work. I clean my project and start it again, now is´t only this code thats work. if(navigationType == UIWebViewNavigationTypeLinkClicked) { [[UIApplication sharedApplication] openURL:[request URL]]; return NO; How can I make them both in same method? Please can you help me?
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.