Viewed   46 times

In an iOS app, I used

stringFromJavaScript = [webView stringByEvaluatingJavascriptFromString:@"document.getElementById("image").getAttribute("src")"];

To get the src directory of the image that was being displayed on the webView. I want to do the same for Android. What are my options?

Basically the intent is to capture the path so that I can email this same picture...

ie.

"picture.php?image=%@",stringFromJavascript

This way, that same image would be loaded when the user clicks the link, or posts it to facebook etc.

 Answers

5

Yeah, I miss this method greatly in Android ;)

To execute JavaScript and get response you can do as follows:

  1. Define JavaScript callback interface in your code:

    class MyJavaScriptInterface {
        @JavascriptInterface
        public void someCallback(String jsResult) {
             // your code...
        }
    }
    
  2. Attach this callback to your WebView

    MyJavaScriptInterface javaInterface = new MyJavaScriptInterface();
    yourWebView.addJavascriptInterface(javaInterface, "HTMLOUT");
    
  3. Run your JavaScript calling window.HTMLOUT.someCallback from the script:

    yourWebView.loadUrl("javascript:( function () { var resultSrc = document.getElementById("image").getAttribute("src"); window.HTMLOUT.someCallback(resultSrc); } ) ()");
    

Hope this helps!

Wednesday, October 5, 2022
1

I suggest you take a look at http://phpaes.com/. It's a free AES encryption library implemented purely in PHP; it's fast and very very simple to use.

At the very least, it allows you get one step closer to isolating the true source of the issue.

Tuesday, September 20, 2022
3

Try this in Java: This will give you the long version of the string (UTF-8)

byte[] encoded = Base64.encode(encrypted.getBytes("UTF-8"), Base64.DEFAULT);
String str = new String(encoded, "UTF-8");

Updated:

Try this in Java: This will give you the short version of the string (CP1252)

// This should give the same results as in PHP
byte[] encoded = Base64.encode(encrypted.getBytes("CP1252"), Base64.DEFAULT);
String str = new String(encoded, "CP1252");

Alternatively try this PHP Script:

file: test.php

<?php

echo base64_encode($_GET['str'])." Default UTF-8 version<br />";
echo base64_encode(iconv("UTF-8","CP1252",$_GET['str']))." CP1252 Version <br />";

?>

usage: http://[SOMEDOMAIN]/test.php?str=†+Ü]M(‡=ñö
Saturday, August 13, 2022
 
4

I found a perfect solution using UITextView, it will make every word in within the UITextView clickable.

Firstly, Create an UITextView and add an UITapGestureRecognizer to it as follows:

CGRect textViewFrame = CGRectMake(0, 40, 100, 100);
textView = [[UITextView alloc]initWithFrame: textViewFrame];
textView.textAlignment = NSTextAlignmentCenter;
textView.backgroundColor = [UIColor clearColor];
textView.editable = NO;
textView.selectable = NO;

[self.view addSubView:textView];

// i used to `NSMutableAttributedString` highlight the text

 string = [[NSMutableAttributedString alloc]initWithString:@"Any text to detect A B $ & - +"];
    [string addAttribute:NSFontAttributeName
                  value:[UIFont systemFontOfSize:40.0]
                  range:NSMakeRange(0, [string length])];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init] ;
    [paragraphStyle setAlignment:NSTextAlignmentCenter];

    [string addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [string length])];

    [textView setAttributedText:string];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];

//modify this number to recognizer number of tap
[singleTap setNumberOfTapsRequired:1];
[textView addGestureRecognizer:singleTap];

Then add the UITapGestureRecognizer @selector:

- (void)tapRecognized:(UITapGestureRecognizer *)recognizer{

    if(recognizer.state == UIGestureRecognizerStateRecognized)
    {

        CGPoint point = [recognizer locationInView:recognizer.view];
        NSString * detectedText = [self getWordAtPosition:point inTextView: textView];

        if (![detectedText isEqualToString:@""]) {

    NSLog(@"detectedText  == %@", detectedText);


        } }

}

All this magic is related to this method, witch can detect any touch on the UITextView and get the tapped word:

-(NSString*)getWordAtPosition:(CGPoint)pos inTextView:(UITextView*)_tv
{
    //eliminate scroll offset
    pos.y += _tv.contentOffset.y;

    //get location in text from textposition at point
    UITextPosition *tapPos = [_tv closestPositionToPoint:pos];

    //fetch the word at this position (or nil, if not available)
    UITextRange * wr = [_tv.tokenizer rangeEnclosingPosition:tapPos withGranularity:UITextGranularityWord inDirection:UITextLayoutDirectionRight];

    return [_tv textInRange:wr];
}

And for highlighting the text:

-(void)setTextHighlited :(NSString *)txt{

    for (NSString *word in [textView.attributedText componentsSeparatedByString:@" "]) {

        if ([word hasPrefix:txt]) {
            NSRange range=[self.textLabel.text rangeOfString:word];
            [string addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
         }}
    [textView setAttributedText:string];

}

And thats it, hope this helps others.

Saturday, November 26, 2022
 
1

The equivalent of an Android TextView in iOS is UILabel and UITextView. The one you want for large amounts of text is UITextView. Below are descriptions taken from the documentation:

UITextView

The UITextView class implements the behavior for a scrollable, multiline text region. The class supports the display of text using custom style information and also supports text editing. You typically use a text view to display multiple lines of text, such as when displaying the body of a large text document.

UILabel

The UILabel class implements a read-only text view. You can use this class to draw one or multiple lines of static text, such as those you might use to identify other parts of your user interface.

UITextField

A UITextField object is a control that displays editable text and sends an action message to a target object when the user presses the return button. You typically use this class to gather small amounts of text from the user and perform some immediate action, such as a search operation, based on that text.

Here are some links for further research:

  • Using Text Kit to Draw and Manage Text (documentation)
  • Text Kit Tutorial in Swift
  • Using Text Kit to Manage Text in Your iOS Apps
  • ios: best way to display variable-length, multi-line text
Tuesday, August 9, 2022
 
jermah
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :