ios swift Alamofire5网络请求

背景

网络请求工具的使用。
app开发离不开网络,我们页面显示的数据几乎来自网络。
我们就从简单的网络请求开始尝试。

网络请求

新建一个UIViewController,让storyboard指向我们最新的控制器。
作为第一个启动的页面。

Xnip20211129_225552.png

导入网络请求库

编写Podfile文件,增加两个依赖。


# 网络请求
pod 'Alamofire', '~> 5.2.2'
  
# json
pod 'HandyJSON', '~> 5.0.2'

关闭xcode,进入项目根目录,打开命令行终端。


pod install

等待下载依赖完成。


Analyzing dependencies
Downloading dependencies
Installing Alamofire (5.2.2)
Installing HandyJSON (5.0.2)
Generating Pods project
Integrating client project

重新打开项目,开始编写我们的网络请求。

carbonnet.png

编译运行,看控制的输出。

当我们请求之后,发现失败了,我们可以看控制台输出的信息

Xnip20211129_215529.png

sessionTaskFailed(error: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

要求我们使用安全的连接,因为我们这是用的是http访问,不安全,需要我们换https,但是我就是要http怎么办?

就好像安卓9开始要求https一样,应该也有类似的配置,让我们去允许http访问吧。

打开Google,搜索NSURLErrorDomain Code=-1022,发现解决方案。

打开info.plist文件,可以用文本编辑工具打开,在末尾增加如下代码


<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>

允许http访问。完整整的数据如下,这是我的demo。


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleDevelopmentRegion</key>
	<string>$(DEVELOPMENT_LANGUAGE)</string>
	<key>CFBundleExecutable</key>
	<string>$(EXECUTABLE_NAME)</string>
	<key>CFBundleIdentifier</key>
	<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	<key>CFBundleInfoDictionaryVersion</key>
	<string>6.0</string>
	<key>CFBundleName</key>
	<string>$(PRODUCT_NAME)</string>
	<key>CFBundlePackageType</key>
	<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
	<key>CFBundleShortVersionString</key>
	<string>1.0</string>
	<key>CFBundleVersion</key>
	<string>1</string>
	<key>LSRequiresIPhoneOS</key>
	<true/>
	<key>UIApplicationSceneManifest</key>
	<dict>
		<key>UIApplicationSupportsMultipleScenes</key>
		<false/>
		<key>UISceneConfigurations</key>
		<dict>
			<key>UIWindowSceneSessionRoleApplication</key>
			<array>
				<dict>
					<key>UISceneConfigurationName</key>
					<string>Default Configuration</string>
					<key>UISceneDelegateClassName</key>
					<string>$(PRODUCT_MODULE_NAME).SceneDelegate</string>
					<key>UISceneStoryboardFile</key>
					<string>Main</string>
				</dict>
			</array>
		</dict>
	</dict>
	<key>UIApplicationSupportsIndirectInputEvents</key>
	<true/>
	<key>UILaunchStoryboardName</key>
	<string>LaunchScreen</string>
	<key>UIMainStoryboardFile</key>
	<string>Main</string>
	<key>UIRequiredDeviceCapabilities</key>
	<array>
		<string>armv7</string>
	</array>
	<key>UISupportedInterfaceOrientations</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
	</array>
	<key>UISupportedInterfaceOrientations~ipad</key>
	<array>
		<string>UIInterfaceOrientationPortrait</string>
		<string>UIInterfaceOrientationPortraitUpsideDown</string>
		<string>UIInterfaceOrientationLandscapeLeft</string>
		<string>UIInterfaceOrientationLandscapeRight</string>
	</array>
	<key>NSAppTransportSecurity</key>
	<dict>
    	<key>NSAllowsArbitraryLoads</key>
    	<true/>
</dict>
</dict>
</plist>


再次在xcode打开info.plist文件,底下增加多了这一项数据。

Xnip20211129_220958.png

到这里http文件解决了。再次允许我们的demo,看控制台。

Xnip20211129_221042.png

请求数据成功了,这是我的博客的首页的文章列表。
下一步就是把json转对象了。

总结

  1. 网络请求框架,搜索下网络最流行的就ok
  2. 通常错误会在控制台打印,我们留意控制台异常信息,进行解读,关键词搜索,基本能解决

项目地址:https://gitee.com/dong_rong/ios_swift_demo.git

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×