I have tried for a week to get writeToUrl to work, but no... nothing.
I simply want to update a file on my server from an iPhone app.
This is the code:
NSURL *url = [NSURL URLWithString:#"http://user:pw#192.168.120.167/test.txt"];
NSString *teststring = #"it works";
if ([teststring writeToURL:url atomically:YES encoding:NSASCIIStringEncoding error:nil]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Status" message: #"Yessss" delegate: self cancelButtonTitle: #"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Status" message: #"Noooooo" delegate: self cancelButtonTitle: #"Close" otherButtonTitles: nil];
[alert show];
[alert release];
}
I switched from writeToURL to writeToFile, using the exact same file path, and it suddenly worked. Seems like there might be a bug in writeToURL. This was tested on iOS 7.1.
You can only write to local files. See documentation:
Since at present only file:// URLs are supported, there is no difference between this method and writeToFile:options:error:, except for the type of the first argument.
Related
I am new to development with Theos, so i decided to try a simple tweak from a tutorial. The tweak compiles and installs correctly, but it does not work when I test it on my iPad mini with iOS 8.4, using the iOS 8.1 SDK. This is a copy of the code from the tutorial I used:
%hook SBApplicationIcon
-(void)launch
{
NSString *appName = [self displayName];
NSString *message = [NSString stringWithFormat:#"The app %# has been launched", appName, nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:appName message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
%orig;
}
%end
I tried to do localization. It's Working fine, but here, I am facing a problem in UIAlertView. It's not working for more then one string, but I need to keep more then one string AlertView OtherButton Titles.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertTitle", #"")
message:NSLocalizedString(#"AlertMessage", #"")
delegate:nil
cancelButtonTitle:NSLocalizedString(#"AlertCancel", #"")
otherButtonTitles:NSLocalizedString(#"AlertOk", #""), nil];
Try following piece of code.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"AlertTitle", nil) message:NSLocalizedString(#"AlertMessage", nil) delegate:nil cancelButtonTitle:NSLocalizedString(#"Cancel", nil) otherButtonTitles:NSLocalizedString(#"Change Password", nil), NSLocalizedString(#"Profile", nil), NSLocalizedString(#"log out", nil), nil];
[alertView show];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertTitle",nil)] message:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertMessage",nil)] delegate:nil cancelButtonTitle:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertCancel",nil)] otherButtonTitles:[NSString stringWithFormat:#"%#",NSLocalizedString(#"AlertOk",nil)], nil];
To simplify all NSLocalizedString calls, I use this technic
#define Trad(string, ...) [NSString stringWithFormat: NSLocalizedString(string, nil), ##__VA_ARGS__]
Then you can create your AlertView like this:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:Trad(#"Title")
message:Trad(#"Message")
delegate:nil
cancelButtonTitle:Trad(#"Cancel")
otherButtonTitles:Trad(#"Button1"),
Trad(#"Button2"),
Trad(#"Button3"), nil];
The #define statement isn't necessary, it's only purpose is to simplify the code.
I would like to display something like this: SampleUser poked you. in a UIAlertView's message, but actually i'm getting errors. I know how to do it with a simple string, i don't know how to do it with a string that contains another string.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:#"%# poked you.", self.senderString delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];
You should create your composed-NSString first and then call it in your UIAlertView:
NSString *message = [NSString stringWithFormat:#"%# poked you.", userName];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:message, self.senderString delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];
The problem here is that for the message: argument, you're attempting to send this:
#"%# poked you.", userName
This doesn't make any sense.
Instead, you need to send an NSString object as the argument.
NSString *message = [NSString stringWithFormat:#"%# poked you.", self.senderString];
Now that we've created an NSString object, we can use this object as the message argument.
You could create this object embedded in the call to create the alert view, but for readability and debugging , it's better to do it this way.
NSString *message = [NSString stringWithFormat:#"%# poked you.", self.senderString];
UIAlertView *pokeAlert = [[UIAlertView alloc] initWithTitle:#"Poke"
message:message
delegate:self
cancelButtonTitle:#"Yes"
otherButtonTitles:#"No", nil];
[pokeAlert show];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Poke" message:[NSString stringWithFormat:#"%# poked you.", self.senderString] delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"No", nil];
[alertView show];
I would like to pass some values from json to a message of UIAlertView. I have some codes
- (void)jsonParse{
NSString* path = #"http://phdprototype.tk/getResultData.php";
NSURL* url = [NSURL URLWithString:path];
NSString* jsonString = [[NSString alloc]initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSData* jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableLeaves error:nil];
NSDictionary* resultDic = [dic objectForKey:#"maxid"];
NSString* recData = [resultDic objectForKey:#"recommendData"];
NSString* rData = [resultDic objectForKey:#"room"];
NSString* lData = [resultDic objectForKey:#"level"];
}
- (void)locationView
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#."]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
[alert show];
}
As I know, I have to do something in
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#."]. However, I have no idea how to do it. Can someone tell me how to pass the values of lData and rData to the message of uialertview??
You can make a global NSString variable which defines you message
In .h file
NSString *message;
In jsonParse method
- (void)jsonParse {
//Your Stuff
message = [NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#", lData, rData];
}
and then in your UIAlertView
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Recommendation"
message:message
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"GO", nil];
[alert show];
- (void)jsonParse{
//your code
[self locationViewWithRoom:rData level:lData];
}
- (void)locationViewWithRoom:(NSString *)room level:(NSString *)level
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", room, level]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
[alert show];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", iData, rData]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
Just write the values after you write the argument for stringWithFormat. The number of your arguments should match with the number of %#s used in argument string, and your arguments should be comma separated, otherwise you will get a build error.
Declare both the variables (rData & lData) globally (means, in your interface) and then use it like this
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: #"Recommendation"
message:[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.",lData,rData]
delegate: self
cancelButtonTitle: nil
otherButtonTitles: #"GO", nil];
Its actually pretty easy. You are doing right just few things are missing.
Declare rData & lData as properties in your class,
#interface <classname>
#propert (nonatomic,strong) NSString *lData;
#propert (nonatomic,strong) NSString *rData;
#end
Then your string can be formed using,
[NSString stringWithFormat:#"OK! There is another collection which located at room %# in level %#.", lData, rData];
This should resolve your issue.
I want an alert when the following text fields receive the conditions in second section of code
self.circuit.rcdAtIan = [ICUtils nonNilString:self.rcdAtIan.text];
self.circuit.rcdAt5an = [ICUtils nonNilString:self.rcdAt5an.text];
The above code works fine so now I need to fire it. Using the below method was my first thought but that fires the alert on every keyboard resignation. I only want the alert to display once.
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
}
if ([_rcdAt5an.text intValue]> 40) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
}
}
Im thinking maybe I need a bool with NSUserDefaults perhaps? but not sure how to implement that to check if the alert has been shown. Normally if I wanted an alert shown once I would do
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:#"alert"]]){
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[alert show];
}
However in this instance, when the page is reloaded I want the alerts to be shown again and in any case im not sure if thats an efficient way to solve this
Don't use NSUserDefaults, that would be inappropriate
in your #interface...
#property (assign) BOOL freshAlert1;
#property (assign) BOOL freshAlert2;
then something like this... (assuming 'page is reloaded' equates to your viewController coming back onscreen)
- (void)viewDidAppear
{
self.freshAlert1 = YES;
self.freshAlert2 = YES;
}
if (([_rcdAtIan.text intValue]> 200) && self.freshAlert1) {
self.freshAlert1 = NO;
...
}
if (([_rcdAt5an.text intValue]> 40) && self.freshAlert2) {
self.freshAlert2 = NO;
...
}
What exactly do you mean by "once"? Once per app run, once per editing step?
Or maybe simply add two BOOL flags that you can reset whenever you want and simply to
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200 && !alert1shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
alert1shown = YES;
[alert show];
}
if ([_rcdAt5an.text intValue]> 40 && !alert2shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
alert2shown = YES;
[alert show];
}
}