更改uiwebview中的链接 [英] Changing link in uiwebview
问题描述
我必须编写非常简单的应用程序,即在UiWebView中打开链接,但我有一点问题,因为我想从外部数据中获取UiWebView下载链接,例如http://www.example.com/link.txt并检查每次运行应用程序的链接。谢谢你的帮助!
我的尝试:
我尝试过下载数据到NSString但我没有任何想法如何使用UiWebView。
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.
推荐答案
嗨..让你的网址/地址是 http://www.example.com/ ,而文件名是 link.txt 。当您的应用程序加载时,您可以更改任何字符串中的文件名,然后附加字符串..然后将网址加载到您的网页视图。
Code Bellow,>
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,
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];
地址应该是这样的: - http://www.example.com/link.txt [ ]
好的,
然后如果你想下载按钮上的文件单击执行此操作,下载并将文件下载到您的设备
在按钮操作上写下此方法,
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,
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.
这篇关于更改uiwebview中的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!