ma11hew28
ma11hew28

Reputation: 126457

iOS: Can openURL ever be nil?

For -[UIApplicationDelegate application:openURL:sourceApplication:annotation:], can the URL ever be nil? I'm asking because I saw someone put in a check if (url) // ..., but isn't that unnecessary? I.e., doesn't this method always get called with a non-nil NSURL argument for openURL?

Upvotes: 2

Views: 1145

Answers (2)

jdoyle1983
jdoyle1983

Reputation: 831

According to Apple Documentation, the url parameter is:

A object representing a URL (Universal Resource Locator). See Apple URL Scheme Reference for Apple-registered schemes for URLs.

You have to register your application to accept specific URLs.
This article shows how it is done.

This would lead me to believe that this method would not be called unless it adhered to a URL Schema specified by the application.

If it is being called internally, than you would know whether you would need to check for nil or not. As for an outside application calling it, I can't see how that would happen.

Upvotes: 2

Roger
Roger

Reputation: 15813

Assuming an external invocation (ie from another app trying to open yours), then I would assume it is non-nil, however perhaps (unlikely I know) you call it internally in which case it might be?

Assumptions however are dangerous!

It would therefore seem prudent to sanity check the value before doing something that might cause a crash should the value be nil.

Seems OK to check if you ask me.

Upvotes: 2

Related Questions