ios crash on NSMutableURLRequest - ios

NSURL *url = [NSURL URLWithString:#"http://locationofurlonlocalhost.mp4"];
...
NSMutableURLRequest *req;
req = [NSMutableURLRequest requestWithURL:url // <-- CRASH IS HERE!! thx to a breakpoint stepthrough
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
a log of url will result in:
Printing description of url:
<NSURLRequest http://locationofurlonlocalhost.mp4>
the crash will log
-[NSURLRequest absoluteURL]: unrecognized selector sent to instance 0x88003e0
The URL looks just fine so I'm not sure what's going on. Could this happen because the url is unreachable the simulator? I can connect to the url on the sim's safari as well as my desktop safari. What should I do?
** EDIT **
it's been determined that the url object is being released too early. In the ... the url is passed to a different selector and then the rest of the code takes place there. How can I force retain the url? I have already tried:
__strong NSURL *url = [[NSURL alloc] initWithString:#"http://locationofurlonlocalhost.mp4"];

This most definitely looks like your url has been released somehow. If you print the description of the url variable and wind up with an NSURLRequest, then you're looking at freed up memory.
Try using alloc / initWithString: as a troubleshooting step.

Related

How to use different User-Agent in different iOS Webview request

I know I can use the below code to change the default agent:
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:#"Your user agent", #"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
but I want to request a different User-Agent for different requests. Examples: request 'www.google.com', use agent=‘google Agent’, request 'www.github.com', use agent='github Agent'.
I have tried the below way to set the 'User_Agent' in each request, but it doesn't seem to work.
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.amazon.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSMutableURLRequest *requestObj = [NSMutableURLRequest requestWithURL:url];
[requestObj setValue:#"Foobar/1.0" forHTTPHeaderField:#"User_Agent"];
// But the value is not "Foobar/1.0"
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:#"navigator.userAgent"];
NSLog(#"agent - %#", secretAgent);
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
One more question:
It seems a change to the default agent only work change in 'AppDelegate' or 'initialize'. Am I right? Because I try to change in 'viewDidLoad' but it doesn't seem to work.
I observed that if you load any url in webView with a user-agent set
to it. After some time you load the different url & different user
agent but used the same allocated WebView instane.The web view will
load the new url but can not set new user-agent. It just set old one
that you have set before loading first time.
You can not change untill you initiate new instanse of UIWebview(or
we can say that user agent can be set once per session). So I coame to result that user-agent can be set only once.
What I did for this problem is, I allocated new instance when I want set new user-agent.
You don't need to set it using JavaScript.
You have misspelled User-Agent. Use this minimal code taken from here
NSString* userAgent = #"My Cool User Agent";
NSURL* url = [NSURL URLWithString:#"http://whatsmyuseragent.com/"];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue:userAgent forHTTPHeaderField:#"User-Agent"];

Internet data doesn't update correctly

I've got this weird issue with my apps that uses internet connection. The app doesn't seem to update the data from internet in the right way. I've got a UIWebView with a simple chat. When I write a message in the chat with safari or in my web browser everything works fine. When writing something in the chat in the app, the data doesn't update, even when reloading the page.
It updates totally un-logically as well. Even when restarting the app the new data isn't shown, then all of a sudden it shows.
Had the same issue with another app too, which was not handled by a UIWebView. Any idea what it might be?
Example code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *username = [[Database singletonDataBase] currentUserName];
NSString *urlAddress = [NSString stringWithFormat:#"%#?name=%#", #"http://webapi.com/index.php", username];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
In your app delegates, didFinishLoading method, set the NSURLCache to not cache the data:
// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
If that doesn't work, you can also try setting the cache policy on the NSURLRequest:
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval: 10.0];
I finally found the answer. Its nothing wrong with my code, as I thought. The problem lies on the server side. And, as they say in this thread:
NSURLConnection is returning old data
It seems to be badly configured server-side or proxy. Which makes the cache act all wierd. I tried another server and everything worked just fine!
Hope this helps someone with the same issue, cause it sure wasted me ALOT of time.

UIWebView not displaying URL

Pretty easy enough problem, but It's driving me crazy. I connected my uiwebview to the .h and called it myWebView. then in the viewdidLoad, i did this:
NSString *urlString = #"www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
But when I run the app I just see a blank screen...
I don't know what's going on.
All help is appreciated, thanks
Your urlstring isn't well-formed (for your intent)
Use http://www.google.com/
Technically www.google.com is a valid URL (according to things like RFC 1738 and 3986), but since it lacks the scheme (the http part), the UIWebView doesn't quite know what to do with it.

IOS - How do I add segments to NSURL?

Suppose you have a URL that looks like http://localhost:5867 and your wanted to append '/something' to the url. How do I append segments to this URL?
I am trying this:
//_baseURL is NSURL* and documentid is NSString*
NSURL* url = [_baseURL URLByAppendingPathComponent:documentid];
Oddly.. xcode(4.6.2) will not output the resulting URL to console.
NSLog(#"%#", [url absoluteString]);//nothing output
Thanks!
Edit
I am working in a workspace. The code that I am debugging is a static lib which is in the workspace. Could this be the culprit of all this weirdness??
Fixed
I think the reason I was having problems was because my workspace was not configured correctly and I was actually executing an older version of my static lib. This explains why I wasn't getting NSLog'ing and why the result were unexpected. I found this post which helped me understand the workspace and how to configure it for a static library. Thanks a lot for everyone's time!
There is a class message,
+ (id)URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL
So,
NSURL *base = [NSURL URLWithString: #"http://localhost:5867"];
NSURL *child = [NSURL URLWithString: #"something" relativeToURL:base];
NSURL *base = [NSURL URLWithString:#"http://localhost:5867"];
NSURL *url = [base URLByAppendingPathComponent:#"something"];
NSLog(#"%#", [url absoluteString]);
This works for me and prints http://localhost:5867/something, so check your variables (maybe one of them is nil or an empty string?).

UIWebView staying blank until I hard code the URL

I am writing an App where I pass a Link as a NSString from a TableView to my DetailView. The String gets passed correctly, as I can see it in the log. The problem is, that the WebView just stays blank; unless I hard code any URL in. Even the ones that I can see in the log by just copy pasting them. What am I missing? Any help is greatly appreciated. Here's a sample log output and my code. I also checked this thread, where the guy hab the exact same problem as me, but none of the solutions posted there where helpful. UIWebView not loading URL when URL is passed from UITableView
2013-06-17 13:19:52.147 Feuerwehr[1661:c07] http://oliverengelhardt.de/ffw_app/Einsaetze/GB0505/index.html
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlString = url;
NSURL *myURL = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:myURL];
[webView loadRequest:requestObj];
NSLog(urlString);
}
I think you stringUrl is nil when it loads (so it doesn't get passed. Break point view did load and see the value of the urlstring
Based on what you've said, your url isn't formatted correctly
i think you are missing delegate
self.webView.delegate = self;

Resources