iOSDevSupport我的iOS开发小屋

iOS 开发中的『库』(一)

2016-09-29  本文已影响10214人  Damonwong

看文章之前,你可以看下下面几个问题,如果你都会了,或许可以不看。


我是前言


理论篇

动态库 VS. 静态库

Static frameworks are linked at compile time. Dynamic frameworks are linked at runtime

静态库 动态库

.framework VS .a

.tbd VS .dylib

小总结

Embedded VS. Linked

Embedded frameworks are placed within an app’s sandbox and are only available to that app. System frameworks are stored at the system-level and are available to all apps.

Embedded && Link
- (void)dlopenLoad{
    NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents/Dylib.framework/Dylib",NSHomeDirectory()];
    [self dlopenLoadDylibWithPath:documentsPath];
}

- (void)dlopenLoadDylibWithPath:(NSString *)path
{
    libHandle = NULL;
    libHandle = dlopen([path cStringUsingEncoding:NSUTF8StringEncoding], RTLD_NOW);
    if (libHandle == NULL) {
        char *error = dlerror();
        NSLog(@"dlopen error: %s", error);
    } else {
        NSLog(@"dlopen load framework success.");
    }
}

关于制作过程


实践篇

framework 的方式实现 app 的热修复

实现大致思路

    
    NSString *fileName = @"remote";
    
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentDirectory = nil;
    if ([paths count] != 0) {
        documentDirectory = [paths objectAtIndex:0];
    }

    NSFileManager *manager = [NSFileManager defaultManager];
    NSString *bundlePath = [[NSBundle mainBundle]
                                         pathForResource:fileName ofType:@"framework"];
    
    BOOL loadDocument = YES;
    
    // Check if new bundle exists
    if (![manager fileExistsAtPath:bundlePath] && loadDocument) {
        bundlePath = [documentDirectory stringByAppendingPathComponent:[fileName stringByAppendingString:@".framework"]];
    }

    // Load bundle
    NSError *error = nil;
    NSBundle *frameworkBundle = [NSBundle bundleWithPath:bundlePath];
    
    if (frameworkBundle && [frameworkBundle loadAndReturnError:&error]) {
        NSLog(@"Load framework successfully");
    }else {
        NSLog(@"Failed to load framework with err: %@",error);
        
    }
    // Load class
    Class PublicAPIClass = NSClassFromString(@"PublicAPI");
    if (!PublicAPIClass) {
        NSLog(@"Unable to load class");
        
    }
    
    NSObject *publicAPIObject = [PublicAPIClass new];
    [publicAPIObject performSelector:@selector(mainViewController)];

番外篇

关于lipo

<a name="lipo"/>

$ lipo -info /Debug-iphoneos/Someframework.framwork/Someframework
# Architectures in the fat file: Someframework are: armv7 armv7s arm64 
# 合并
$ lipo –create a.framework b.framework –output output.framework
#拆分
$ lipo –create a.framework -thin armv7 -output a-output-armv7.framework

<a name="build"/>

从源代码到app

当我们点击了 build 之后,做了什么事情呢?

ld && libtool

Build phases && Build rules && Build settings


谈谈 Mach-O

Mach-O

参考资料


后记

@我就叫Sunny怎么了 提出的问题。

问题

更多

工作之余,写了点笔记,如果需要可以在我的 GitHub 看。

上一篇 下一篇

猜你喜欢

热点阅读