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);
}
Related
I tried to fetch the file size of m3u8 video before downloading by its url. but its not giving me the file size instead its giving me fixed number which is not file size.
NSURL *URL = [NSURL URLWithString:#"myURl"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:#"HEAD"];
NSHTTPURLResponse *response;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error: nil];
long long size = [response expectedContentLength];
NSLog(#"file size %lld",size);
I have one url and using the viglink api i created other one, now using viglink api I want to get the URL, if i am getting the response.
NSString *vigLinkApiKey = #"somekey";
NSString *url = self.textfieldProductURL.stringValue;
NSString *affiliatedUrl = [NSString stringWithFormat:#"http://api.viglink.com/api/click?key=%#&out=%#&loc=http://amazon.com[&cuid=%#][&reaf=0][&ref=http://amazon.com]", vigLinkApiKey, url, [PFUser currentUser].username];
NSLog(#"Product URL: %#", url);
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:affiliatedUrl] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
NSData *urlData;
NSURLResponse *response;
NSError *error;
// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
NSString *str = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"str:%#",str);
Now, at last i want to get the URL when i am getting response. Is it possible. How can can i get the URL?
Please send the answer.
Thanks.
NSURLResponse has a method - URL use it to get URL.
NSURL *url = [response URL];
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLResponse_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLResponse/URL
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 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.
i am trying to add like button on my application in ios . when i hit url in graph api explorer it works fine, and give me response true. but when i hit this in my code nothing happened
(void)likeFacebookPost:(NSString*)likeID
{
NSString *theWholeUrl = [NSURL URLWithString:[NSString stringWithFormat:
#"%#/%#/likes&access_token=%#",
GRAPH_API_BASEURL,likeID,
[_facebookToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURL *facebookUrl = [NSURL URLWithString:theWholeUrl];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:facebookUrl];
[req setHTTPMethod:#"POST"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];
NSLog(#"responseData: %#", content);
}