iOS devtips

作者: Devbrave | 来源:发表于2017-07-25 11:20 被阅读29次

    主要记录一些在平时开发中用到的一些小知识点

    1、tableViewCell初始化时默认的宽度为320(可能是遗留的bug),所以实际布局时在- (void)layoutSubviews方法中重载子视图frame布局。
    2、- (void)layoutSubviews方法调用时机:1>视图有frame变化 2>addsubview的时候 3、init时是不会触发,只有在init带frame时会触发
    3、-(void)setNeedsLayout标记需要重新布局:异步调用-layoutIfNeeded刷新布局,一定会调用- (void)layoutSubviews
    4、-(void)layoutIfNeed如果有要刷新的标记,立即调用-(void)layoutSubviews
    立即刷新的方法:

    //刷新的视图
    [view layoutIfNeed];
    

    5、 拨打电话的API

    NSString *phoneNumber =  [NSString stringWithFormat:@"telprompt:%@", phone];
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
        });
    

    在iOS10 之后会阻塞主线程,导致有很长的延迟。加上一个异步线程去处理会减少延迟。

    6、 iOS11中AppIcon不显示的问题
    使用了CocoaPods的Xcode工程,在iOS11版的手机上AppIcon不显示,原因是CocoaPods的资源编译脚本在iOS11下出了点问题.需要修改脚本.两种修改方式:

    • 在Podfile添加脚本修改
      1>在Podfile 添加如下代码
    post_install do |installer|
        copy_pods_resources_path = "Pods/Target Support Files/Pods-[工程名]/Pods-[工程名]-resources.sh"
        string_to_replace = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"'
        assets_compile_with_app_icon_arguments = '--compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${BUILD_DIR}/assetcatalog_generated_info.plist"'
        text = File.read(copy_pods_resources_path)
        new_contents = text.gsub(string_to_replace, assets_compile_with_app_icon_arguments)
        File.open(copy_pods_resources_path, "w") {|file| file.puts new_contents }
    end
    

    需要注意的是,将[工程名] 换成自己工程的名称
    2>然后运行

    $pod install
    
    • 手动修改
      打开工程目录下:[工程名]/Pods/Target Support Files/Pods-[工程名]/Pods-[工程名]-resources.sh这个文件,替换最后一段代码:
    //修改前
    printf "%s\0" "${XCASSET_FILES[@]}" | xargs -0 xcrun actool --output-format human-readable-text --notices --warnings --platform "${PLATFORM_NAME}" --minimum-deployment-target "${!DEPLOYMENT_TARGET_SETTING_NAME}" ${TARGET_DEVICE_ARGS} --compress-pngs --compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}"
    fi
    //修改后
    printf "%s\0" "${XCASSET_FILES[@]}" | xargs -0 xcrun actool --output-format human-readable-text --notices --warnings --platform "${PLATFORM_NAME}" --minimum-deployment-target "${!DEPLOYMENT_TARGET_SETTING_NAME}" ${TARGET_DEVICE_ARGS} --compress-pngs --compile "${BUILT_PRODUCTS_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}" --app-icon "${ASSETCATALOG_COMPILER_APPICON_NAME}" --output-partial-info-plist "${BUILD_DIR}/assetcatalog_generated_info.plist"
    fi
    

    7、Xcode上传IPA包到iTunes Connect后构建版本不显示
    这个问题是我在xcode升级到9.0之后上传一个新的APP时遇到的,因为第一个版本APP比较简单所以很多用户权限不需要用,导致在plist文件中权限声明没有加全。出现的问题就是IPA包在上传到iTunes Connect中在构建中显示了一会儿就消失不见了,Apple也没有任何的提示。经过查阅资料发现有以下几个原因导致的:

    • 未声明完整用户权限,iOS10以后Apple更注重保护用户权限,在任何需要使用设备权限的地方都需要提前声明.
    • 访问了Apple禁止的私有api,一般的是在第三方库中(这个就需要花费人工一个个的去排查了)。
      第一个的解决办法就是在plist文件中添加以下权限声明:
    <key>NSCameraUsageDescription</key>
        <string>是否允许使用相机?</string>
        <key>NSContactsUsageDescription</key>
        <string>是否允许访问通讯录?</string>
        <key>NSMicrophoneUsageDescription</key>
        <string>是否允许使用麦克风?</string>
        <key>NSPhotoLibraryUsageDescription</key>
        <string>是否允许访问媒体资料库?</string>
    

    我在设置了这几项之后提交就没有问题了,其他的几项可以需要时再设置。

    8、WKWebView加载本地HTML文件

      //Base路径
        NSURL *baseUrl = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
        //文件路径
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"headLines" ofType:@"html"];
        //html文件
        NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        [self.webView loadHTMLString:html baseURL:baseUrl];
    

    向HTML文件中插入数据


    HTML示例截图
        NSURL *baseUrl = [NSURL fileURLWithPath:[NSBundle mainBundle].bundlePath];
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"headLines" ofType:@"html"];
        NSString *html = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
        NSString *html1 = [html stringByReplacingOccurrencesOfString:@"{title}" withString:@"头条文章"];
        NSString *html2 =  [html1 stringByReplacingOccurrencesOfString:@"{time}" withString:@"2018-7-09"];
        NSString *html3 = [html2 stringByReplacingOccurrencesOfString:@"{body}" withString:@"头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章头条文章"];
        [self.webView loadHTMLString:html3 baseURL:baseUrl];
    

    根据HTML中的标示替换字符串即可

    9、动画切换window的根控制器

    // options是动画选项
    
    [UIView transitionWithView:[UIApplication sharedApplication].keyWindow duration:0.5f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
    BOOL oldState = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    [UIApplication sharedApplication].keyWindow.rootViewController = [RootViewController new];
    [UIView setAnimationsEnabled:oldState];
    } completion:^(BOOL finished) {
    }];
    

    10、拨打电话的API

        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
        });
    在iOS10 之后会阻塞主线程,导致有很长的延迟。加上一个异步线程去处理会减少延迟。
    

    持续更新中

    相关文章

      网友评论

        本文标题:iOS devtips

        本文链接:https://www.haomeiwen.com/subject/nmkwkxtx.html