0

So I have a website that is built in php (IP Board), I have a download manager in my ios app that can detect file extensions like zip, deb etc. However I can't find a way to have it recognize the url that board uses to attach files.

My code I use now:

//Download manager
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

    if(navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *theRessourcesURL = [request URL];
        NSString *fileExtension = [theRessourcesURL pathExtension];

        NSLog(@"fileExtension is: %@", fileExtension);
        if ([fileExtension hasSuffix:@"zip"] || [fileExtension hasSuffix:@"deb"] || [fileExtension hasSuffix:@"rar"] || [fileExtension hasSuffix:@"mp3"] || [fileExtension hasSuffix:@"pdf"] || [fileExtension hasSuffix:@"exe"] || [fileExtension hasSuffix:@"mp4"] || [fileExtension hasSuffix:@"flv"] || [fileExtension hasSuffix:@"torrent"] || [fileExtension hasSuffix:@"png"] || [fileExtension hasSuffix:@"jpg"] || [fileExtension hasSuffix:@"jpeg"]) {

            NSError *error = nil; //error setting
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
            NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"downloads"];

            if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
                [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder

            HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
            [dlvc downloadURL:theRessourcesURL userInfo:nil];


            [self.navigationController setNavigationBarHidden:NO];
            self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:1];
            [self.navigationController pushViewController:dlvc animated:YES];

            dlvc.delegate = self;
            return NO;
        }
        else {
        }
    }
    return YES;
}

Currently the site uploads attachments which gives a link like this:

http://mysite.com/index.php?app=core&module=attach&section=attach&attach_id=38354

Whats the best way to achieve this?

Thank you for your help.

UPDATE 7/17:

From what I have tried to gather, I'm trying to use something like what follows below. But I can't seem to link it up to the download manager to start the download view controller.

//New method I'm trying
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

    NSString *userId= [prefs stringForKey:@"userIdkey"];

    NSString *urlStr = [NSString stringWithFormat:@"http://mysite.com/index.php?app=core&module=attach&section=attach&attach_id=%@",userId];

//Need to push this view controller like the file extension code above
    HCDownloadViewController *dlvc = [[HCDownloadViewController alloc] init];
    [dlvc downloadURL:theRessourcesURL userInfo:nil];

UPDATE 7/18:

Trying something else here, still not pushing the view controller though.

NSURL *url = [request URL];
NSString *id = @"12212323";
NSString *fullURL = [[url pathExtension] initWithFormat:@"www.mysite.com/index.php?app=core&module=attach&section=attach&attach_id=%@", id];

Throws this log:

* WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: * initialization method -initWithFormat:locale:arguments: cannot be sent to an abstract object of class NSPathStore2: Create a concrete instance!

1 Answer 1

0

Solved this by adjusting some things on the server side and then found the extension by using this.

NSString *fileExtension = [[externalURL absoluteString] pathExtension];

Don't know why this was marked as edited, when the code was exactly the same..

  //My code
    NSString *fileExtension = [[externalURL absoluteString] pathExtension];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.