I am loading a uiwebview with the following code:
NSString *fullURL = #"http://www.example.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webview loadRequest:requestObj];
Is there any means to cache the website so its available offline if there is no connection?
The key is: NSURLRequestReturnCacheDataElseLoad
NSData *urlData;
NSString *baseURLString = #"mysite.com";
NSString *urlString = [baseURLString stringByAppendingPathComponent:#"myfile"];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:nil];
if (connection)
{
urlData = [NSURLConnection sendSynchronousRequest: request];
NSString *htmlString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
[webView loadHTMLString:htmlString baseURL:baseURLString];
[htmlString release];
}
[connection release];
Hope this will help.
Related
NSURL *url = [NSURL URLWithString: address];
NSString *body = [NSString stringWithFormat: #"%#", webViewString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[_paymentWebView loadRequest: request];
I load the UIWebView but I got this error in my application when I try to access the PayUMoney payment gateway via UIWebView in above code but it show this error and I am using AFNetworking in other API calls.
Kindly help me to resolve this error.
objc[4933]: Class PLBuildVersion is implemented in both /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/AssetsLibraryServices.framework/AssetsLibraryServices (0x12487e998) and /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/PrivateFrameworks/PhotoLibraryServices.framework/PhotoLibraryServices (0x1246a3d38). One of the two will be used. Which one is undefined.**
Just use
NSURL *url = [NSURL URLWithString: address];
NSString *body = [NSString stringWithFormat: #"%#", webViewString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_paymentWebView loadRequest: request];
You don't need to use setHTTPBody or setHTTPMethod.
The code you have is not how we should load url in webview.
The sweet and simple way is as below to load URL in UIWebView.
NSURL *websiteUrl = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
[myWebView loadRequest:urlRequest];
I'm trying to get data and show in tableView. As I have to send Request in viewDidLoad. But it took much time while loading my data, so I need it to be done in async way.
Any Suggestions or Recommendations.?
- (void)viewDidLoad {
//------castApi----//
[super viewDidLoad];
[[self tableView2]setDelegate:self ];
[[self tableView2]setDataSource:self];
array=[[NSMutableArray alloc]init];
NSString *castString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/movie/%#/credits?api_key=c4bd81709e87b12e609433c49",movieIDinString];
NSURL *url=[NSURL URLWithString:castString];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:request delegate:self];
if (connection){
webData= [[NSMutableData alloc]init];
}
}
Try this
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"https://api.themoviedb.org/3/movie/%#/credits?api_key=c4bd81709e87b12e609433c49",movieIDinString]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
}];
I am developing HTML web page. I am creating session id in that.
I want to use the same session id for opening that web site through my iOS app as well.
NSString *urlString = #"http://www.myserver.com:8080/Test/TestPage";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webview loadRequest:urlRequest];
Is it possible to launch a web url using Session ID appended in iOS?
If I am not understanding it in the wrong way, you just want to add some parameters to your request, in this case sessionID. In this case you should do this:
NSURL *url = [NSURL URLWithString: #"http://www.myserver.com:8080/Test/TestPage"];
NSString *body = [NSString stringWithFormat: #"sessionID=%#", #"val1"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [body dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
On the other way around, you can use for example AFNetworking Library that will allow you to do things like this:
NSDictionary * parameters = #{#"sessionID" : #"123"};
NSMutableURLRequest * mutableRequest = [self requestWithMethod:#"POST"
path:#"http://www.myserver.com:8080/Test/TestPage"
parameters:parameters];
Anyways, #Popeye is right, you would need to manage this somehow in your server.
I have an UIWebView in my iOS app that loads different url's depending on a previous action. I wan't these pages to load as fast as possible. I found the class EGOCache (source) and I i got it working to store cacheData in library/Caches directory. But I'm not sure how to retrieve this cache to load it faster, I can't see the difference. Maybe use NSCache? What have I missed?
- (void)webViewDidStartLoad:(UIWebView *)webView {
if (webView_1) {
NSString *urlAddress = #"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[webView1 loadRequest:request];
NSData *data0 = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
[[EGOCache globalCache] setData:data0 forKey:#"webCache"];
}
}
Thanks!
Okay so I made my own method and I got it working. All you have to do is to give the url address. See this code:
-(void)loadPage:(NSString *)urlAddress {
NSURL *url = [NSURL URLWithString:urlAddress];
NSString* cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* file = [cachePath stringByAppendingPathComponent:#"/xxx.APPNAME/EGOCache/EGOCache.plist"];
NSDictionary *dict =[NSDictionary dictionaryWithContentsOfFile:file];
if ([dict objectForKey:urlAddress] )
{
NSData *data = [[EGOCache globalCache] dataForKey:urlAddress];
data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
NSLog(#"loading from cache %#",urlAddress);
}else{
NSData *data = [NSURLConnection sendSynchronousRequest:
[NSURLRequest requestWithURL:url]
returningResponse:nil
error:nil];
[[EGOCache globalCache] setData:data forKey:urlAddress];
NSLog(#"saving cache %#",urlAddress);
[[EGOCache globalCache] setDefaultTimeoutInterval:60*60*24*4]; //timeout (4 days)
}
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval: 10.0];
[webView loadRequest:request];
}
To accomplish what you are trying to implement you would need to subclass NSURLCache and implement the required methods, with your implementation accessing EGOCache. UIWebView and NSURLConnection use NSURLCache for caching data. Once you have your custom subclass implemented, you would set an instance of it to be your application's URL cache using setSharedCache:.
I want to use some api provided by douban.com so I'm wondering how to make my ipad simulator connect the Internet.
Anyone would do this favor to me?
-(IBAction)testParsingXml:(id)sender
{
NSURL *url = [NSURL URLWithString:#"GET http://api.douban.com/book/subjects? tag=cowboy&start-index=1&max-results=2apikey={0a80c344758f3bfa1f8249cd60adb3ee}"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(#"%#",data);
}