├── .gitignore ├── README.md └── WKWebViewAndUIWebViewCookiesDemo ├── WKWebViewAndUIWebViewCookiesDemo.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata └── WKWebViewAndUIWebViewCookiesDemo ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── UIWebViewViewController.h ├── UIWebViewViewController.m ├── WKCookieSyncManager.h ├── WKCookieSyncManager.m ├── WKWebViewViewController.h ├── WKWebViewViewController.m └── main.m /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata 19 | 20 | ## Other 21 | *.xccheckout 22 | *.moved-aside 23 | *.xcuserstate 24 | *.xcscmblueprint 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | 30 | # CocoaPods 31 | # 32 | # We recommend against adding the Pods directory to your .gitignore. However 33 | # you should judge for yourself, the pros and cons are mentioned at: 34 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 35 | # 36 | # Pods/ 37 | 38 | # Carthage 39 | # 40 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 41 | # Carthage/Checkouts 42 | 43 | Carthage/Build 44 | 45 | # fastlane 46 | # 47 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 48 | # screenshots whenever they are needed. 49 | # For more information about the recommended setup visit: 50 | # https://github.com/fastlane/fastlane/blob/master/docs/Gitignore.md 51 | 52 | fastlane/report.xml 53 | fastlane/screenshots 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WKWebViewAndUIWebViewCookiesDemo 2 | 一个解决uiwebView和wkwebview cookies交互的demo 3 | 4 | 具体见博客:http://fengqiang.leanote.com/post/iOS%E5%BC%80%E5%8F%91-%E6%89%93%E9%80%9AUIWebView%E5%92%8CWKWebView%E7%9A%84Cookie (这个博客已经停止使用) 5 | 6 | 7 | 博客新地址:http://fengqiangboy.com/14611518603473.html 8 | 9 | 2016.11.3: 貌似苹果已经修复这个bug了,我在iOS9和iOS10上面测试,发现uiwebView和wkwebview、AFN的cookies都是互通的了,这个例子就当作学习cookie的吧 -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | CDD08ACD1CD9E3500020BFA3 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = CDD08ACC1CD9E3500020BFA3 /* main.m */; }; 11 | CDD08AD01CD9E3500020BFA3 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = CDD08ACF1CD9E3500020BFA3 /* AppDelegate.m */; }; 12 | CDD08AD31CD9E3500020BFA3 /* UIWebViewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CDD08AD21CD9E3500020BFA3 /* UIWebViewViewController.m */; }; 13 | CDD08AD61CD9E3500020BFA3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CDD08AD41CD9E3500020BFA3 /* Main.storyboard */; }; 14 | CDD08AD81CD9E3500020BFA3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CDD08AD71CD9E3500020BFA3 /* Assets.xcassets */; }; 15 | CDD08ADB1CD9E3500020BFA3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CDD08AD91CD9E3500020BFA3 /* LaunchScreen.storyboard */; }; 16 | CDD08AE71CD9E5220020BFA3 /* WKWebViewViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = CDD08AE61CD9E5220020BFA3 /* WKWebViewViewController.m */; }; 17 | CDD08AEA1CD9E7E00020BFA3 /* WKCookieSyncManager.m in Sources */ = {isa = PBXBuildFile; fileRef = CDD08AE91CD9E7E00020BFA3 /* WKCookieSyncManager.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | CDD08AC81CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WKWebViewAndUIWebViewCookiesDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | CDD08ACC1CD9E3500020BFA3 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | CDD08ACE1CD9E3500020BFA3 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | CDD08ACF1CD9E3500020BFA3 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | CDD08AD11CD9E3500020BFA3 /* UIWebViewViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = UIWebViewViewController.h; sourceTree = ""; }; 26 | CDD08AD21CD9E3500020BFA3 /* UIWebViewViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = UIWebViewViewController.m; sourceTree = ""; }; 27 | CDD08AD51CD9E3500020BFA3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | CDD08AD71CD9E3500020BFA3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | CDD08ADA1CD9E3500020BFA3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | CDD08ADC1CD9E3500020BFA3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | CDD08AE51CD9E5220020BFA3 /* WKWebViewViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKWebViewViewController.h; sourceTree = ""; }; 32 | CDD08AE61CD9E5220020BFA3 /* WKWebViewViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKWebViewViewController.m; sourceTree = ""; }; 33 | CDD08AE81CD9E7E00020BFA3 /* WKCookieSyncManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKCookieSyncManager.h; sourceTree = ""; }; 34 | CDD08AE91CD9E7E00020BFA3 /* WKCookieSyncManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WKCookieSyncManager.m; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | CDD08AC51CD9E3500020BFA3 /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | CDD08ABF1CD9E3500020BFA3 = { 49 | isa = PBXGroup; 50 | children = ( 51 | CDD08ACA1CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo */, 52 | CDD08AC91CD9E3500020BFA3 /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | CDD08AC91CD9E3500020BFA3 /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | CDD08AC81CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | CDD08ACA1CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | CDD08ACE1CD9E3500020BFA3 /* AppDelegate.h */, 68 | CDD08ACF1CD9E3500020BFA3 /* AppDelegate.m */, 69 | CDD08AD11CD9E3500020BFA3 /* UIWebViewViewController.h */, 70 | CDD08AD21CD9E3500020BFA3 /* UIWebViewViewController.m */, 71 | CDD08AE51CD9E5220020BFA3 /* WKWebViewViewController.h */, 72 | CDD08AE61CD9E5220020BFA3 /* WKWebViewViewController.m */, 73 | CDD08AE81CD9E7E00020BFA3 /* WKCookieSyncManager.h */, 74 | CDD08AE91CD9E7E00020BFA3 /* WKCookieSyncManager.m */, 75 | CDD08AD41CD9E3500020BFA3 /* Main.storyboard */, 76 | CDD08AD71CD9E3500020BFA3 /* Assets.xcassets */, 77 | CDD08AD91CD9E3500020BFA3 /* LaunchScreen.storyboard */, 78 | CDD08ADC1CD9E3500020BFA3 /* Info.plist */, 79 | CDD08ACB1CD9E3500020BFA3 /* Supporting Files */, 80 | ); 81 | path = WKWebViewAndUIWebViewCookiesDemo; 82 | sourceTree = ""; 83 | }; 84 | CDD08ACB1CD9E3500020BFA3 /* Supporting Files */ = { 85 | isa = PBXGroup; 86 | children = ( 87 | CDD08ACC1CD9E3500020BFA3 /* main.m */, 88 | ); 89 | name = "Supporting Files"; 90 | sourceTree = ""; 91 | }; 92 | /* End PBXGroup section */ 93 | 94 | /* Begin PBXNativeTarget section */ 95 | CDD08AC71CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo */ = { 96 | isa = PBXNativeTarget; 97 | buildConfigurationList = CDD08ADF1CD9E3500020BFA3 /* Build configuration list for PBXNativeTarget "WKWebViewAndUIWebViewCookiesDemo" */; 98 | buildPhases = ( 99 | CDD08AC41CD9E3500020BFA3 /* Sources */, 100 | CDD08AC51CD9E3500020BFA3 /* Frameworks */, 101 | CDD08AC61CD9E3500020BFA3 /* Resources */, 102 | ); 103 | buildRules = ( 104 | ); 105 | dependencies = ( 106 | ); 107 | name = WKWebViewAndUIWebViewCookiesDemo; 108 | productName = WKWebViewAndUIWebViewCookiesDemo; 109 | productReference = CDD08AC81CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo.app */; 110 | productType = "com.apple.product-type.application"; 111 | }; 112 | /* End PBXNativeTarget section */ 113 | 114 | /* Begin PBXProject section */ 115 | CDD08AC01CD9E3500020BFA3 /* Project object */ = { 116 | isa = PBXProject; 117 | attributes = { 118 | LastUpgradeCheck = 0730; 119 | ORGANIZATIONNAME = fengqiang; 120 | TargetAttributes = { 121 | CDD08AC71CD9E3500020BFA3 = { 122 | CreatedOnToolsVersion = 7.3; 123 | }; 124 | }; 125 | }; 126 | buildConfigurationList = CDD08AC31CD9E3500020BFA3 /* Build configuration list for PBXProject "WKWebViewAndUIWebViewCookiesDemo" */; 127 | compatibilityVersion = "Xcode 3.2"; 128 | developmentRegion = English; 129 | hasScannedForEncodings = 0; 130 | knownRegions = ( 131 | en, 132 | Base, 133 | ); 134 | mainGroup = CDD08ABF1CD9E3500020BFA3; 135 | productRefGroup = CDD08AC91CD9E3500020BFA3 /* Products */; 136 | projectDirPath = ""; 137 | projectRoot = ""; 138 | targets = ( 139 | CDD08AC71CD9E3500020BFA3 /* WKWebViewAndUIWebViewCookiesDemo */, 140 | ); 141 | }; 142 | /* End PBXProject section */ 143 | 144 | /* Begin PBXResourcesBuildPhase section */ 145 | CDD08AC61CD9E3500020BFA3 /* Resources */ = { 146 | isa = PBXResourcesBuildPhase; 147 | buildActionMask = 2147483647; 148 | files = ( 149 | CDD08ADB1CD9E3500020BFA3 /* LaunchScreen.storyboard in Resources */, 150 | CDD08AD81CD9E3500020BFA3 /* Assets.xcassets in Resources */, 151 | CDD08AD61CD9E3500020BFA3 /* Main.storyboard in Resources */, 152 | ); 153 | runOnlyForDeploymentPostprocessing = 0; 154 | }; 155 | /* End PBXResourcesBuildPhase section */ 156 | 157 | /* Begin PBXSourcesBuildPhase section */ 158 | CDD08AC41CD9E3500020BFA3 /* Sources */ = { 159 | isa = PBXSourcesBuildPhase; 160 | buildActionMask = 2147483647; 161 | files = ( 162 | CDD08AD31CD9E3500020BFA3 /* UIWebViewViewController.m in Sources */, 163 | CDD08AD01CD9E3500020BFA3 /* AppDelegate.m in Sources */, 164 | CDD08AE71CD9E5220020BFA3 /* WKWebViewViewController.m in Sources */, 165 | CDD08ACD1CD9E3500020BFA3 /* main.m in Sources */, 166 | CDD08AEA1CD9E7E00020BFA3 /* WKCookieSyncManager.m in Sources */, 167 | ); 168 | runOnlyForDeploymentPostprocessing = 0; 169 | }; 170 | /* End PBXSourcesBuildPhase section */ 171 | 172 | /* Begin PBXVariantGroup section */ 173 | CDD08AD41CD9E3500020BFA3 /* Main.storyboard */ = { 174 | isa = PBXVariantGroup; 175 | children = ( 176 | CDD08AD51CD9E3500020BFA3 /* Base */, 177 | ); 178 | name = Main.storyboard; 179 | sourceTree = ""; 180 | }; 181 | CDD08AD91CD9E3500020BFA3 /* LaunchScreen.storyboard */ = { 182 | isa = PBXVariantGroup; 183 | children = ( 184 | CDD08ADA1CD9E3500020BFA3 /* Base */, 185 | ); 186 | name = LaunchScreen.storyboard; 187 | sourceTree = ""; 188 | }; 189 | /* End PBXVariantGroup section */ 190 | 191 | /* Begin XCBuildConfiguration section */ 192 | CDD08ADD1CD9E3500020BFA3 /* Debug */ = { 193 | isa = XCBuildConfiguration; 194 | buildSettings = { 195 | ALWAYS_SEARCH_USER_PATHS = NO; 196 | CLANG_ANALYZER_NONNULL = YES; 197 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 198 | CLANG_CXX_LIBRARY = "libc++"; 199 | CLANG_ENABLE_MODULES = YES; 200 | CLANG_ENABLE_OBJC_ARC = YES; 201 | CLANG_WARN_BOOL_CONVERSION = YES; 202 | CLANG_WARN_CONSTANT_CONVERSION = YES; 203 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 204 | CLANG_WARN_EMPTY_BODY = YES; 205 | CLANG_WARN_ENUM_CONVERSION = YES; 206 | CLANG_WARN_INT_CONVERSION = YES; 207 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 208 | CLANG_WARN_UNREACHABLE_CODE = YES; 209 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 210 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 211 | COPY_PHASE_STRIP = NO; 212 | DEBUG_INFORMATION_FORMAT = dwarf; 213 | ENABLE_STRICT_OBJC_MSGSEND = YES; 214 | ENABLE_TESTABILITY = YES; 215 | GCC_C_LANGUAGE_STANDARD = gnu99; 216 | GCC_DYNAMIC_NO_PIC = NO; 217 | GCC_NO_COMMON_BLOCKS = YES; 218 | GCC_OPTIMIZATION_LEVEL = 0; 219 | GCC_PREPROCESSOR_DEFINITIONS = ( 220 | "DEBUG=1", 221 | "$(inherited)", 222 | ); 223 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 224 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 225 | GCC_WARN_UNDECLARED_SELECTOR = YES; 226 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 227 | GCC_WARN_UNUSED_FUNCTION = YES; 228 | GCC_WARN_UNUSED_VARIABLE = YES; 229 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 230 | MTL_ENABLE_DEBUG_INFO = YES; 231 | ONLY_ACTIVE_ARCH = YES; 232 | SDKROOT = iphoneos; 233 | }; 234 | name = Debug; 235 | }; 236 | CDD08ADE1CD9E3500020BFA3 /* Release */ = { 237 | isa = XCBuildConfiguration; 238 | buildSettings = { 239 | ALWAYS_SEARCH_USER_PATHS = NO; 240 | CLANG_ANALYZER_NONNULL = YES; 241 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 242 | CLANG_CXX_LIBRARY = "libc++"; 243 | CLANG_ENABLE_MODULES = YES; 244 | CLANG_ENABLE_OBJC_ARC = YES; 245 | CLANG_WARN_BOOL_CONVERSION = YES; 246 | CLANG_WARN_CONSTANT_CONVERSION = YES; 247 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 248 | CLANG_WARN_EMPTY_BODY = YES; 249 | CLANG_WARN_ENUM_CONVERSION = YES; 250 | CLANG_WARN_INT_CONVERSION = YES; 251 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 252 | CLANG_WARN_UNREACHABLE_CODE = YES; 253 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 254 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 255 | COPY_PHASE_STRIP = NO; 256 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 257 | ENABLE_NS_ASSERTIONS = NO; 258 | ENABLE_STRICT_OBJC_MSGSEND = YES; 259 | GCC_C_LANGUAGE_STANDARD = gnu99; 260 | GCC_NO_COMMON_BLOCKS = YES; 261 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 262 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 263 | GCC_WARN_UNDECLARED_SELECTOR = YES; 264 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 265 | GCC_WARN_UNUSED_FUNCTION = YES; 266 | GCC_WARN_UNUSED_VARIABLE = YES; 267 | IPHONEOS_DEPLOYMENT_TARGET = 9.3; 268 | MTL_ENABLE_DEBUG_INFO = NO; 269 | SDKROOT = iphoneos; 270 | VALIDATE_PRODUCT = YES; 271 | }; 272 | name = Release; 273 | }; 274 | CDD08AE01CD9E3500020BFA3 /* Debug */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 278 | INFOPLIST_FILE = WKWebViewAndUIWebViewCookiesDemo/Info.plist; 279 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 280 | PRODUCT_BUNDLE_IDENTIFIER = thinkerfeng.WKWebViewAndUIWebViewCookiesDemo; 281 | PRODUCT_NAME = "$(TARGET_NAME)"; 282 | }; 283 | name = Debug; 284 | }; 285 | CDD08AE11CD9E3500020BFA3 /* Release */ = { 286 | isa = XCBuildConfiguration; 287 | buildSettings = { 288 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 289 | INFOPLIST_FILE = WKWebViewAndUIWebViewCookiesDemo/Info.plist; 290 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 291 | PRODUCT_BUNDLE_IDENTIFIER = thinkerfeng.WKWebViewAndUIWebViewCookiesDemo; 292 | PRODUCT_NAME = "$(TARGET_NAME)"; 293 | }; 294 | name = Release; 295 | }; 296 | /* End XCBuildConfiguration section */ 297 | 298 | /* Begin XCConfigurationList section */ 299 | CDD08AC31CD9E3500020BFA3 /* Build configuration list for PBXProject "WKWebViewAndUIWebViewCookiesDemo" */ = { 300 | isa = XCConfigurationList; 301 | buildConfigurations = ( 302 | CDD08ADD1CD9E3500020BFA3 /* Debug */, 303 | CDD08ADE1CD9E3500020BFA3 /* Release */, 304 | ); 305 | defaultConfigurationIsVisible = 0; 306 | defaultConfigurationName = Release; 307 | }; 308 | CDD08ADF1CD9E3500020BFA3 /* Build configuration list for PBXNativeTarget "WKWebViewAndUIWebViewCookiesDemo" */ = { 309 | isa = XCConfigurationList; 310 | buildConfigurations = ( 311 | CDD08AE01CD9E3500020BFA3 /* Debug */, 312 | CDD08AE11CD9E3500020BFA3 /* Release */, 313 | ); 314 | defaultConfigurationIsVisible = 0; 315 | }; 316 | /* End XCConfigurationList section */ 317 | }; 318 | rootObject = CDD08AC01CD9E3500020BFA3 /* Project object */; 319 | } 320 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | - (void)applicationWillResignActive:(UIApplication *)application { 24 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 25 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 26 | } 27 | 28 | - (void)applicationDidEnterBackground:(UIApplication *)application { 29 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 30 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 31 | } 32 | 33 | - (void)applicationWillEnterForeground:(UIApplication *)application { 34 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 35 | } 36 | 37 | - (void)applicationDidBecomeActive:(UIApplication *)application { 38 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 39 | } 40 | 41 | - (void)applicationWillTerminate:(UIApplication *)application { 42 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | NSAppTransportSecurity 26 | 27 | NSAllowsArbitraryLoads 28 | 29 | 30 | UILaunchStoryboardName 31 | LaunchScreen 32 | UIMainStoryboardFile 33 | Main 34 | UIRequiredDeviceCapabilities 35 | 36 | armv7 37 | 38 | UISupportedInterfaceOrientations 39 | 40 | UIInterfaceOrientationPortrait 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/UIWebViewViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface UIWebViewViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/UIWebViewViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import "UIWebViewViewController.h" 10 | #import "WKCookieSyncManager.h" 11 | 12 | @interface UIWebViewViewController () 13 | 14 | @property (weak, nonatomic) IBOutlet UIWebView *mainWebView; 15 | 16 | @end 17 | 18 | @implementation UIWebViewViewController 19 | 20 | #pragma mark - LifeCircle 21 | - (void)viewDidLoad { 22 | [super viewDidLoad]; 23 | 24 | [self setMainWebView]; 25 | 26 | } 27 | 28 | #pragma mark - BuildView 29 | - (void)setMainWebView { 30 | NSURL *url = [NSURL URLWithString:@"http://duoshuo.com/"]; 31 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; 32 | [self.mainWebView loadRequest:request]; 33 | 34 | self.mainWebView.delegate = self; 35 | } 36 | 37 | #pragma mark - UIWebViewDelegate 38 | - (void)webViewDidFinishLoad:(UIWebView *)webView { 39 | //创建设置cookie单例 40 | WKCookieSyncManager *cookiesManager = [WKCookieSyncManager sharedWKCookieSyncManager]; 41 | //搬移cookies 42 | [cookiesManager setCookie]; 43 | } 44 | 45 | @end 46 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/WKCookieSyncManager.h: -------------------------------------------------------------------------------- 1 | // 2 | // WKCookieSyncManager.h 3 | // shoes 4 | // 5 | // Created by 奉强 on 16/4/5. 6 | // Copyright © 2016年 saygogo. All rights reserved. 7 | // 8 | 9 | #import 10 | #import 11 | 12 | @interface WKCookieSyncManager : NSObject 13 | 14 | @property (nonatomic, strong) WKProcessPool *processPool; 15 | 16 | - (void)setCookie; 17 | 18 | + (instancetype)sharedWKCookieSyncManager; 19 | 20 | @end 21 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/WKCookieSyncManager.m: -------------------------------------------------------------------------------- 1 | // 2 | // WKCookieSyncManager.m 3 | // shoes 4 | // 5 | // Created by 奉强 on 16/4/5. 6 | // Copyright © 2016年 saygogo. All rights reserved. 7 | // 8 | 9 | #import "WKCookieSyncManager.h" 10 | 11 | @interface WKCookieSyncManager () 12 | 13 | @property (nonatomic, strong) WKWebView *webView; 14 | 15 | ///用来测试的url这个url是不存在的 16 | @property (nonatomic, strong) NSURL *testUrl; 17 | 18 | 19 | @end 20 | 21 | @implementation WKCookieSyncManager 22 | 23 | + (instancetype)sharedWKCookieSyncManager { 24 | static WKCookieSyncManager *sharedWKCookieSyncManagerInstance = nil; 25 | static dispatch_once_t predicate; 26 | dispatch_once(&predicate, ^{ 27 | sharedWKCookieSyncManagerInstance = [[self alloc] init]; 28 | }); 29 | return sharedWKCookieSyncManagerInstance; 30 | } 31 | 32 | - (void)setCookie { 33 | //判断系统是否支持wkWebView 34 | Class wkWebView = NSClassFromString(@"WKWebView"); 35 | if (!wkWebView) { 36 | return; 37 | } 38 | WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init]; 39 | config.processPool = self.processPool; 40 | self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config]; 41 | NSURLRequest *request = [[NSURLRequest alloc] initWithURL:self.testUrl]; 42 | self.webView.navigationDelegate = self; 43 | [self.webView loadRequest:request]; 44 | } 45 | 46 | #pragma - get 47 | - (WKProcessPool *)processPool { 48 | if (!_processPool) { 49 | static dispatch_once_t predicate; 50 | dispatch_once(&predicate, ^{ 51 | _processPool = [[WKProcessPool alloc] init]; 52 | }); 53 | } 54 | 55 | return _processPool; 56 | } 57 | 58 | - (NSURL *)testUrl { 59 | if (!_testUrl) { 60 | NSURLComponents *urlComponents = [NSURLComponents new]; 61 | urlComponents.host = @"duoshuo.com"; 62 | urlComponents.scheme = @"http"; 63 | urlComponents.path = @"/tsttsssdsds.php"; 64 | NSLog(@"测试url=%@", urlComponents.URL); 65 | 66 | return urlComponents.URL; 67 | } 68 | 69 | return _testUrl; 70 | } 71 | 72 | #pragma mark - WKNavigationDelegate 73 | - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation { 74 | //取出cookie 75 | NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 76 | //js函数 77 | NSString *JSFuncString = 78 | @"function setCookie(name,value,expires)\ 79 | {\ 80 | var oDate=new Date();\ 81 | oDate.setDate(oDate.getDate()+expires);\ 82 | document.cookie=name+'='+value+';expires='+oDate;\ 83 | }\ 84 | function getCookie(name)\ 85 | {\ 86 | var arr = document.cookie.match(new RegExp('(^| )'+name+'=([^;]*)(;|$)'));\ 87 | if(arr != null) return unescape(arr[2]); return null;\ 88 | }\ 89 | function delCookie(name)\ 90 | {\ 91 | var exp = new Date();\ 92 | exp.setTime(exp.getTime() - 1);\ 93 | var cval=getCookie(name);\ 94 | if(cval!=null) document.cookie= name + '='+cval+';expires='+exp.toGMTString();\ 95 | }"; 96 | 97 | //拼凑js字符串 98 | NSMutableString *JSCookieString = JSFuncString.mutableCopy; 99 | for (NSHTTPCookie *cookie in cookieStorage.cookies) { 100 | NSString *excuteJSString = [NSString stringWithFormat:@"setCookie('%@', '%@', 1);", cookie.name, cookie.value]; 101 | [JSCookieString appendString:excuteJSString]; 102 | } 103 | //执行js 104 | [webView evaluateJavaScript:JSCookieString completionHandler:nil]; 105 | } 106 | 107 | @end 108 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/WKWebViewViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // WKWebViewViewController.h 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface WKWebViewViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/WKWebViewViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // WKWebViewViewController.m 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import "WKWebViewViewController.h" 10 | #import 11 | #import "WKCookieSyncManager.h" 12 | 13 | @interface WKWebViewViewController () 14 | 15 | @property (nonatomic, strong) WKWebView *mainWebView; 16 | 17 | @end 18 | 19 | @implementation WKWebViewViewController 20 | 21 | #pragma mark - LifeCircle 22 | - (void)viewDidLoad { 23 | [super viewDidLoad]; 24 | 25 | 26 | [self setMainWebView]; 27 | } 28 | 29 | 30 | #pragma mark - BuildView 31 | - (void)setMainWebView { 32 | WKCookieSyncManager *cookiesManager = [WKCookieSyncManager sharedWKCookieSyncManager]; 33 | 34 | WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; 35 | configuration.processPool = cookiesManager.processPool; 36 | 37 | WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) configuration:configuration]; 38 | [self.view addSubview:webView]; 39 | 40 | NSURL *url = [NSURL URLWithString:@"http://duoshuo.com/"]; 41 | NSURLRequest *request = [NSURLRequest requestWithURL:url]; 42 | 43 | [webView loadRequest:request]; 44 | 45 | 46 | self.mainWebView = webView; 47 | } 48 | 49 | /* 50 | #pragma mark - Navigation 51 | 52 | // In a storyboard-based application, you will often want to do a little preparation before navigation 53 | - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 54 | // Get the new view controller using [segue destinationViewController]. 55 | // Pass the selected object to the new view controller. 56 | } 57 | */ 58 | 59 | @end 60 | -------------------------------------------------------------------------------- /WKWebViewAndUIWebViewCookiesDemo/WKWebViewAndUIWebViewCookiesDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // WKWebViewAndUIWebViewCookiesDemo 4 | // 5 | // Created by 奉强 on 16/5/4. 6 | // Copyright © 2016年 fengqiang. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------