【iOS开发】判断app启动的方式(launchOptions)

iOS app启动的方式有哪些:

  1. 自己启动(用户手动点击启动)
  2. urlscheme启动(关于urlScheme的详解点击打开链接
  3. 本地通知启动  (自己写的本地通知启动,蓝牙模块的启动,地理围栏的启动)
  4. 远程通知启动    (后台服务器的推送通知)

在appdelegate.m文件中

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
系统给的一些关于launchoption的字典里面的内容

UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsURLKey                   NS_AVAILABLE_IOS(3_0); // userInfo contains NSURL with launch URL
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsSourceApplicationKey     NS_AVAILABLE_IOS(3_0); // userInfo contains NSString with launch app bundle ID
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsRemoteNotificationKey    NS_AVAILABLE_IOS(3_0) __TVOS_PROHIBITED; // userInfo contains NSDictionary with payload
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsLocalNotificationKey     NS_AVAILABLE_IOS(4_0) __TVOS_PROHIBITED; // userInfo contains a UILocalNotification
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsAnnotationKey            NS_AVAILABLE_IOS(3_2); // userInfo contains object with annotation property list
UIKIT_EXTERN NSString *const UIApplicationProtectedDataWillBecomeUnavailable    NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN NSString *const UIApplicationProtectedDataDidBecomeAvailable       NS_AVAILABLE_IOS(4_0);
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsLocationKey              NS_AVAILABLE_IOS(4_0); // app was launched in response to a CoreLocation event.
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsNewsstandDownloadsKey    NS_AVAILABLE_IOS(5_0) __TVOS_PROHIBITED; // userInfo contains an NSArray of NKAssetDownload identifiers
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsBluetoothCentralsKey     NS_AVAILABLE_IOS(7_0); // userInfo contains an NSArray of CBCentralManager restore identifiers
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsBluetoothPeripheralsKey  NS_AVAILABLE_IOS(7_0); // userInfo contains an NSArray of CBPeripheralManager restore identifiers
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsShortcutItemKey          NS_AVAILABLE_IOS(9_0) __TVOS_PROHIBITED; // userInfo contains the UIApplicationShortcutItem used to launch the app.

// Key in options dict passed to application:[will | did]FinishLaunchingWithOptions and info for UIApplicationDidFinishLaunchingNotification
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsUserActivityDictionaryKey    NS_AVAILABLE_IOS(8_0); // Sub-Dictionary present in launch options when user activity is present
UIKIT_EXTERN NSString *const UIApplicationLaunchOptionsUserActivityTypeKey          NS_AVAILABLE_IOS(8_0); // Key in user activity dictionary for the activity type

UIKIT_EXTERN NSString *const UIApplicationOpenSettingsURLString NS_AVAILABLE_IOS(8_0);

// Keys for application:openURL:options:
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsSourceApplicationKey NS_AVAILABLE_IOS(9_0);   // value is an NSString containing the bundle ID of the originating application
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsAnnotationKey NS_AVAILABLE_IOS(9_0);   // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsOpenInPlaceKey NS_AVAILABLE_IOS(9_0);   // value is a bool NSNumber, set to YES if the file needs to be copied before use

如何判断呢?代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    
    //用户自己点击启动
    if(!launchOptions)
    {
        NSLog(@"用户点击app启动");
    }
    else
    {
        NSURL *url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
        //app 通过urlscheme启动
        if (url) {
            NSLog(@"app 通过urlscheme启动 url = %@",url);
        }
        UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
        //通过本地通知启动
        if(localNotification)
        {
            NSLog(@"app 通过本地通知启动 localNotification = %@",localNotification);
        }
        NSDictionary *remoteCotificationDic = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
        //远程通知启动
        if(remoteCotificationDic)
        {
            NSLog(@"app 通过远程推送通知启动 remoteCotificationDic = %@",remoteCotificationDic);
        }
        
    }
    
    
    
    
    return YES;
}

//位置变动的通知
        if(launchOptions[@"UIApplicationLaunchOptionsLocationKey"])
        {
            //写你的重新定位的代码
        }

总结:当launchOptions为空时是用户自己点击app启动,不为空时,要根据相应的key是否存在来判断是通过哪种方式启动的。





  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
iOS开发中实现Twitter登录功能的Demo可以按照以下步骤进行: 1. 首先,在Twitter开发者网站上创建一个应用,获取应用的API Key和Secret Key,并将它们添加到项目的Info.plist文件的URL schemes和URL whitelist中。 2. 在AppDelegate中导入Twitter框架并添加以下代码: ```swift import TwitterKit func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { TWTRTwitter.sharedInstance().start(withConsumerKey: "YOUR_API_KEY", consumerSecret: "YOUR_API_SECRET") return true } ``` 3. 在需要实现登录的ViewController中导入Twitter框架并添加以下代码: ```swift import TwitterKit @IBAction func twitterLoginButtonTapped(_ sender: UIButton) { TWTRTwitter.sharedInstance().logIn { (session, error) in if (session != nil) { print("Twitter login successful with user ID: \(session!.userID)") } else { print("Twitter login error: \(error!.localizedDescription)") } } } ``` 在上述代码中,当用户点击Twitter登录按钮时,调用`TWTRTwitter.sharedInstance().logIn`方法会打开Twitter登录界面,用户在该界面上登录成功后,会返回一个session,其中包含用户的userID和token等信息。根据返回的session是否为nil,判断登录是否成功。 注意:在使用此Demo之前,需要在项目的Build Settings中的Other Linker Flags添加`-ObjC`标志,否则可能会出现链接错误。 以上就是实现Twitter登录功能的iOS开发Demo。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMSmile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值