Click here to Skip to main content
15,040,090 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have to write very simple application whoes open link in UiWebView but I have a little problem because I want that UiWebView download link from outside data e.g. http://www.example.com/link.txt and check link on every run of application. Thanks for help!

What I have tried:

I tried to download data to NSStringbut I don't have any ideas how to use this with UiWebView.
Posted
Updated 30-Jun-16 0:14am

1 solution

Hi .. Let your url / address is http://www.example.com/ , and ur file name is link.txt. when ur application load u can change the file name in any string then append the string .. then load the url to ur web view.

Code Bellow,

C#
NSString *fileName = @"demo.txt";
NSString *address = @"https://www.example.com/";
address = [address stringByAppendingString:fileName];
NSLog(@"%@",address); // check ur url link in the debug terminal, it should be like .
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:request];


address should be like :- http://www.example.com/link.txt[]

Okay ,
then if u want to download the file on button click do this, download and the file to ur device
write this method on button action,

Objective-C
NSData *urlData = [NSData dataWithContentsOfURL:url];
NSString  *filePath;
NSArray  *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString  *documentsDirectory = [paths objectAtIndex:0];
filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.txt"];
NSLog(@"%@",filePath);
[urlData writeToFile:filePath atomically:YES];



If u want you web view to do this automatically, just give some delay after web view loaded and the above method.
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900