Get size of m3u8 video by URL iOS - ios

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);

Related

Overpass API - URL fetching, not working on iPhone, working on mac

I've got this problem last time i did some coding in my project, and i just can't seem to find where the error should be located. When i try the URL in the browser on my mac, everything is working fine - and i get the json file displayed.
My code is as following:
NSURL *url = [NSURL URLWithString:#"http://www.overpass-api.de/api/interpreter?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:25];
[request setHTTPMethod: #"POST"];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"%#", response1);
NSLog(#"%#", requestError);
NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(#"myString: %#", myString);
The error that i'm getting is the following:
Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo=0x1706753c0 {NSLocalizedDescription=unsupported URL,
NSUnderlyingError=0x17044f480 "unsupported URL"}
Bests, Jakob
Adjusted your code and this now seems to work. Basically split end of URL into the body of the POST.
// Split URL from Body
NSURL *url = [NSURL URLWithString:#"http://www.overpass-api.de/api/interpreter"];
NSString *body=#"?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:25];
// Add body to the request
[request setHTTPMethod: #"POST"];
[request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSError *requestError;
NSURLResponse *urlResponse = nil;
NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(#"%#", response1);
NSLog(#"%#", requestError);
NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(#"myString: %#", myString);

Can I get URL through NSURLResponse?

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

How to use EGOCache to cache and load page faster

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:.

Facebook like a post

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);
}

how to connect the Internet?

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);
}

Resources