简体   繁体   中英

Parse a javascript window.close() function from a web page in xcode

I am writing an application for my parent's photo booth business.

They upload all the event pictures to different Flickr photosets on their account, and on the website they have an albums page with a collection of all their events so that people from the events can go and see all their photo strips, as well as each individual picture and then download them for themselves.

Most of their traffic is from mobile devices (my brother wrote the website specifically to fit mobile device screens as well as normal computer screens just as well); however, downloading and sharing the images is easier through an app (thanks to Apple's iOS 6 UIActivityViewController and UICollectionView).

So I'm writing the app that makes it easier to view and share the pictures. I have most of it done and working great! However, it currently only supports iOS 6 and I'm trying to include iOS 5. It only supports iOS 6 because I am using a UICollectionView to display the pictures from events. However, since iOS 5 does not include collection views I use a web view of our albums web page which displays all images.

When you select an image, you can choose a size of the image to view. When you pick a size, it opens a new tab, that includes the image and two links. One to download the image, and one to close the tab. The two links are within an unordered list and each link is its own list item.

This is the link to download the image:

<a href="/downloadImage.php?link=IMG_LINK&amp;title=TITLE">Download Image</a>

And the closing tab link has a tag of:

<a href="javascript:window.close();">

I've seen that you can use the webView's delegate method shouldStartLoadWithRequest: and then:

if ([[[request URL] absoluteString] hasPrefix:@"/downloadImage.php"]) {
    UIImageWriteToSavedPhotosAlbum(imageToBeSaved, nil, nil, nil);
    return NO;
} else if ([[[request URL] absoluteString] hasPrefix:@"javascript"]) {
    [webView goBack];
    return NO;
} else { return YES; }

Which would make it do those functions when the links are clicked...right? These do not work though. And I'm not sure how to make them work.
I am searching for the correct way to use this method, or some alternative method, if possible, to use Objective-C to do the equivalent of what the html normally does.

I figured out a way to do basically what I wanted. Rather than let the webView open a new page when you selected an image size. I set the size links to just download the image right there using this code:


// for links thats contain the displayImage.php do not allow user interaction
if ([[[request URL] absoluteString] hasPrefix:@"http://www.remembermephotobooths.com/displayImage.php?link="]) {
        //scan through the link url get the image link so that I can download it
        NSMutableArray *substring = [NSMutableArray new];
        NSScanner *scanner = [NSScanner scannerWithString:[[request URL] absoluteString]];
        [scanner scanUpToString:@"http://farm9" intoString:nil]; // Scan all characters after and including http://farm9 because all download links begin with http://farm9
        while(![scanner isAtEnd]) {
            NSString *string = nil;
            [scanner scanString:@"&title=" intoString:nil]; // Scan up to &title
            if([scanner scanUpToString:@"&" intoString:&string]) {
                [substring addObject:string];
            }
            [scanner scanUpToString:@".jpg" intoString:nil]; // Scan all characters up to and including .jpg because all images are jpegs
        }
        // save image to photo library
        UIImageWriteToSavedPhotosAlbum([UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:substring[0]]]], nil, nil, nil);
        // let the user know the image was saved
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [hud setMode:MBProgressHUDModeText];
        hud.labelText = @"Saved Image!";
        [hud setMinShowTime:1.5];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [substring release];
        return NO; //this is what disables the links usual code and has it run my code instead
    }
    else { //all other links are enabled
        return YES;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM