Discussion Forums  >  WebViews and HTML for Mobile

Replies: 17    Views: 466

tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
11/20/13 11:37 PM (10 years ago)

Open Link in Webview in App

Hello Everyone, I'm developing an Android app (buzztouch cp_v20) on Eclipse (Juno Service Release 2). I'm developing on a Macbook pro. My app is, essentially, a series of Webview components which the user browses. What I'm trying to do is to be able to email a link to a user (let's say, <a href="http://www.mywebsite.com/this/page?id=1234" target="_blank" rel="nofollow">http://www.mywebsite.com/this/page?id=1234</a>) and have that link be opened by my app in a webview. The link I send in the email won't be a pre-existing page on the app, as it is dynamic (note the id). I've already got my AndroidManifest.xml set up correctly (I think). Here's my intent: <intent-filter> <data android:scheme="https" android:host="www.mywebsite.com"/> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> </intent-filter> When I click on a link pointing to my website from, say, an email on my phone, it does launch my app, but (of course) it opens to the app's default page. I've tried using intent.getData on the BT_screen_customURL file, but it returns null. I figured the class probably has a parent activity class that creates a new intent to launch the customURL screen, and that I probably need to work from there, but I couldn't find it. I'm very new to developing on Android, so I'm still trying to wrap my head around how it all works. Any help on how I can achieve this would be greatly appreciated! Also, if it's not too much trouble, examples would be a big help. Best Regards
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/21/13 12:21 AM (10 years ago)
Hello Everyone, Update: I've managed to locate the intent. By using: "BT_debugger.showIt("tom_tom " + getIntent());" I've managed to get the output I was looking for: tom_tom Intent { act=android.intent.action.VIEW cat=[android.intent.category.BROWSABLE] dat=https://www.mywebsite.com/test.html flg=0x3c00000 cmp=com.mywebsite/.BT_activity_root } So now that I have that...Can anyone help me open the link in a Webview? Any help would be greatly appreciated.
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/21/13 02:05 AM (10 years ago)
Hello Everyone, Update: I managed to solve the issue on my own. Even thought I fought with this all day yesterday and got no where, it seems today was my lucky day. Here's how I did it (for anyone who has comments or anyone who wants to do the same thing). 1. Setup the intent in the AndroidManifest.xml (code in my first post). 2. In the BT_activity_root class, Open a new field (public static String) which will hold the value. Set it equal to getIntent().getDataString(); during the onCreate method. This will now hold the URL that the user clicked on to launch your app. I called the field customDataURL. 3. On line 173 of the BT_screen_customURL file, write an if statement that will check if the variable is not null, and if so, set the variable dataURL equal to your variable. My code looks like this: if (BT_activity_root.customDataURL != null) {dataURL = BT_activity_root.customDataURL; } The customDataURL variable will always be null if the user opens the app without clicking on a link (as far as I can tell). Opening and browsing the app normally works smoothly. Comments welcome!
 
farcat
buzztouch Evangelist
Profile
Posts: 1008
Reg: Jan 27, 2012
France
13,230
like
11/21/13 02:46 AM (10 years ago)
Well done and thanks for taking the time to update with the solution :) Farcat
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/21/13 04:37 AM (10 years ago)
Hello Farcat, Thanks for the kind words. You have the most curious profile picture! I now have the great pleasure of figuring it out all over again, just for the iPhone. Any tips?
 
farcat
buzztouch Evangelist
Profile
Posts: 1008
Reg: Jan 27, 2012
France
13,230
like
11/21/13 04:42 AM (10 years ago)
My profile reflect my state of mind when I am forced to code... Your question is beyond my ability but maybe one the other members van jump in? Farcat
 
Niraj
buzztouch Evangelist
Profile
Posts: 2943
Reg: Jul 11, 2012
Cerritos
37,930
like
11/21/13 09:15 AM (10 years ago)
Very nice work in solving that on the Android side-- excellent!!! :-) I had solved this in iOS-land for iOS 6. https://www.buzztouch.com/resources/ Search for my name on that page. Download and read that document for knowledge. But that technique does not work for the just-now-released V3 of BuzzTouch (Nov-2013). Good news -- a few other folks got their heads together to make it work for BT3 on iOS 7! I will ask Susan to respond to this post with an updated set of information. It would be cool to see that member badge under your avatar! :-) -- Niraj
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
11/21/13 06:57 PM (10 years ago)
Thank you, @tomtom! It is really cool what you can do with url schemes in iOS, and so awesome that you figured it out for all of us on Android.
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/22/13 02:57 AM (10 years ago)
Hello Niraj and Susan, Thank you for all the kind words! I'm really glad I was able to contribute to the community. I'm a little disappointed with IOS for only supporting custom schemes. I think Android's support for intercepting links to specific URLs is extremely effective, as the native browser acts as a fallback if the app isn't installed. Niraj, I found your tutorial, which was very well written, but like you pointed out, the new core doesn't support it :(. I've had some success though with the IOS side. Kittsy has a good tutorial on how to setup the custom scheme to launch the app, but I had problems using his code. What I was able to do was setup a function in the appDelegate.m file which launches correctly and has the URL when the custom scheme is visited. I'm having some issues setting up the actual WebView though. Right now, my code looks like this: - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { UIWebView *webView=[[UIWebView alloc] initWithFrame:CGRectMake(0,0,320,480)]; [self.view addSubView:webView]; NSURLRequest *urlRequest; NSURL *urlforWebView; urlforWebView=[NSURL URLWithString:@"http://www.google.com"]; urlRequest=[NSURLRequest requestWithURL:urlforWebView]; [webView loadRequest:urlRequest]; } I put it just before the dealloc. Yes, I know it'll only load google.com, but that's usually where I start from. Right now, it's throwing me an error saying "Property "view" not found on object of type "myapp_appDelegate". Any help on how I could properly create and display the WebView would be greatly appreciated.
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
11/22/13 07:33 AM (10 years ago)
@tomtom, will you send me an email to [email protected]? I will give you a replacement appDelegate file. I have been very busy so I don't have a tutorial, but I think you will have what you need if you swap out this file.
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
11/25/13 04:28 PM (10 years ago)
@tomtom, I need to put a link in an app back from a PDF back to a certain page in my app. Do you think this will work for that? The PDF reader is outside of my app.
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/26/13 01:10 AM (10 years ago)
Hello Susan, If I understand correctly, you have a PDF file open in an independent PDF reader app, and you'd like to link to a specific page in your app (correct me if I'm wrong). It will work, but it will require some modification. The code I wrote is designed specifically to open pages that don't exist in the app and launch them in a Webview. In my case, I wanted to open a dynamic receipt page from my website in the app, and I wanted to email my users a link which will launch their receipt in either my app or the browser. Because the page was dynamic, I couldn't link to it using buzztouch menus, as the URL contains variables that need to change. What I'd recommend is to follow what I did up to step 2 (setup custom scheme, create field in BT_activity_root class, set field equal to getIntent().getDataString() within onCreate), then setup a link in your PDF file which will include the ID of the buzztouch menu page you'd like to open. The field you setup in the BT_activity_root class should contain the id of the page when the app is accessed via the link in your PDF. From there, it's just instructing the app to load the requested page. Regards, Tomtom
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/27/13 12:10 AM (10 years ago)
Hello Everyone, I was wondering if someone could help me with the iOS implementation. I'm working with this code right now: - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; NSString(*myString) = @"http://mysite.com"; myString = [myString stringByAppendingString:[url path]]; myString = [myString stringByAppendingString:@"?"]; myString = [myString stringByAppendingString:[url query]]; int browserHeight = self.window.bounds.size.height; int browserWidth = self.window.bounds.size.width; UIWebView *webView = [[[UIWebView alloc] initWithFrame:CGRectMake(0, 20, browserWidth, browserHeight)] autorelease]; NSURL *dataURL = [NSURL URLWithString:myString]; NSURLRequest *request = [NSURLRequest requestWithURL:dataURL]; [webView loadRequest:request]; webView.scalesPageToFit = YES; webView.backgroundColor = [UIColor clearColor]; [self.window addSubview:webView]; [self.window makeKeyAndVisible]; return YES; } This works on a very basic level in terms of opening the link in a webView, but it's very dirty. It's essentially a mini-version of the customURL plugin, but it's missing a lot of the customURL functionality. Instead of working this way, I was wondering if anyone can help me pass the dataURL over to the BT_screen_customURL.m file. That way, I'll be able to load it within the native buzztouch customURL plugin. Thanks! Regards, Tomtom
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/27/13 01:15 AM (10 years ago)
Hello Everyone, Update: I found a partial solution. In myapp_appDelegate.h, I added a new property: @property (nonatomic, retain) NSString *customURL; In the myapp_appDelegate.m, I used the following function to set it equal to the URL of the link: - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { NSString(*myString) = @"http://mysite.com"; myString = [myString stringByAppendingString:[url path]]; myString = [myString stringByAppendingString:@"?"]; myString = [myString stringByAppendingString:[url query]]; self.customURL = myString; return YES; } Finally, in the BT_screen_customURL.m file, I found this line (#66): myApp_appDelegate *appDelegate = (myApp_appDelegate *)[[UIApplication sharedApplication] delegate]; and put this immediately below it: if(appDelegate.tomURL != nil) {self.dataURL = appDelegate.tomURL; } Now, when I click a link, it launches the app, but not the actually linked page. When I click refresh, then the page I clicked on loads. I'm assuming some refresh needs to be performed in order for the linked page to be displayed. Anyone got any ideas? I'll update if I find a solution. Regards, Tomtom
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
11/27/13 12:44 PM (10 years ago)
I'm not going to lie this looks friction confusing. let me get this straight you want a custom url from an email to open your app and present a dynamically loaded web screen from the customURL geez. What I would do and I can't really write much but in 24 hours once this stupid uni assignment is done, I will give you a full run down. What I will do is post the steps and then some of the others can guide you through it. you are entirely right with passing it to a custom url all you need to do is learn how to create a custom url screen from scratch. your custom url needs to be in the format myApp://mywebsite.com?whateverCrapHere you then parse the code in your handle openurl parse it so only this appears "mywebsite.com?whateverCrapHere" append the url so it looks like http://www.mywebsite.com?whateverCrapHere the create the BTitem give it a blah nickname, id, tell it its a custom url, then add the jsonvars you require noting that the data url will now be the string version of your url. then load the screen copy some code from some other programs that load other screens, if you have the countdown or voucher plugin it has the method in there. (I am only suggesting them plugins as it only has one screen to load as opposed to a cmore complicated tableview method) and that should be it. Simple lol
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/27/13 11:43 PM (10 years ago)
Hello Kittsy, Thanks for the tip! I actually had a similar solution working. What I did is simply create a webView component within the function that handles the app when it is launched via custom scheme. The issue I had is with setting up my own webview. In essence, I had it working, and it worked alright, but there was no single sign-on between the webViews. So a person could be logged in while browsing my app (in the custom URL screen), and later when clicking a link in their email, they'd be directed to the new webView, and they'd have to login again. Also, I found myself copying a lot of the code that the buzztouch customURL class already creates, particularly those involving the size of the browser, and as a developer, I HATE writing the same code twice, so I figured it might be more wise to simply pass the URL I want loaded to the customURL class, and instruct it to load the URL I supply instead of the default page when the app is launched via custom scheme. It works pretty well too, the only issue is that I have to reload the webView manually in order to see the new page, otherwise it just shows me the last page I was on in the app (retrieves it from the background). Do you have any ideas how I can solve this reload issue? Thank you for your time and help, and Good luck with that Uni assignment, I feel your pain! Regards, Tomtom
 
Susan Metoxen
buzztouch Evangelist
Profile
Posts: 1706
Reg: May 01, 2011
Hopkins, Minnes...
26,260
like
11/28/13 09:26 PM (10 years ago)
I am still trying to get this to work in Android. My problem is a bit different in that the user picks a PDF in the app, they leave the app to view the PDF, then I need to link them back to a specified screen the app with a URL in the PDF. We are ok with limited access to one PDF viewer--the Adobe Reader if that would work. We would just tell users that for best performance you must use the "Adobe Reader for Android". Will this work? Am I on the right track? Here are the instructions from the Android developer site: http://developer.android.com/training/basics/intents/filters.html I have this in the manifest--I added the intent to the BT_activity_host activity: <!-- BT_activity_host runs after startup and after splash screen (splash screens are not required --> <activity android:name=".BT_activity_host" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize" android:theme="@style/hostThemeWithTitle" android:uiOptions="splitActionBarWhenNarrow"> <!-- This activity intent-filter is for the mainelink:// url scheme --> <intent-filter> <data android:scheme="mainelink" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.BROWSABLE"/> </intent-filter> </activity> And this is what I have so far in onCreate in the BT_activity_host.java file: // Get the intent that started this activity Intent maineLinkIntent = getIntent(); Uri data = maineLinkIntent.getData(); // Figure out what to do based on the intent type if (maineLinkIntent.getType().equals("mainelink")) { BT_debugger.showIt(activityName + ": a PDF is linking back to the app via mainelink"); //get the next screen Nickname from what follows mainelink:// in the url //set the next screen to what the screen Nickname is }
 
tomtom
Aspiring developer
Profile
Posts: 20
Reg: Jan 18, 2011
location unknow...
1,450
like
11/28/13 11:29 PM (10 years ago)
Hello Susan, Yes, I think you are definitely on the right track. I haven't debugged the code or checked it for syntax issues, but everything appears to be correct. If the getIntent().getData doesn't work, try putting it into BT_activity_root.java instead. Regards, tomtom
 

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.