在iOS 5上使用libcurl作为NSURLConnection的替代方案

bia*_*bit 11 libcurl nsurlconnection ios5

更新:NSURLConnection现在似乎正确支持100-Continue.在任何情况下,此答案都包含指向用于为iOS/OSX构建libcurl的脚本的链接.

NSURLConnection考虑到它不支持RFC 2616(HTTP/1.1)的8.2.3节,我有点困难.

基本上客户端需要能够支持发送标头Expect: 100-Continue; 在发送请求标头之后,它必须100在发送POST/ PUTbody 之前等待来自具有状态代码的服务器的响应.

此外,NSURLConnection(并且因此构建在其上的所有库)在上传所有数据之前不会从服务器返回任何响应 - 这是一个痛苦,因为服务器可能立即拒绝上载(无效凭证,没有空格,文件太大了等).虽然它对小文件"工作"(内容完全上传,并且调用了响应的委托方法),但在大文件上而不是从服务器获取错误响应(我肯定会发送它),我只是得到一个"连接失败"错误.

我知道libcurl正确支持100-Continue规范所以我需要一些帮助来编译它并在iOS 5项目上运行它.

我从这篇文章开始(滚动到底部)但我无法走远...

做出这些改变......

export CC=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
export CFLAGS="-arch armv7 --sysroot=/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.0.sdk"
export CPP=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-cpp-4.2
export AR=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/ar
./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=arm-apple-darwin10 --build=arm-apple-darwin10
make clean
make
ar rv libcurl.armv7.a lib/*.o
Run Code Online (Sandbox Code Playgroud)

...但编译因消息而失败

(...)
checking for arm-apple-darwin10-gcc... /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2
checking whether the C compiler works... no
configure: error: in `/Users/bruno/Downloads/curl-7.21.4':
configure: error: C compiler cannot create executables
Run Code Online (Sandbox Code Playgroud)

我正在使用从Apple的开源网站下载的curl 7.21.4 .

那么,我如何编译curl并在iOS 5项目中使用它,最好是支持SSL?

小智 15

这对我来说最新的SDK:

#!/bin/sh 
export SDK=5.0

buildit()
{
    target=$1
    platform=$2

    export CC=/Developer/Platforms/${platform}.platform/Developer/usr/bin/gcc
    export CFLAGS="-arch ${target} -isysroot /Developer/Platforms/${platform}.platform/Developer/SDKs/${platform}${SDK}.sdk"
    export CPP="/Developer/Platforms/${platform}.platform/Developer/usr/bin/llvm-cpp-4.2"
    export AR=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ar
    export RANLIB=/Developer/Platforms/${platform}.platform/Developer/usr/bin/ranlib

    ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap \
            --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a
Run Code Online (Sandbox Code Playgroud)

  • 在ranlib二进制文件上有一个小错字(你有"ranib"),除此之外,这是完美的.谢谢! (2认同)

Ber*_*ier 9

获得重要更新:C compiler cannot create executables
确保安装命令行XCode工具...

从XCode菜单中选择: Preferences --> downloads --> Command Line Tools

...如果没有安装,请点击"安装".

上述脚本的问题在于它适用于旧版本的XCode.如果安装了最新的XCode,则所有编译器工具的位置都会不同.我已经更改了脚本以支持旧的和新的XCode版本,并且内置了一些错误检查.这有帮助吗?

下面 - 我假设SDK版本5.1 - 您可能需要修改...

#!/bin/bash

set -e

export SDK=5.1

checkExists() {

    if [ ! -e $1 ]
    then
        echo "Didn't find: $1 -- try to locate parts of this to see how to fix the path"
        exit 1
    else 
        echo "using $1"
    fi

}

buildit() {

    target=$1
    platform=$2

    root="/Applications/Xcode.app/Contents/Developer/Platforms/${platform}.platform/Developer"
    oldRoot="/Developer/Platforms/${platform}.platform/Developer"

    if [ ! -d "${root}" ]
    then
        root="${oldRoot}"
    fi

    if [ ! -d "${root}" ]
    then
        echo " "
        echo "Oopsie.  You don't have an iOS SDK root in either of these locations: "
        echo "   ${root} "
        echo "   ${oldRoot}"
        echo " "
        echo "If you have 'locate' enabled, you might find where you have it installed with:"
        echo "   locate iPhoneOS.platform | grep -v 'iPhoneOS.platform/'"
        echo " "
        echo "and alter the 'root' variable in the script -- or install XCode if you can't find it... "
        echo " "
        exit 1
    fi

    export CC="${root}/usr/bin/gcc"
    export CFLAGS="-arch ${target} -isysroot ${root}/SDKs/${platform}${SDK}.sdk"
    export CPP="${root}/usr/bin/llvm-cpp-4.2"
    export AR="${root}/usr/bin/ar"
    export RANLIB="${root}/usr/bin/ranlib"

    checkExists ${CC}
    checkExists ${root}/SDKs/${platform}${SDK}.sdk
    checkExists ${CPP}
    checkExists ${AR}
    checkExists ${RANLIB}

    ./configure --disable-shared --without-ssl --without-libssh2 --without-ca-bundle --without-ldap --disable-ldap --host=${target}-apple-darwin10

    make clean
    make
    $AR rv libcurl.${target}.a lib/*.o
}

buildit armv6 iPhoneOS
buildit armv7 iPhoneOS
buildit i386 iPhoneSimulator

lipo -create libcurl.armv7.a libcurl.armv6.a libcurl.i386.a -output libcurl.a
Run Code Online (Sandbox Code Playgroud)