UIApplication (常用函数)



1.1 openURL (A访问B)

(A)


/**   
     *  A访问其它App
     *  URL格式:app2://?x=2,y=1
        1、app2 为 被访问程序 URL Schemes
        2、://  网址格式符,必须紧跟着 app2://  (如果只是openURL:"app2",无法访问app)
        3、? 这里分为带标识和不带标识,可以有两种格式(app2://URL identifier? 或者 app2://?)
           (这里方法3非访问App的必需参数,但是如果要传值,用?后面加上传递的值 x=1&y=2&z=4.....)
        4、值分隔符 (常用 '&'(推荐通用分隔符), 也可以','号,具体看两个App之间约定,)
     */
    NSURL *otherApplUrl = [NSURL URLWithString:@"app2://?x=2&y=1"];

    if ([[UIApplication sharedApplication] canOpenURL:otherApplUrl])
    {
        [[UIApplication sharedApplication] openURL:otherApplUrl];
    }
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{

    if (![sourceApplication isEqualToString:@"com.Jason.UIApplication"]) 
    {
        return NO;
    }
    
    //如果不传值过来,以下可以省略
    NSMutableDictionary *queryInfo = [NSMutableDictionary dictionary];
    
    NSString *query = [url query];
    NSLog(@"url query%@",query);
    
    if (!(query.length>0))
    {
        return NO;
    }
    query = [query stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    //根据值分隔符提取 key value
    NSArray *querys = [query componentsSeparatedByString:@"&"];
    for (NSString *str in querys) {
        NSArray *strs = [str componentsSeparatedByString:@"="];
        if ([strs count]==2) {
            [queryInfo setObject:[strs lastObject] forKey:[strs firstObject]];
        }
    }
    
    return YES;
}



Bug:"This app is not allowed to query for scheme ***"

原因:IOS 9限制了白名单App访问 和 强制https

解决: 添加 LSApplicationQueriesSchemes 和 App Transport

Xcode报错: 

-canOpenURL: failed for URL: "weixin://app/*************/" - error: "This app is not allowed to query for scheme weixin"

iOS 9系统策略更新,限制了http协议的访问,此外应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装。

受此影响,当你的应用在iOS 9中需要使用微信SDK的相关能力(分享、收藏、支付、登录等)时,需要在“Info.plist”里配置



0



1.2 其它openURL

//打开指定网页,程序外调用 Safari
    NSURL *webUrl = [NSURL URLWithString:@"http://www.baidu.com"];

    if ([[UIApplication sharedApplication] canOpenURL:webUrl])
    {
//        [[UIApplication sharedApplication] openURL:webUrl];
    }
    
    //拨打电话
    NSURL *phoneUrl = [NSURL URLWithString:@"132496006xx"];
    
    if ([[UIApplication sharedApplication] canOpenURL:phoneUrl])
    {
//        [[UIApplication sharedApplication] openURL:phoneUrl];
    }
    
    //打开邮箱
    NSURL *emailUrl = [NSURL URLWithString:@"mailto://2871186xx@qq.com"];

    if ([[UIApplication sharedApplication] canOpenURL:emailUrl])
    {
//        [[UIApplication sharedApplication] openURL:emailUrl];
    }
    
    //发送SMS
    NSURL *smslUrl = [NSURL URLWithString:@"sms://132496006xx"];
    
    if ([[UIApplication sharedApplication] canOpenURL:smslUrl])
    {
//        [[UIApplication sharedApplication] openURL:smslUrl];
    }
    
    //打开地图
    NSURL *mapUrl = [NSURL URLWithString:@"http://maps.google.com/maps?q=guangzhou"];
    
    if ([[UIApplication sharedApplication] canOpenURL:mapUrl])
    {
        [[UIApplication sharedApplication] openURL:mapUrl];
    
    }
    //打开蓝牙设置
    NSURL *url = [NSURL URLWithString:@"prefs:root=Bluetooth"];
    if ([[UIApplication sharedApplication]canOpenURL:url])
    {
        [[UIApplication sharedApplication]openURL:url];
    }


    //@delegate
    //应用全局单例
    AppDelegate *app = (AppDelegate*)[UIApplication sharedApplication].delegate;
    app.isFirst = YES;
    
    <p style="margin-top: 0px; margin-bottom: 0px; font-size: 13.5px; line-height: normal; font-family: Menlo; color: rgb(76, 191, 87);"><span style="font-variant-ligatures: no-common-ligatures; color: #ffffff">    </span>//rootViewController <span style="font-size: 13.5px; line-height: normal; font-family: 'Heiti SC Light';">的</span>keyWindow<span style="font-size: 13.5px; line-height: normal; font-family: 'Heiti SC Light';">是</span>nil, <span style="font-size: 13.5px; line-height: normal; font-family: 'Heiti SC Light';">经测试要经过页面更换才会由系统赋值</span>keyWindow</p>    //@keyWindow (readOnly)
    //在系统window中添加 view (ios9之后 如果用户实例化一个新的window,则会替代原先系统的window,旧项目中的集成三方控件会失去依附不显示)
//    TestView *testView = [[TestView alloc]init];
//    [testView show];
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(200, 200, 80, 80)];
    view.backgroundColor = [UIColor redColor];
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    
    
    //@statusBarStyle
    //状态栏字体颜色
    /**
     *  1.设置字体颜色 
     *
     *   1.1 info.plist 添加 (View controller-based status bar appearance)  值设为NO(可改变的)
     *
     *   1.2 (UIStatusBarStyleDefault 黑色字体,UIStatusBarStyleLightContent 白色字体)
     *
     */
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    
    
    //@IgnoringInteractionEvents
    //目前测试是界面交互开关(待) (beginIgnoringInteractionEvents 停止交互,endIgnoringInteractionEvents 结束限制,    isIgnoringInteractionEvents 当前交互状态)
    [[UIApplication sharedApplication] beginIgnoringInteractionEvents];
    BOOL b = [[UIApplication sharedApplication] isIgnoringInteractionEvents];

    [[UIApplication sharedApplication] endIgnoringInteractionEvents];
    BOOL bb = [[UIApplication sharedApplication] isIgnoringInteractionEvents];


//-(void)sendAction...
    //隐藏键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];

    
    //@networkActivityIndicatorVisible default is NO
    //显示网络等待菊花
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    
    
    //@statusBarHidden
    //隐藏状态栏 (自带两种动画效果,Fade 褪减、 slider 滑动)
    //    [UIApplication sharedApplication].statusBarHidden = YES;
    //    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    
    
    
    //@statusBarOrientation
    //状态栏方向 (需要是可读、写常数值,但官方不建义此值去改变状态栏方向)
    /**
     *
     *  UIInterfaceOrientationUnknown            = UIDeviceOrientationUnknown,            未知
     UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,           1 竖屏正面向上
     UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown, 2 竖屏正面向下
     UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,     3 横屏touchIcon在右
     UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft       4 横屏touchIcon在左
     *
     *
     */
//    [[UIApplication sharedApplication] setStatusBarOrientation:UIDeviceOrientationLandscapeLeft animated:YES];
//        [UIApplication sharedApplication].statusBarOrientation = UIDeviceOrientationLandscapeRight;
    
    NSLog(@"+++++%d",[UIApplication sharedApplication].statusBarOrientation);
    
    
    //-(NSUinteger)supportedInterfaceOrientationsForWindow:
    //获取当前应用所支持的屏幕方向
    /**
     *  返回是一个位值和,
        2    Portrait
        4    UpsideDown
        16   Landscape Left
        8    Landscape Right
     */
    NSUInteger i = [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.view.window];
    NSLog(@"support=%d",i); //support = 26 = 2+16+8
    
    
    
    //@statusBarOrientationAnimationDuration (readonly)
    //状态栏改变方面的动画时长
    NSTimeInterval tim = [UIApplication sharedApplication].statusBarOrientationAnimationDuration;
    NSLog(@"tim+++%lf",tim);//0.3秒
    
    
    
    //@statusBarFrame (readonly)
    //状态栏尺寸,如果隐藏返回 CGRectZero
    CGRect fram = [UIApplication sharedApplication].statusBarFrame; //(0,0,deviceWidth,20)
    
    
    
    //@applicationIconBadgeNumber
    //每次打开App的推送数量 (显示在图标右上角未读) 控件显示原则和Button类似,超过显示会默认中间省略符 (10..00)
    //IOS 8 要注册推送授权 , IOS 7不用
    [UIApplication sharedApplication].applicationIconBadgeNumber = 100000000; //(10..00)
    
    
    
    //@applicationSupportsShakeToEdit
    //是否允许设备摇一摇 (default is YES)
    [UIApplication sharedApplication].applicationSupportsShakeToEdit = NO;
    
    
    
    //@property(nonatomic,readonly) UIApplicationState applicationState
    //获取程序当前处于哪个状态
    /**
     *  UIApplicationStateActive,    活动
        UIApplicationStateInactive,  不活动
        UIApplicationStateBackground 后台
     */
    UIApplicationState appState = [UIApplication sharedApplication].applicationState; //first run is Inactive in VC
    
    
    
    //@property(nonatomic,readonly) NSTimeInterval backgroundTimeRemaining
    //后台停留时间,该变量程序进入后台在相应方法里面取值
    NSTimeInterval timeRemain = [UIApplication sharedApplication].backgroundTimeRemaining;
    NSLog(@"remainTime %.1f",timeRemain);
    
    
    
    //-beginBackgroundTaskWithExpirationHandler  -endBackgroundTask 合着使用
    //在- (void)applicationDidEnterBackground: 的时候向后台借时间
    //ios 8之前是10分钟,之后是3分钟,以下方法写在进入后台的AppDelegate.m方法里
    __block UIBackgroundTaskIdentifier bgTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        
        //达到最大时间后进入,处理内存清理
        [[UIApplication sharedApplication] endBackgroundTask:bgTaskID];
        bgTaskID = UIBackgroundTaskInvalid;
    }];

//-(void)setMinimumBackgroundFetchInterval
    //调整为最小间隔时间去刷新后台
    //回调方法 -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    //设置参考:http://www.tekuba.net/program/320/
    [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    
    
    
    //@backgroundRefreshStatus (readonly)
    //该App后台刷新权限
    UIBackgroundRefreshStatus refreshStatus = [UIApplication sharedApplication].backgroundRefreshStatus;
    //如果不是激活状态
    if (refreshStatus != UIBackgroundRefreshStatusAvailable)
    {
        //"设置"--“通用”--“后台应用程序刷新”--“打开”
    }
    
    


    //@protectedDataAvailable (readonly)
    //当前NSFileManager 是否可读写 (文档解析,待后续验证)
    BOOL isAvailable = [UIApplication sharedApplication].protectedDataAvailable;
    
    
    
    
    //@preferredContentSizeCategory (UICTContentSizeCategoryL)
    //当前显示字体大小类型,对于一个可能的值的列表,请参阅内容大小类常量和可访问性内容大小类常量。Content Size Category Constants and Accessibility Content Size Category Constants.
    NSString *sizeCategory = [UIApplication sharedApplication].preferredContentSizeCategory;
    

    //推送
    //1.1 ios 8
    //设置通知类型
    UIUserNotificationSettings *set = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:set];
    //获取推送通知类型
    UIUserNotificationSettings *str = [[UIApplication sharedApplication] currentUserNotificationSettings];
    //-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {[[UIApplication sharedApplication] registerForRemoteNotifications];}
    
    //1.2 ios 7
    UIRemoteNotificationType types = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:types];
    
    
    
    //- (void)scheduleLocalNotification:(UILocalNotification *)notification
    //设定本地通知
    UILocalNotification *localNotification = [[UILocalNotification alloc]init];
    localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    localNotification.alertBody = @"2222222,你会怎样处理";
    localNotification.alertTitle = @"通知";
    localNotification.alertAction = @"查看";
    localNotification.repeatInterval = NSCalendarUnitMinute;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    //延迟推送
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    //马上推送
    //    [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
    
    //-(void)cancelLocalNotification
    //清除指定推送
    //    [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
    //清除所有推送
    //    [[UIApplication sharedApplication] cancelAllLocalNotifications];



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值