Discussion Forums  >  Uncategorized

Replies: 6    Views: 1154

Aescleah
Code is Art
Profile
Posts: 43
Reg: Sep 15, 2011
Earth
430
11/08/11 12:48 AM (14 years ago)

Opening link in webview into a new webview (iOS)

Here's my issue: I have a tabbed app with an RSS reader, which loads a webview displaying the link element of a row when it's selected. Of course, there are sometimes links within the webview itself, and when those are clicked, they are normally displayed in the same webview (sorry to describe the basic behavior everyone knows, it's just so that everything is as clear as possible). I don't want to display a browser toolbar to give as much space as I can to the webview, so the problem is that if the user wants to go back to the previous screen once he clicked a link, he has to go back to the rss list, and then click the desired row again. I can see two options: launching safari when a link is clicked (which is easy to do), or pushing a new (modal) webview on top of the previous one. I don't want to use safari, as it takes the user out of the app. I know will have to use this to handle a clicked link: -(BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ if (navigationType == UIWebViewNavigationTypeLinkClicked) { ... So my question is, how to define there a method that would launch a new webview displaying the link clicked by the user? By clicking a back button, the user would dismiss this new view and would be taken back to the previous one. I've tried a few things, without success so far...
 
Aescleah
Code is Art
Profile
Posts: 43
Reg: Sep 15, 2011
Earth
430
like
11/08/11 08:21 AM (14 years ago)
I finally found a solution :) It required a bit more coding than I thought, but I got it working. In case anyone would be interested to have such a behavior in their app, let me know and I'll explain here what I did!
 
GoNorthWest
buzztouch Evangelist
Profile
Posts: 8197
Reg: Jun 24, 2011
Oro Valley, AZ
1,000,000
like
11/08/11 11:19 AM (14 years ago)
Feel free to add that info to the BTUGwiki.com as well...we're trying to collect procedures for various things there. Thanks! Mark
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/08/11 12:31 PM (14 years ago)
Hi Aescleah. Yes I want now how you fix that. Can you help me with that?
 
Aescleah
Code is Art
Profile
Posts: 43
Reg: Sep 15, 2011
Earth
430
like
11/10/11 04:29 AM (14 years ago)
Hi guys :) Mackimack, I'll try to be as clear as possible. The files you will have to modify are BT_screen_webView.h and BT_screen_webView.m. An important thing to understand is that once you have mofidied them, the changes will be visible on ALL the pages in your app that are displayed in a webView. If you want to have this feature on just a particular screen, the you should go for a custom plugin, which is what I did (just copy-paste the Buzztouch files, modify them, give them any name you want and transfer them to your project in the BT_Plugins folder). Now, the code: In the .h file, you will have to add these lines: NSURL *myURL; //this one goes int the interface, together with externalURL, dataURL... @property (nonatomic, retain) NSURL *myURL; //this one obviously goes with the other properties In the .m file now, where things are a bit longer: You will have to synthesize thz variable myURL, by adding this at the beginning of the file: @synthesize myURL; Next, add this anywhere in your file: - (id)initWithURL:(NSURL*)url { self = [super init]; if( self ) { myURL = [url retain]; } return self; } Then, you have to add a few lines in the webView delegate method, after -(BOOL)webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{ (should be around line 600): if( navigationType == UIWebViewNavigationTypeLinkClicked ) { BT_screen_webView* vc = [[BT_screen_webView alloc] initWithURL:[request URL]]; [self.navigationController pushViewController:vc animated:YES]; [vc release]; return NO; } return YES; Then, you will have to add this to the initload method, after [webView loadRequest:URLReq]; (around line 440): }else if (myURL!=nil){ [webView loadRequest:[NSURLRequest requestWithURL:myURL]]; Finally, you'll have to release myURL, by adding at the end of the file in the dealloc method: [myURL release]; myURL = nil; And that's it! If you have any question or problem, just post it here and I'll try to help. @ Mark I'll add this to the wiki when I have a bit more time, I'm pretty busy right now. Anyway, the wiki is an excellent idea, and I'll literally have a ton of stuff to put there!
 
Mackimack
Apple Fan
Profile
Posts: 481
Reg: Dec 30, 2010
Sweden
14,310
like
11/10/11 03:06 PM (14 years ago)
@aescleah Thanks for this information. I will try it out tomorrow.
 
BET
Aspiring developer
Profile
Posts: 6
Reg: Dec 31, 2011
Addis Ababa
360
like
05/04/12 11:09 AM (13 years ago)
Thanks for this info. I had a similar situation where I have a tabbed app with Menu - Buttons. When the button is tapped, it loads a custom url - webpage. Now, having added your modifications above, and after clicking a link within the webview, I am able to go back from the new webview to the previous webview (the first webview loaded after tapping the button). But when I want to go back to the button menu by clicking the back button, the app crashes in the iPad 4.3 simulator, if the webview had not loaded completely (internet connection is slow here:). The debugger shows "thread 1: Program received signal:"EXC_BAD_ACCESS" , the line "int retVal= UIApplicationMain(argc,argv,@"UIApplication",@"xxx_appDelegate")" being colored; I am analyzing the navigation controller stack. Any suggestions? Thank you
 

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.