iOS Web view into an Alert!

Previous topic - Next topic

DaCarSoft

Hi...

I'm translating today all my wrappers published under the spanish forum :P

This is a code that shows a little web view embedded into an alert...

I tried to make it pretty with the functions to make the rounded corners to the Web control...   using the Quartz core library.

Please, test it with care, because I wrote it so fast...

WARNING: I don't know If Apple may reject an App that uses this code, because it does not use illegal or undocumented functions, but uses an alert in a very strange way that bypasses the purpose of an alert...   XDDDDDDDDD

PLEASE, paste in your reply some photos to see the results, I have no time today to paste it myself...

Be careful also, the web showed by the wrapper should not contain any text control, or then you will see a keyboard that you can't use...

One more time, the code should be in a .mm file in your XCode project, please see my other codesnippet posts:

Code (glbasic) Select

#if defined (TARGET_OS_IPHONE)

// UIWEBALERT

//#import <UIKit/UIAlert.h>    // It is not needed to duplicate an import done before
#import <QuartzCore/QuartzCore.h>    // Needed libraries for the rounded corners

@interface GLBasicWebBoxer: NSObject <UIAlertViewDelegate>
{
    // Pointer declarations
    NSString* resValues;
    NSString* pTitle,* pURL,* pButtons;
}
@end

@implementation GLBasicWebBoxer

// Code that shows the alert from the main thread
- (void)showWebAlertCaller:(NSString*)cTitle andURL:(NSString*)cURL andButtons:(NSString*)cButtons
{
    // Obtaining the values from "showWebAlert"
    pTitle = [[NSString alloc] initWithString:cTitle];
    pURL = [[NSString alloc] initWithString:cURL];
    pButtons = [[NSString alloc] initWithString:cButtons];
    // "showWebAlert" call sending it to the main thread
    [self performSelectorOnMainThread:@selector(showWebAlert) withObject:nil waitUntilDone:NO];
}

- (void)showWebAlert
{
// View creation for the alert
    UIAlertView* alert = [[UIAlertView alloc] init];
alert.title = pTitle;
alert.message = @"\n\n\n\n\n\n\n\n\n\n\n\n";    // We need some space for the web in the alert XD
alert.delegate = self;
    // We need that the button with an index of 0 does not cancel the alert
    alert.cancelButtonIndex = -1;
// Buttons array creation from the values separated with "|"
NSArray* aButtons = [pButtons componentsSeparatedByString:@"|"];
    // Button creation from each value
for (NSString* iObject in aButtons){
[alert addButtonWithTitle:iObject];
}
   
// We need to create the control and adjust it to the Alert view
UIWebView* webview = [[UIWebView alloc] initWithFrame:CGRectMake(10, 40, 264, 254)];
    webview.scalesPageToFit = YES;   // You can change it if you don't need to shrink the page...
    // Rounded corners thanks to our friend CALayer
    [[webview layer] setCornerRadius:10];
    [webview setClipsToBounds:YES];
    // We can create a little black border to be freaks  XDDDD
    [[webview layer] setBorderColor:
     [[UIColor colorWithRed:0.00 green:0.00 blue:0.00 alpha:1] CGColor]];
    [[webview layer] setBorderWidth:1.00];
   
    // Addition of the control to the view
    [alert addSubview:webview];
    [webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pURL]]];
    [webview release];
    webview = nil;



[alert show];
[alert release];
}

// We need to recover the values using "delegate" (= self) in the "showAlert"
- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
resValues = [[NSString alloc] initWithFormat:@"%d", buttonIndex];
}

// Code that obtains the value of the button pressed
- (void)getValues
{
    // We need the "NSData" to avoid possible errors related to "encodings" (all this code may not be needed... I have not tested it yet)
    NSData* tmpValuesEncoded = [[NSData alloc] init];
    // Conversión to a Windows Latin "encoding" avoiding not recognized chars using "allowLossyConversion"
    tmpValuesEncoded = [resValues dataUsingEncoding:NSWindowsCP1252StringEncoding allowLossyConversion:YES];
    // We now can use the string from "getValuesCaller"
    resValues = [[NSString alloc] initWithData:tmpValuesEncoded encoding:NSWindowsCP1252StringEncoding];
    // Liberar la memoria
    tmpValuesEncoded = nil;
}

// Obtaining the values from the pressed button
- (const char*)getValuesCaller
{
    // Was a button pressed?
    if (resValues != nil){
        // Calling to "getValues" in the main thread
        [self performSelectorOnMainThread:@selector(getValues) withObject:nil waitUntilDone:YES];
        // You can return the string from "getValues"
        return [resValues cStringUsingEncoding:NSWindowsCP1252StringEncoding];
        // Freeing memory
        [resValues release];
        resValues = nil;
        [pTitle release];
        pTitle = nil;
        [pURL release];
        pURL = nil;
        [pButtons release];
        pButtons = nil;
        [self release];
        [super dealloc];
    }
    else
    {
        // If a button was not pressed then...
        return "";
    }
}

@end

// Pointer for the wrapper
GLBasicWebBoxer* newWebAlert;



// External function creation
extern "C" void iOSWebBox(const char* cTitle, const char* cURL, const char* cButtons)
{
newWebAlert = [[GLBasicWebBoxer alloc] init];
    // Convert from "C" to "NSString" and showing the alert...
[newWebAlert showWebAlertCaller:[NSString stringWithCString:cTitle encoding:NSASCIIStringEncoding] andURL:[NSString stringWithCString:cURL encoding:NSASCIIStringEncoding] andButtons:[NSString stringWithCString:cButtons encoding:NSASCIIStringEncoding]];
}

// Function to use from GLBasic (symbol separated values are returned)
extern "C" const char* GetiOSWebBoxValues()
{
    // Now you can recover the button pressed value
return [newWebAlert getValuesCaller];
}

// END UIWEBALERT

#endif



Now from GLBasic:

Code (glbasic) Select


GLOBAL BOTONPULSADOWEBBOXER$

IMPORT "C" void iOSWebBox(const char*, const char*, const char*)

IMPORT "C" const char* GetiOSWebBoxValues()

GOSUB WebBox_iOS

PRINT BOTONPULSADOWEBBOXER$, 50, 100, TRUE

// Here we can use an IF to do something if certain button was pressed, and use (for example:) NETWEBEND to go to the real navigator...

SHOWSCREEN

SUB WebBox_iOS:

iOSWebBox("Title","http://www.glbasic.com","OK|Cancel")

LOCAL InstanteComienzo% = GETTIMERALL()

BOTONPULSADOWEBBOXER$ = ""
WHILE BOTONPULSADOWEBBOXER$ = ""
SLEEP 500
WHILE ABS(GETTIMERALL() - InstanteComienzo%) < 500
HIBERNATE
WEND

BOTONPULSADOWEBBOXER$ = GetiOSWebBoxValues()
WEND

ENDSUB




I hope that this code could be helpful  ;D

Please tell us the results and comment if all goes fine (or not)  :P


Regards.



"Si quieres resultados distintos... no hagas siempre lo mismo" - Albert Einstein.

spicypixel

#1
I am getting this error :( Not sure why?

Code (glbasic) Select
"filename.gbas"(25) error : command not inside function or sub

on line 25 is --> GOSUB WebBox_iOS
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

DaCarSoft

#2
I'm not in front of my laptop just now... I will look if there is a problem when I arrive to home, but until then, please test this steps:

You should create for example a file called GLBasic.mm with a plain text editor, you have to copy and paste all the code from the first block of the post.

Open GLBasic, and create a new project.

Write only a couple of lines of code, like a line to print "Hello world" and other line to do a Showscreen.

Go to the build option of GLBasic and select build for multiplatform... and select iOS.

Open the project generated by GLBasic from XCode, and drag and drop the .mm file into the project of XCode (I don't remember now the tree... but you should see your file at the end of the XCode's tree).

Plug your device to your mac and press the "play" in XCode.

Your Hello world message should appear on screen.

Now press stop in XCode, go to GLBasic again and erase the two lines in your GLBasic project.

Copy and paste the code from the second block of my post.

Go to build for multiplatform and select iOS.

See if now the error does not appear.

If it is error free, go to XCode and press the "Play"



Good luck.

"Si quieres resultados distintos... no hagas siempre lo mismo" - Albert Einstein.

Slydog

Perhaps the following portion should be in another file?
Code (glbasic) Select
GOSUB WebBox_iOS

PRINT BOTONPULSADOWEBBOXER$, 50, 100, TRUE

// Here we can use an IF to do something if certain button was pressed, and use (for example:) NETWEBEND to go to the real navigator...

SHOWSCREEN


You can't have any commands AFTER a subroutine I'm assuming (never used them so I don't know).
Or move those above commands to the start of the file, before the subroutine.
Just a hunch.

Or perhaps make it into a generic function, such as:
Code (glbasic) Select
FUNCTION InputBox$: title$, message$, buttons$
    iOSWebBox(title$, message$, buttons$)
    LOCAL result$ = ""
    LOCAL time% = GETTIMERALL()

    WHILE result$ = ""
        SLEEP 500
        WHILE ABS(GETTIMERALL() - time) < 500
            HIBERNATE
        WEND
        result$ = GetiOSWebBoxValues()
    WEND

    RETURN result$
ENDFUNCTION

Usage:
LOCAL inputResult$
inputResult$ = InputBox("Title", "http://www.glbasic.com", "OK|Cancel")
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

spicypixel

You are correct Slydog about the subroutine being after the main program but it still won't compile because it's expecting there to be functions. This  however does compile correctly and will compile on win32 and presumably will work correctly on iOS.

Code (glbasic) Select

GLOBAL BOTONPULSADOWEBBOXER$

?IFDEF IPHONE
IMPORT "C" void iOSWebBox(const char*, const char*, const char*)
IMPORT "C" const char* GetiOSWebBoxValues()
?ENDIF


GOSUB WebBox_iOS

PRINT BOTONPULSADOWEBBOXER$, 50, 100, TRUE

// Here we can use an IF to do something if certain button was pressed, and use (for example:) NETWEBEND to go to the real navigator...

SHOWSCREEN
MOUSEWAIT
END


SUB WebBox_iOS:
iOSWebBox("Title","http://www.glbasic.com","OK|Cancel")
LOCAL InstanteComienzo% = GETTIMERALL()
BOTONPULSADOWEBBOXER$ = ""
WHILE BOTONPULSADOWEBBOXER$ = ""
SLEEP 500
WHILE ABS(GETTIMERALL() - InstanteComienzo%) < 500
HIBERNATE
WEND
BOTONPULSADOWEBBOXER$ = GetiOSWebBoxValues()
WEND
ENDSUB


?IFDEF WIN32
FUNCTION iOSWebBox: t$, u$, v$
ENDFUNCTION
FUNCTION GetiOSWebBoxValues:
ENDFUNCTION
?ENDIF
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

DaCarSoft

#5
Yes!

Slydog: You are in true, I tested the original code from one of my projects, with several files, and I failed in the GLBasic copy and paste.

And yes, Spicypixel: The ?IFDEF will correct the errors in Win32, but remember to adjust the code for the SUB and/or GOSUB part if you plan to make your project for multiplatform. The steps of my previous post are only for iOS compilation, to help GLBasic to recognize the code added to the XCode project.
As you know this code was made too fast because I wanted to reply a question in the Spanish forum with it :P and I tried also to reply you fast also :P   I have so so so much work lately  :'( :whip:

I will correct the first post.

Someone have time to paste a photo?   :)

Cheers.
"Si quieres resultados distintos... no hagas siempre lo mismo" - Albert Einstein.

spicypixel

It's 1am here in the UK so I'm hoping to give this a test tomorrow :-) I am only compiling for iOS anyway otherwise I would ignore the SUB if it was for anything else the WIN32 functions are just so it compiles without error. Thanks for a great post and I hope to show what I've done with it tomorrow ;)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

spicypixel

I'm stil working on my game but plan to use this in the game to allow facebook posting from the app with a return to GLB control without invoking a browser :)
http://www.spicypixel.net | http://www.facebook.com/SpicyPixel.NET

Comps Owned - ZX.81, ZX.48K, ZX.128K+2, Vic20, C64, Atari-ST, A500.600.1200, PC, Apple Mini-Mac.

DaCarSoft

Good!

Tell us how it goes...


:booze:
"Si quieres resultados distintos... no hagas siempre lo mismo" - Albert Einstein.

Neurox


Hi,
I've a strange error,
I've this file html
Code (glbasic) Select

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Elenco righe</title>
</head>
<body>
<br> Stampa una riga <br\>
<br> Seconda riga <br\>
<br> terza riga <br\>
<br> quarta riga <br\>
<br> ---------------- <br\>
<br> Totale righe <br\>
<br> <br\>
<br> <br\>
<br> <br\>
<a href="javascript:window.print()">Print this page</a>
</body>
</html>


If I've opened with iPhone browser the link function and appear the printer setup but
if I open the page with your WebView function the link not function and not appear anything.

Neurox
Paolo Borzini | paolo@borzini.it
The WhiteFly Software | www.thewhitefly.it
Service on line for screen printers | www.4pellicole.it

DaCarSoft

#10
Hi...

Have you tested the code with a simple known page, like http://www.glbasic.com for example? :)


Are you trying to open the page from local storage???


What is the calling to the code that are you trying?


At the other side, I have not tested the code for printing, may be that the problem is related to the Javascript call. Try also removing that call, to see if printing is not supported under this code. Anyway, I will try to investigate it.


Tell us, please :)
Regards.

"Si quieres resultados distintos... no hagas siempre lo mismo" - Albert Einstein.