Ad

Our DNA is written in Swift
Jump

Category Archive for ‘Q&A’ rss

Ignoring Certificate Errors on NSUrlRequest

Fabian asks:

I’m trying to request data from a website via HTTPS that does not have valid certificate. How can I ignore the certificate error?

When the iPhone makes a HTTPS request it verifies that the certificate used to encrypt the data has a valid root certificate authority. Usually – for big sites – this is provided by Thawte or Verisign or any other recognized Root Certification Authority (CA). A bundle of the public certificates of such CAs is installed in the OS and enables the client to know which CAs are valid.

The problem arises however if you don’t have the funds to purchase such a certificate from a CA, those are expensive. Or sometimes you want to create a certificate for your own use or testing. This is called self-signed certificates. Those are also deemed invalid at first glance, unless you tell your browser to accept these certificates. Or it may be the case of Twitter who seem to have an expired certificate on one of their API servers.

Read more

Detect Roaming

hhyyy9 asks:

It’s possible to detect when device goes on roaming or out of home network and turn on / turn off the data connection?

Well, yes and no. Yes to the part of the question about detecting. No to changing a system setting.

You have no direct access at all to roaming or home network information. Probably via a private framework but Apple does not approve of apps using those. Though you could infer the currently used data network from the IP address range you get from the currently active cellular connection. Each provider will have certain IP ranges and if you collect these ranges then you could build up a database to detect such network switching.

Also wheres the point? There already IS a setting that allows you to disable data roaming.

general network

The fact remains that you still cannot mess with the data system setting, but only work within your app.

But this would not be an worthy Dr. Touch post if I didn’t share some cool knowledge, this time how you can get all the current IP addresses of your device. Something like “IPCONFIG /ALL” on Windows or “IFCONFIG” on Unix.

Read more

Alternating Tableview Cell Backgrounds

Wanner asks:

“How can I make my cells alternate colors across multiple sections if the sections don’t always have the same number of rows?”

A nice effect that you sometimes see in table views like the app store app is when you alternate the tableview cell backgrounds between light and dark shades of a color.

Read more

"Are You a Cocoa Crack?" Quiz (1)

These questions will show if you are really the Cocoa Crack you like to believe to be. To see the answer highlight the answer text with your mouse. No peeking! Respond in the comments how many you got correct.

Quiz: Warm Up. Why can you not add an NSInteger to an NSArray? With which extra step can you do it?

Answer: NSInteger is only a different name for a signed integer. It is not a class even though the name might suggest it is. Only instances of classes can be added to NSArrays. The extra step mentioned is to first create an NSNumber object from the integer which you can add to an NSArray.


Quiz: I have an outlet defined and my code compiles without error or warning. But when I try to connect in in Interface Builder it does not show up. Why?

 
@interface CocoaQuizViewController : UIViewController
{
	UILabel IBOutlet *myLabel;
}
 
@property (nonatomic, retain) UILabel IBOutlet *myLabel;
 
@end

Answer: The IBOutlet is at the wrong position. It needs to be before the type UILabel. IBOutlet itself is defined to be replaced with empty text for compiling, that’s why it does not cause a syntax error. But Interface Builder is looking for this keyword in header files to know what outlets there are.


Quiz: What does the following code do? Variable age is defined as NSInteger. Is the syntax correct or is there something missing? It compiles fine, but why does it crash?

myLabel.text = age?[NSString stringWithFormat:@"%@", age]:@"";

Answer: If the variable age is not equal to zero then the result of stringWithFormat will be assigned to the label text. If it is zero then an empty string will be assigned. The format argument is wrong, %@ gets the description of an instance. Since age is probably a number the format argument needs to be %d. It crashes because objC tries to access the memory at position “age” which the app does not own and therefore causes an exception.


Quiz: The following three methods all allow you to connect the outlet in Interface Builder. What is the technical difference between them and which is the method recommended by Apple?

Variant 1:

@interface CocoaQuizViewController : UIViewController
{
	IBOutlet UILabel *myLabel;
}
@end

Variant 2:

@interface CocoaQuizViewController : UIViewController
{
	IBOutlet UILabel *myLabel;
}
 
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end

Variant 3:

@interface CocoaQuizViewController : UIViewController
{
	UILabel *myLabel;
}
 
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end

Answer: Method 3 is the correct one that should be preferred according to Apple. The instance that gets loaded from the XIB gets retained, just in case which prevents accidental unloading in case of low memory. Method 2 has a superfluous IBOutlet next to the instance variable because in this case the compiler will use the property anyway. Method 1 also can be connected, but it does not retain the loaded instance. This might cause problems if the instance gets deallocated for some reason.


Quiz: Complete this code so that it becomes syntactically correct using no more than one additional word and two square brackets.

NSInteger myNumber = @"1000";

Answer: @”1000″ is in itself a NSString instance. Therefore you can use it as receiver for the method integerValue. NSInteger myNumber = [@”1000″ integerValue];


Quiz: (Bonus Question) Why does the following code crash as soon as the property is set? What is the simple fix?

MeasureStripView.h

@interface MeasureLinealView : UIView
{
	NSUInteger minValue;
}
 
@property (nonatomic, assign) NSUInteger minValue;

MeasureStripView.m

#import "MeasureStripView.h"
 
@implementation MeasureStripView
 
@synthesize minValue;
 
 
#pragma mark Passthrough Properties
- (void) setMinValue:(NSUInteger)aVal
{
	// this overriding is absolutely necessary
	self.minValue = aVal;
}

Some other file

MeasureStripView *myMS;  // assume it is properly initialized and otherwise working
myMS.minValue = 100;   // this line crashes. If commented out the app works fine

Answer: Using self.minValue as an lvalue cases the setter method setMinValue to be called. This causes an endless loop running out of memory eventually. The fix is to remove the “self.” inside the setter because minValue is an instance variable and thus accessible just by it’s name throughout the class.

So how many did you catch? Be honest! If you also have a Cocoa riddle like these to contribute please mail them directly to me (oliver@drobnik.com) and I will publish them in this format.

How to Rename an Appthe

Andreas asks:

I need to rename my app to something else because the original name is a trademark of a UK-based company. After I replaced all instances of the name where I found it the name of the product is still the same. Now if I build the products stay red and I get “No launchable executable present at path.” I have changed the name EVERYWHERE. How can I fix that?

Apart from the quick fix that I found googling I am going to see if I can fix this in XCode myself. I’ll show all the steps for guidance and the “pro fix” at the end.

Read more

Display Local HTML file in UIWebView

Stefan87 asks:

Is it possible to display a HTML File which was created by the application in a UIWebview?

I would like to provide a helpfile in HTML-Format in the webview.

I’ve read the documentation, but it seems that every loadprocess requires a URL ?

I wanted to do the very same thing for displaying the instructions in LuckyWheel. After some experimentation I found the following way to be the simplest for me.

Read more

Wait a Week

Moshe asks:

“How can make my app show a message 1 week after an event? I want to show an alert view the first time the app is launched after at least 1 week has expired and the event is still true.”

This technique can be used for any kind of action you don’t want to do right away or somehow should be based on a value determined in a previous run of your app.
Read more

Sending E-Mail Attachments

Ravikumar Gajjavelly asks:

I want to send email It contains image as an attachment.How can we send am email.how to attach image.can anybody please give solution.

When creating my app GeoCorder I was faced with the exact same dilemma of needing to attach a file to an email. I did some research and this is what I found.

Read more

Should I Release Outlets?

bqbqhaha asks:

Let me ask you something as below.

  1. A variable declared as IBOutlet could be released?
  2. A variable declared as IBOutlet should be released?

I search for this issue many times, but still the answer is not found.
plz help me , plz

He is asking a question that most of us have when beginning with Interface Builder and Outlets. In plain English his question is “Should I release instance variables declared as IBOutlet?”

Read more

Date Closest to 'NOW'

Nagarajan asks:

I have needed a SQLite query that returns exactly a time which is very closest to the current time.

From 7:00pm,8:00pm, 9:00am and 10:00pm if the current time is 8:30pm, I will require 10:00pm.

This is more of a SQLite question than related to Cocoa Touch. But since SQLite is the mobile database engine of choice for Cocoa Touch, I’ll entertain this question as well. And follow it up with some Cocoa Code.

Read more