Read Uiapplicationlaunchoptionsurlkey in Swift

Read UIApplicationLaunchOptionsURLKey in Swift

Swift 3:

In Swift 3, launchOptionsis a dictionary of type [UIApplicationLaunchOptionsKey: Any]?, so you'd access the value like this:

launchOptions?[UIApplicationLaunchOptionsKey.url]

Since the key type is UIApplicationLaunchOptionsKey, you can abbreviate the enum type as simply .url:

launchOptions?[.url]

The value associated with that key is a URL though, and not a String. Also, the key might not be present in the dictionary, so you should use conditional casting as? instead of normal casting.

In Swift, you want to do:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let url = launchOptions?[.url] as? URL {
// If we get here, we know launchOptions is not nil, we know
// key .url was in the launchOptions dictionary, and we know
// that the type of the launchOptions was correctly identified
// as URL. At this point, url has the type URL and is ready to use.
}

Swift 2:

In your code, launchOptions is a dictionary of type [NSObject: AnyObject]?, so you'd want to access the value like this:

options?[UIApplicationLaunchOptionsURLKey]

The value associated with that key is an NSURL though, and not a String. Also, the key might not be present in the dictionary, so you should use conditional casting as? instead of normal casting.

In Swift, you want to do:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if let url = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
// If we get here, we know launchOptions is not nil, we know
// UIApplicationLaunchOptionsURLKey was in the launchOptions
// dictionary, and we know that the type of the launchOptions
// was correctly identified as NSURL. At this point, url has
// the type NSURL and is ready to use.
}

Launch App using URL, but OpenUrl Not Called

I agree with Kaloyan, "handleOpenURL" is never called at application launch. So you have to check for URL in "launchOptions" in didFinishLaunchingWithOptions.

HOWEVER

I adopted the same solution as Apple example code for QuickActions (3D Touch). I keep the URL at launch in a variable, and I handle it in applicationDidBecomeActive:.

@interface MyAppDelegate ()
@property (nonatomic, strong) NSURL *launchedURL;
@end

@implementation MyAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.launchedURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
...
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (self.launchedURL) {
[self openLink:self.launchedURL];
self.launchedURL = nil;
}
}

- (BOOL) application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
NSURL *openUrl = url;

if (!openUrl)
{
return NO;
}
return [self openLink:openUrl];
}

- (BOOL)openLink:(NSURL *)urlLink
{
...
}

@end

Swift Error - extra argument 'option' in call for SimpleAuth

It's your definition of your completion block. SimpleAuthRequestHandler is defined as:

typedef void (^SimpleAuthRequestHandler) (id responseObject, NSError *error);

But your completion block/closure is defined as:

(responseObject : NSDictionary!, error : NSError!) -> Void in

You can't just change the type from id (AnyObject in Swift) to NSDictionary! without explicitly casting it. Your call should look something like this:

SimpleAuth.authorize("instagram", options: ["scope" : "likes"], completion: {
(responseObject : AnyObject!, error : NSError!) -> Void in

/* ... */
})

You can then make responseObject an NSDictionary with a cast:

var response = responseObject as NSDictionary

iOS open attached file in an app. No such file or directory

Your code to get the file path isn't correct. Replace lines like this:

let path = "" + (file?.filePathURL?.description)!

with:

let path = file?.path

FYI - never use the description method for anything other than debugging and logging.

Disclaimer - I'm not fluent in Swift so you may need to tweak my answer with a ! or ? somewhere in there.



Related Topics



Leave a reply



Submit