Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-06-2010, 06:33 PM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default NSURL Help

I am trying to get a customized URL in a webview for the user but it is not working out so far. It crashes even though it returns no errors (the red code is the line that is crashing it). Anyone know a solution to use an NSString as NSURL.

yopmailLink is an NSString.
mailNumber is an int

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yopmailLink]]];
		
	}

}
I also tried:

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSString *yopmailLinkURL = [yopmailLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yopmailLinkURL]]];
		
	}

}

Last edited by Speed; 07-06-2010 at 06:35 PM.
Speed is offline   Reply With Quote
Old 07-06-2010, 07:26 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Speed View Post
I am trying to get a customized URL in a webview for the user but it is not working out so far. It crashes even though it returns no errors (the red code is the line that is crashing it). Anyone know a solution to use an NSString as NSURL.

yopmailLink is an NSString.
mailNumber is an int

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yopmailLink]]];
		
	}

}
I think I know at least one of your problems. I gather that yopmailLink is an instance variable of the class you are working on? (A view controller perhaps)

When you execute the statement

Code:
	yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
you are creating an autoreleased object. This object is temporary, and unless you send it a retain message, it will get released the next time your code returns to the main event loop.

The basic rule is this: System methods that begin with init, new, or copy return objects with a retain count of 1. Those objects are not temporary. You own them, and are responsible for sending them a release message when you are done with them.

For all other methods that return an object, unless the documentation specifically says that the object is returned with a retain count of 1, the object is autoreleased. That means it was sent an autorelease message. THAT means that the object no longer has an owner, and will be release soon.

You should probably change your yopmailLink variable to a retained property of your class, and then assign values to it using property syntax, like this:


Code:
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
When you change the value of a retained property using property syntax (like "object.property = value" or "[object setProperty: value]", the object runs a special bit of code called the "setter", that causes any old value of the property to be released, and the new value to be sent a retain message.


Once you are done with yopmailLink, you should say

Code:
self.yopmailLink =nil;
That will cause the old value of the property to be released.

You should read slick's tutorials on Cocoa memory management and properties.

For those of you trying to learn iOS programming, stop and learn about Cocoa memory management and how to use properties before you do ANYTHING else. Read and study this topic until it is second nature. If you follow a few simple rules, your code will work well. IF you mess up, even once, your program will either leak memory and eventually run out of memory and be terminated by the system, or you will have objects get released when you are still using them, and the next time you reference the object, your app will crash.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 07:56 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

Thanks so much, you always come through for me.

One issue however, this is the working code:

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSLog(@"yopmail");
		
		NSString *yopmailLinkURL = [yopmailLink stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yopmailLinkURL]]];

		
	}

}
It displays the email and goes to the page but for some reason it adds %0d between the name and the number. This is from the NSUTF8StringEncoding line but I cannot get it to work without that. Is there a fix?
Speed is offline   Reply With Quote
Old 07-06-2010, 08:26 PM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Speed View Post
Thanks so much, you always come through for me.

One issue however, this is the working code:

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
        mailToDisplay = [mailToDisplay stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@%d",mailToDisplay,mailNumber];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSLog(@"yopmail");	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:yopmailLinkURL]]];

		
	}

}
It displays the email and goes to the page but for some reason it adds %0d between the name and the number. This is from the NSUTF8StringEncoding line but I cannot get it to work without that. Is there a fix?
Which line of code inserts a "%0d" between the name and number? The randomMailLabel.text = assignment? The self.yopmailLink assignment?

One thing I noticed: You are escaping the entire URL, and that is almost certainly wrong and going to cause you problems. That will escape the "://" in your URL (I think) which is bad. You want to escape your mail body before adding it to the URL. I changed the code above to show you what I mean, and put the changes in bold.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-06-2010, 10:20 PM   #5 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

I believe that the encoding is the thing that puts the number there. It displays normally otherwise. But if I have no encoding, it won't show up in the webview.
Speed is offline   Reply With Quote
Old 07-06-2010, 10:40 PM   #6 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

I also tried this:

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	self.mailFull = [NSString stringWithFormat:@"%@%d",mailToDisplay,mailNumber];
	mailFull = [mailFull stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSLog(@"yopmail");
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull]]]];

		
	}

}
But it crashes as soon as it reaches the loadRequest. The webview does not load if I remove the bolded line but as soon as I add it, the app crashes.
Speed is offline   Reply With Quote
Old 07-07-2010, 09:02 AM   #7 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

That is weird that it doesn't accept it as a normal nsstring. Why does it have to be encoded? Letters are letters :/
Speed is offline   Reply With Quote
Old 07-07-2010, 10:14 AM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

I tried removing the %0d from the string manually.

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	self.mailFull = [NSString stringWithFormat:@"%@%d",mailToDisplay,mailNumber];
	self.mailFull = [mailFull stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSLog(@"yopmail");
		
		mailFull = [mailFull stringByReplacingOccurrencesOfString:@"%0d" withString:@""];
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull]]]];

		
	}

}
It doesn't remove it which leads me to believe that it adds the %0d as it does the URL request.
Speed is offline   Reply With Quote
Old 07-07-2010, 07:30 PM   #9 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 577
Speed is on a distinguished road
Default

Code:
	mailNumber = arc4random() % 100;
	
	int mailArrayCount = [mailArray count];
	int mailObject = arc4random() % mailArrayCount;
	mailToDisplay = [mailArray objectAtIndex:mailObject];
	self.mailFull = [NSString stringWithFormat:@"%@%d",mailToDisplay,mailNumber];
	self.mailFull = [mailFull stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	randomMailLabel.text = [NSString stringWithFormat:@"%@%d@yopmail.com",mailToDisplay,mailNumber];
	
	self.yopmailLink = [NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull];
	
}

-(IBAction)loadYopmail {
	
	if (mailAddressInt == 0) {
		NSLog(@"No Email Link Selected");
		
	}
	
	if (mailAddressInt == 1) {
		
		NSLog(@"yopmail");
		
		mailFull = [mailFull stringByReplacingOccurrencesOfString:@"%0d" withString:@""];
	
		[yopmailWeb loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.yopmail.com?%@",mailFull]]]];

		
	}

}
So the bolded line is 100% the line that adds the %0d. If the original was hi12, it would look like hi%0d12 after going through the encoding. Is there a fix for this so I can actually load the webpage? The webpage does not load without the encoding.

I changed the encoding to only encode the NSString CityToDisplay and I also got rid of the whole mailfull but it still gives @%D

Last edited by Speed; 07-07-2010 at 08:10 PM.
Speed is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 334
9 members and 325 guests
anothermine, Chickenrig, firecall, givensur, iNet, michaelhansen, Objective Zero, PixelInteractive, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,892
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:29 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0